-
-
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
Add a .catching()
method to Predicate to allow nesting ow validators
#154
Conversation
The feature makes sense. I'm just not convinced on the name. Too me it's not immediately obvious what's happening when reading |
.catching()
method to Predicate to allow nesting ow validators
It feels more like an alternative version / overload of |
@@ -240,6 +240,29 @@ ow(1, 'input', ow.number.validate(value => ({ | |||
|
|||
This can be useful for creating your own reusable validators which can be extracted to a separate npm package. | |||
|
|||
#### catching(fn) | |||
|
|||
Use a custom validation function that throws error. The function should throw an error if value is invalid or return nothing when value is valid. This allows to use specific validators for complex property or array types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear whether it support any error or only an Ow ArgumentError. If both, is there any different behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say let's support only the ow one. This way we are not swallowing anyones errors.
|
||
ow(animals, ow.array.ofType(ow.object.catching(animal => validateAnimal(animal as Animal)))); | ||
//=> ArgumentError: (array `animals`) (object) Expected number `Animal.weight` to be finite, got Infinity | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this to the doc comment too?
So basically it's a shorthand for this? const nestedOwValidator = (value: SpecificType) => {
try {
ow(value.x, ow.number.finite);
return true;
} catch (error) {
return error.message;
}
};
const arrayOfNumbers: SpecificType [] = [
{x: 1},
{x: Number.POSITIVE_INFINITY}
];
ow(arrayOfNumbers, ow.array.ofType(ow.object.is(o => nestedOwValidator(o)))); I agree that |
@Jblew Still interested in finishing this? |
Sorry for late response. Interested! Then — what would you say for |
How about |
Bump |
Closing for lack of response. |
I love ow and use it heavily in my TS projects. I use a pattern in which I create a validator function for my interfaces. This validator function uses multiple ow calls to guard the properties. Often an interface is used as a type of a property inside another interface, and that interface also has a validator. It would be very handful if I could just validate this specific property against an already defined validator. Speaking code my pull request allows to do the following:
This is a way I am achiving it now: https://github.com/wise-team/wise-hub/blob/b0ef49a78ff51e3c8a866513a7d13a68d4b8959a/backend/src/publisher/entities/PublishJob.ts My pull request allows the above way which is far more clean and readable. Of course I've added unit tests for the feature.
I named the method
.catching()
because it simply catches errors. Also, 'catch' is a keyword and I was not sure it is safe to use it in a property-method context. I am not sure If it is the best name.I am really looking forward to use this feature!
Thank you in advance,
Jędrzej