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

Validate if string is part of an array .oneOf(string[]) #80

Closed
SamVerschueren opened this issue May 15, 2018 · 9 comments
Closed

Validate if string is part of an array .oneOf(string[]) #80

SamVerschueren opened this issue May 15, 2018 · 9 comments

Comments

@SamVerschueren
Copy link
Collaborator

I had a use case where I needed to check if the input was a string and was part of an array. So it might be nice if we had a .in(list: string[]) predicate (or another name) which could cover this.

Example

const input = 'foo';

ow(input, ow.string.in(['foo', 'bar']));
@sindresorhus
Copy link
Owner

👍 Good idea

@poupryc
Copy link

poupryc commented May 18, 2018

Maybe change the name for enum ?

@SamVerschueren
Copy link
Collaborator Author

Why? Enums are not the same as arrays. Can you give an example?

@poupryc
Copy link

poupryc commented May 19, 2018

for me, enum refers to the possible value that can take the string. Or I did not fully understand the use cases of "is"

@transitive-bullshit
Copy link
Contributor

I think enum would make more sense if it were the type, not the function. And since enums don't exist as a native type in JS, @SamVerschueren's OP suggestion seems the most consistent to me.

@SamVerschueren
Copy link
Collaborator Author

Exactly, enums are more a TS thing rather than a JS thing.

@poupryc
Copy link

poupryc commented May 20, 2018

Yes, it might be confused. "in" seems fine in this case ^^

@transitive-bullshit
Copy link
Contributor

Alternatively, oneOf might be a little clearer and it follows react's prop-types naming.

ow(input, ow.string.oneOf(['foo', 'bar']));

@sindresorhus
Copy link
Owner

👍 oneOf

@SamVerschueren SamVerschueren changed the title Validate if string is part of an array .in(string[]) Validate if string is part of an array .oneOf(string[]) May 28, 2018
SamVerschueren added a commit that referenced this issue May 28, 2018
SamVerschueren added a commit that referenced this issue May 30, 2018
SamVerschueren added a commit that referenced this issue May 30, 2018
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

4 participants