@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
47 lines (46 loc) • 1.94 kB
TypeScript
import { Validator } from '../validator.type';
import { createMaxLengthValidator } from './max-length';
import { createMinLengthValidator } from './min-length';
export type ArrayMessageFactories = {
minLength: Parameters<typeof createMinLengthValidator>[0];
maxLength: Parameters<typeof createMaxLengthValidator>[0];
};
/**
* Configuration options for creating a combined array validator using the
* `.all()` method returned by `createArrayValidators` (accessed via `injectValidators().array.all`).
*/
export type ArrayValidatorOptions = {
/**
* Minimum allowed array length.
* Validation fails if the array has fewer elements than this number.
* @example { minLength: 1 } // Array must not be empty
*/
minLength?: number;
/**
* Maximum allowed array length.
* Validation fails if the array has more elements than this number.
* @example { maxLength: 5 } // Array can have at most 5 items
*/
maxLength?: number;
/**
* Optional label for the array elements used in generated error messages
* (e.g., 'items', 'users', 'tags'). Defaults typically to 'items'.
* @example { minLength: 2, elementsLabel: 'tags' } // Error might be "Min 2 tags"
*/
elementsLabel?: string;
};
export declare function createArrayValidators(factories?: Partial<ArrayMessageFactories>, merger?: <T>(validators: Validator<T>[]) => ((value: T) => string) & {
resolve: (mergedError: string) => {
error: string;
tooltip: string;
};
}): {
all: <T extends any[]>(opt: ArrayValidatorOptions) => ((value: T) => string) & {
resolve: (mergedError: string) => {
error: string;
tooltip: string;
};
};
minLength: <T extends string | any[] | null>(min: number, elementsLabel?: string) => Validator<T>;
maxLength: <T extends string | any[] | null>(max: number, elementsLabel?: string) => Validator<T>;
};