question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript custom validator

See original GitHub issue

How 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:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Britskiycommented, Sep 24, 2018

@eliprand thank you! now all works fine 👍

0reactions
eliprandcommented, Sep 24, 2018

@Britskiy , Here is what I ended up doing:

Create a definition file (mine is ./definition.d.ts ) and add the following:

import 'express-validator';

declare module 'express-validator' {
    interface Validator {
        isPassword(): Validator;
    }
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found