-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
Predicates should just accept a value and return an error message or undefined #2
Comments
I'm looking at how this could look like. Not entirely sure how we should do this though. The predicate should return itself to be able to chain it, so we can't do something like this. minLength(length: number) {
return (value: string) => {
if (value.length < number) {
return `Expected string length to be minimum ${number}`;
}
};
} Have to dig deeper if we have nice way of doing this. |
Maybe we could use Proxies to make it easier? |
Alternatively, we could iterate over each method in our class before using it and wrap it in another function. |
Or maybe use TypeScript decorators. |
Was thinking about decorators as well. But I think you would then write your logic in your decorator :p. Going to experiment with proxies first. @Predicate({
message: 'Value should be alphanumeric',
validator: x => /^[0-9\d]+$/i.test(x)
})
get alphanumeric() {
return this;
} |
That looks weird. I was thinking the |
I tried a bunch of things in the meantime. Not sure we could actually make this work with automatic type definitions. This is what I got export class StringPredicate extends Predicate {
constructor(context?: Context) {
super('string', context);
}
@Validator()
minLength(number: number) {
return {
message: () => `Expected string length to be minimum ${number}`,
validator: (value: string) => value.length < number
};
}
} The thing is that de type definitions think that I return an object with |
Right now you have to push it manually to the context. See: #1 (comment)
The text was updated successfully, but these errors were encountered: