Closed Jblew closed 4 years ago
The feature makes sense. I'm just not convinced on the name. Too me it's not immediately obvious what's happening when reading ow.object.catching
.
It feels more like an alternative version / overload of .validate()
, but not sure we can reuse the name unambiguously.
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 catching
does not immediately tell you what it does. Not sure about a better name though 🤔 .
@Jblew Still interested in finishing this?
Sorry for late response. Interested! .catching
can be misleading I agree.
Then — what would you say for .nest(o => ow())
?
How about .custom()
?
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