jlf-typed-form-craft
Version:
A library to create forms with typed datas and validations
37 lines (36 loc) • 2.17 kB
TypeScript
import { AbstractControl, FormGroup, ValidatorFn } from '@angular/forms';
export interface ConditionalValidatorConfig<T = any> {
condition: (control: AbstractControl) => boolean;
validator: ValidatorFn;
}
/**
* A custom validator function that applies conditional validation logic
* based on a user-defined configuration.
*
* The function evaluates a `condition` provided in the configuration to determine
* whether the associated `validator` function should be applied to the control.
* It also tracks changes to relevant fields in the form and revalidates the control
* whenever any of the dependent fields are updated.
*
* This supports dynamic validation scenarios where the validity of a control
* is determined by its relationship with other controls in the form.
*
* @param {ConditionalValidatorConfig} config - The configuration object containing
* the condition function and the validator.
* @returns {ValidatorFn} A validator function that implements the conditional validation logic.
*/
export declare const conditionalValidator: (config: ConditionalValidatorConfig) => ValidatorFn;
/**
* JlValidators provides a collection of custom validators that execute conditionally
* based on a specified condition function and Angular's built-in validators.
*/
export declare const JlValidators: {
required: (condition: (formGroup: FormGroup) => boolean) => ValidatorFn;
minLength: (length: number, condition: (control: AbstractControl) => boolean, watchFields?: string[]) => ValidatorFn;
maxLength: (length: number, condition: (control: AbstractControl) => boolean, watchFields?: string[]) => ValidatorFn;
pattern: (pattern: string | RegExp, condition: (control: AbstractControl) => boolean, watchFields?: string[]) => ValidatorFn;
email: (condition: (control: AbstractControl) => boolean) => ValidatorFn;
min: (min: number, condition: (control: AbstractControl) => boolean) => ValidatorFn;
max: (max: number, condition: (control: AbstractControl) => boolean) => ValidatorFn;
requiredTrue: (condition: (control: AbstractControl) => boolean) => ValidatorFn;
};