-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
Object shape validator #92
Comments
I agree that this would be useful, though I'm not sure I'm in favor of adding it to the core API since unlike its usage in ow(input, ow.object)
ow(input.color, ow.string)
ow(input.height, ow.number.greatherThan(10)) This isn't possible with My main argument against it would be to keep the API surface as small as possible, but I do find myself using this pattern quite often especially for validating configuration parameters Take this concrete example where I'm using ow for this purpose. It'd be really awkward if I tried to use a combination of I could go either way, though, since I'm sure there are use cases where it'll still be useful to peeps. Just my 2 cents 👍 |
I think it should be in core. I very often have APIs with an options-object and I want to quickly validate the options. Having to do separate I think this: ow(target, ow.object.label('target').shape({
width: ow.number.positive.integer,
height: ow.number.positive.integer,
data: ow.any(ow.uint8Array, ow.uint8ClampedArray)
})) is better than this: ow(target, ow.object.label('target'))
ow(target.width, ow.number.positive.integer.label('target.width'))
ow(target.height, ow.number.positive.integer.label('target.height'))
ow(target.data, ow.any(ow.uint8Array, ow.uint8ClampedArray)) We also wouldn't need |
💯 Sounds good to me :) |
We discussed this somewhere else, can't find it though. I believe our initial suggestion was just ow(target, {
width: ow.number.positive.integer,
height: ow.number.positive.integer,
data: ow.any(ow.uint8Array, ow.uint8ClampedArray)
}) So just passing an object as second parameter would do the trick. I see positive sides to both ideas. |
@SamVerschueren I like that shorthand, but there's no way to use a label or to make it optional (when that is added) with that... |
|
Just a note; the |
@transitive-bullshit The |
@issuehuntfest has funded $60.00 to this issue. See it on IssueHunt |
Using this for simple input validation, unfortunately only throws on the first error, but that's all I need to make sure there are no erroneous inputs being passed.
Usage:
|
This is a nice wrapper @mhaagens 😄 Any reason it has to be asynchronous? |
Thanks! :) Doesn't have to be at all. Just like to be explicit about returning a promise when using async/await and I like the syntax better than callbacks :). |
@mhaagens I meant that there's no need to use async / Promise or callbacks in this case since everything's synchronous. const matchShape = (source, shape) => {
for (const key of Object.keys(source)) {
ow(source[key], shape[key]);
}
}
matchShape(obj, { id: ow.string }) |
@transitive-bullshit Ah, yeah you're right! |
I want to start working on this. But before I do, what API are we going to use now? // Allows extra properties
ow(target, ow.object.shape({
width: ow.number.positive.integer,
height: ow.number.positive.integer,
data: ow.any(ow.uint8Array, ow.uint8ClampedArray)
})); And // Doesn't allow extra properties
ow(target, ow.object.exact({
width: ow.number.positive.integer,
height: ow.number.positive.integer,
data: ow.any(ow.uint8Array, ow.uint8ClampedArray)
}));
|
👍 When you only see |
The Does ow({unicorn: '🦄'}, ow.object.exactShape({
unicorn: ow.string,
rainbow: ow.string
})); ow({unicorn: '🦄', rainbow: '🌈'}, ow.object.exactShape({
unicorn: ow.string
})); Should both of these checks fail, or only one of them? It has quite some impact on the implementation. |
Exact should mean "exact" in that the shape should have all the same properties as the input object. |
Yes |
@sindresorhus has rewarded $54.00 to @SamVerschueren. See it on IssueHunt
|
Would be useful to be able to validate the shape of objects, like
PropTypes.shape
I imagine it could be like:
@SamVerschueren @transitive-bullshit Thoughts?
The text was updated successfully, but these errors were encountered: