sindresorhus
ow

https://github.com/sindresorhus/ow
Go to Github

Function argument validation for humans

https://github.com/sindresorhus/ow/issues/58Go to Github

FundedView inAdd `optional` predicate#58

SamVerschuerencreated this issue 13 days ago

This is something that popped in my head while I was exploring the any and optional predicate. Currently for any we have the following syntax.

ow(5, ow.any(ow.string, ow.number));

The proposed syntax for optional is

ow(x, ow.optional.string);

I already spent quite some time trying to get the type detection work correctly, but I just couldn't get it working. An alternative in line with the any predicate could be ow(x, ow.optional(ow.string)), but that's much ow's there.

An alternative syntax for the previous examples could be

ow.any(5, ow.string, ow.number)
ow.optional(x, ow.string)

The type definition for optional is now very easy to do because x should be either the type of the predicate or undefined. Using a chained operator like ow.optional.string on the other hand is very hard to do (might be even impossible).

I also think it would be easier to add more main operators like not. For instance ow.not(5, ow.string). The only downside is that I think ow(5, ow.not.string) is more readable. Same goes for ow(x, ow.optional.string).

I just wanted to discuss this before we decide to release and make the project opensource. Because it's quite breaking. I would be fine with both approaches. There are benefits and downsides to both. There might be even an other way of dealing with this that I didn't think of so just go ahead and let me know what you guys think.

// @sindresorhus @kevva @vadimdemedes

Logs

Amount funded

$80.00

Resolve this issue and earn $72.00

Fund this issue

Fund as

Payment method

Amount(USD)

Please sign in first to fund this issue

Sign in with

Pull requests

0

    Submit

    URL of your pull request

    Submit your pull request

    Please sign in first to submit a pull request

    Sign in with