TypeScript custom validator
See original GitHub issueHow I can an extend the validator with my validator functions? I do this:
interface Validator extends ExpressValidator.Validator {
isArray: () => ExpressValidator.Validator;
}
export interface IBaseRequest extends ExpressValidator.RequestValidation, express.Request {
checkBody: {
(item: string | string[], message?: string): Validator,
(schema: {}): Validator
};
}
when create express app:
app.use(validator({
customValidators: {
isArray: (value: any) => {
return Array.isArray(value);
}
}
}));
but I think it’s not a right solution. Can you help me or say how I can do this right.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
Top Results From Across the Web
The best way to implement custom validators - Angular inDepth
Learning best practices on how to build your custom validator in Angular by reverse engineering the built-in Angular validators.
Read more >Angular Custom Form Validators: Complete Guide
a custom validator is a function that validates the value of a form field according to a business rule, such as form example...
Read more >Validating form input - Angular
A cross-field validator is a custom validator that compares the values of different fields in a form and accepts or rejects them in...
Read more >How to Create Custom Validators in Angular - DZone
In Angular, creating a custom validator is as simple as creating another function. The only thing you need to keep in mind is...
Read more >Angular custom validators + trick for flexible custom validator
Creating a custom validator for reactive forms is actually more simple than for a template driven form. You only need to implement ValidatorFn,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@eliprand thank you! now all works fine 👍
@Britskiy , Here is what I ended up doing:
Create a definition file (mine is
./definition.d.ts
) and add the following:This will add
isPassword()
as a custom validator, provide intellisense and make sure things compile properly. Remember that you still have to actually register those custom validator during the bootstrapping of your application.The definition file can be anything, anywhere, as long as it’s in the compilation path. It shouldn’t produce anything.