Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to check optional type #25

Open
Shinigami92 opened this issue Aug 22, 2019 · 2 comments
Open

Ability to check optional type #25

Shinigami92 opened this issue Aug 22, 2019 · 2 comments
Assignees

Comments

@Shinigami92
Copy link

Is there a way to check if a target is string | undefined?

I have the current structure:

// value := [string, string, string?]
check(value, 'value').exists();
check(value, 'value').is.an.array();
check(value.length, 'value').is.not.empty();

check(value[0], 'value[0]').exists();
check(value[0], 'value[0]').is.a.string();

check(value[1], 'value[1]').exists();
check(value[1], 'value[1]').is.a.string();

// Maybe with .optional ?
check(value[2], 'value[2]').is.optional.a.string();

Currently when I use check(value[2], 'value[2]').is.a.string();
I get Check failed: value[2] (undefined) was not a String,
but I want to check if it is a string or undefined

@pnann pnann self-assigned this Aug 22, 2019
@pnann
Copy link
Owner

pnann commented Aug 24, 2019

I've been thinking about this, and appreciate the feature request -- this is definitely a common case in public APIs.

One alternative is to support chaining (e.g. check(a).exists().and.is.a.string()), give me a bit of time to think it through.

@Shinigami92
Copy link
Author

I think chaining with and would be nice.
This could be the right way as it is the same as BDD and is used in chai or similar libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants