0

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

Antoniossss
  • 31,590
  • 6
  • 57
  • 99

1 Answers1

1

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[]) => {...}
ricardo-dlc
  • 466
  • 1
  • 3
  • 9
  • Close, but my validator is available (even in intellisense) via `Validators.prototype.isMemberOf`. The problem is, that this must be seen as static function. How to do that? – Antoniossss Nov 04 '20 at 14:12
  • Or you can augment the constructor as showed [here](https://stackoverflow.com/questions/46664732/typescript-how-to-add-static-methods-to-built-in-classes) – ricardo-dlc Nov 04 '20 at 14:21