Antoniossss
Antoniossss

Reputation: 32550

Can I extend `Validators` namespace to include my custom validator implemetation?

I have to add custom validator for reactive forms. I will do that in form of

export class CustomValidators {
  isMemberOf(allowedValues: any[]) {
    return (ctrl: AbstractControl) => {
      //whatever
    };
  }
}

How can I declare such method so it would appear as part of existing Validators class that is provided with forms module so it will be accessible like Validators.isMemberOf(...) just like Validators.required

Upvotes: 0

Views: 209

Answers (1)

ricardo-dlc
ricardo-dlc

Reputation: 486

Check module augmentation, it could be helpful to your needs

import { Validators } from "your-module";

declare module "your-module" {
    interface Validators {
        isMemberOf(allowedValues: any[]): any;
    }
}

Validators.prototype.isMemberOf = (allowedValues: any[]) => {...}

Upvotes: 1

Related Questions