@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
42 lines (41 loc) • 2.1 kB
TypeScript
import { DateMessageFactories, DateValidatorOptions } from '.';
import { Validator } from '../validator.type';
import { defaultFormatDate } from './util';
/**
* Represents a date range with two date values: `from` and `to`.
* Both values can be `null` to indicate an empty range.
* @template TDate The type of the date values (e.g., `Date`, Luxon `DateTime`, Moment).
*/
export type DateRange<TDate = Date> = {
/** The start date of the range. Can be `null` if not set. */
start: TDate | null;
/** The end date of the range. Can be `null` if not set. */
end: TDate | null;
};
export declare function createDateRangeValidators<TDate = Date>(factories?: Partial<DateMessageFactories>, toDate?: (date: string | TDate) => Date, formatDate?: typeof defaultFormatDate, locale?: string, generalValidators?: {
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>;
}, merger?: <T>(validators: Validator<T>[]) => ((value: T) => string) & {
resolve: (mergedError: string) => {
error: string;
tooltip: string;
};
}): {
all: (opt: Pick<DateValidatorOptions, "required" | "min" | "max" | "messageOptions">) => ((value: DateRange<TDate>) => string) & {
resolve: (mergedError: string) => {
error: string;
tooltip: string;
};
};
util: {
toDate: (date: string | TDate) => Date;
formatDate: typeof defaultFormatDate;
};
min: (date: string | TDate) => Validator<string | TDate | null>;
max: (date: string | TDate) => Validator<string | TDate | null>;
isDate: () => Validator<string | TDate | null>;
};