-
-
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 support for a custom validation function #137
Conversation
When I was looking into my The reason why we do this is to make sure users don't get it autocompleted when they type Not sure how we should fix this. I see two options here. 1. Export a custom classWe might export a stripped down version of
The downside of this is that if I would publish my 2. Remove
|
I definitely prefer option 2. |
Definitely my preference as well, the first one has quite a lot of drawbacks. I'll have it fixed! |
I'm stuck on this problem for a couple of days now and I can't find a solution. So I tried with the greaterThan(x: number) {
return addValidator(this, {
message: (value, label) => `Expected ${label} to be greater than ${x}, got ${value}`,
validator: value => value > x
});
} Whereas export default <T, P extends Predicate<T> = Predicate<T>>(predicate: P, validator: Validator<T>) => {
predicate[validatorSymbol].push(validator);
return predicate;
}; This doesn't work. TypeScript is unable to infer the type of A fix would be to provide type greaterThan(x: number) {
return addValidator<number>(this, {
message: (value, label) => `Expected ${label} to be greater than ${x}, got ${value}`,
validator: value => value > x
});
} And then it correctly verifies that So I started looking at it again and one approach would be to mark But then I don't have a solution for the After I wrote all of this above I noticed that actually it's impossible to remove If we make it export const not = (predicate: any) => {
const originalAddValidator = predicate.addValidator;
predicate.addValidator = (validator: any) => {
const fn = validator.validator;
const message = validator.message;
validator.message = (x: any, label: string) => `[NOT] ${message(x, label)}`;
validator.validator = (x: any) => !fn(x);
predicate[validatorSymbol].push(validator);
predicate.addValidator = originalAddValidator;
return predicate;
};
return predicate;
}; We loose the types inside the To be honest, it feels like the only solution at the moment 😂. |
Let's go with that and open a new issue about improving it. Maybe someone else sees a solution we don't. Since it's not exposed to users, it's not essential. Better to just make it works now. |
Co-Authored-By: SamVerschueren <sam.verschueren@gmail.com>
Is this ready to merge? |
Co-Authored-By: SamVerschueren <sam.verschueren@gmail.com>
The only thing I didn't do yet is #137 (comment). Let me do it now. |
Think it's good to go |
Awesome :) |
This PR fixes #40 by adding support for
The message can also be a function which passes in the (inferred) label. Thought it would be nice to have that as it allows you to customize the error message even more.
I also added a test for custom predicates which was actually already supported. Not sure if creating a custom predicate should be documented? I've started my
ow-date
package so I could link to that one in the docs.Let me know if something is unclear or should be added.