sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.81k stars 108 forks source link

Check for optional first before running validator #125

Closed SamVerschueren closed 5 years ago

SamVerschueren commented 5 years ago

The optional predicate currently has a bug. The bug is that the validator is still executed, even when the value is undefined. This is wrong off course because in case of this use case

ow(undefined, ow.optional.string.minLength(3));

The predicate will do undefined.length which throws a Cannot read property 'length' of 'undefined' (or something).

This PR doesn't do that (performance benefit because it doesn't run all the validators) as it first checks if it's optional and if the value is undefined.