@mmstack/form-validation
Version:
Provides a type-safe, composable, and localizable validation system designed specifically for use with [@mmstack/form-core](https://www.npmjs.com/package/@mmstack/form-core). It enables defining validation rules clearly within your TypeScript code and int
36 lines (35 loc) • 2.04 kB
TypeScript
import { Validator } from '../validator.type';
import { createMustBeEmptyValidator, createMustBeValidator } from './must-be';
import { createNotValidator } from './not';
import { createNotOneOfValidator } from './not-one-of';
import { createOneOfValidator } from './one-of';
import { createRequiredValidator } from './required';
export type GeneralMessageFactories = {
required: Parameters<typeof createRequiredValidator>[0];
mustBe: Parameters<typeof createMustBeValidator>[0];
mustBeNull: Parameters<typeof createMustBeEmptyValidator>[0];
not: Parameters<typeof createNotValidator>[0];
oneOf: Parameters<typeof createOneOfValidator>[0];
notOneOf: Parameters<typeof createNotOneOfValidator>[0];
};
/**
* Represents the consolidated object containing all available validator generators,
* configured with appropriate localization and date handling for the specified `TDate` type.
*
* Obtain this object using `injectValidators()` within an Angular injection context.
* Use the nested `.all()` methods (e.g., `validators.string.all({...})`) with their
* corresponding options types (e.g., `StringValidatorOptions`) to create combined
* validator functions for your form controls.
*
* Individual validators (e.g., `validators.general.required()`) are also available.
*
* @template TDate The type used for date values (e.g., Date, Luxon DateTime). Defaults to `Date`.
*/
export declare function createGeneralValidators(factories?: Partial<GeneralMessageFactories>): {
required: <T>(label?: string) => Validator<T>;
mustBe: <T>(value: T, valueLabel?: string, matcher?: (a: T, b: T) => boolean) => Validator<T>;
mustBeNull: <T>() => Validator<T>;
not: <T>(value: T, valueLabel?: string, matcher?: (a: T, b: T) => boolean) => Validator<T>;
oneOf: <T>(values: T[], toLabel?: (value: T) => string, identity?: (a: T) => string, delimiter?: string) => Validator<T>;
notOneOf: <T>(values: T[], toLabel?: (value: T) => string, identity?: (a: T) => string, delimiter?: string) => Validator<T>;
};