import { FormField } from './typeguards.js'; /** * Function type for validating a form. * * @template T - The type of form values. * @template F - The array of form fields. * * Takes the form values, form fields, and an array of validators, and returns * a `FieldErrors` object mapping field names to their validation errors. */ type FormValidator = (values: T, fields: F, validators: Validator[]) => FieldErrors; /** * A generic validator function type for form fields. * * @template DefaultMessage - The default error message to use if validation fails. Defaults to `string`. * * Provides two call signatures: * 1. With a custom message (`Message`) – returns `Message | undefined`. * 2. Without a message – returns `DefaultMessage | undefined`. * * @example * ```ts * const DEFAULT_ERROR = "Validation failed" as const; * * // Abstract validator using a default literal message * const validateField: Validator = (value, field, message = DEFAULT_ERROR) => { * // Example validation logic: value must be truthy if field is required * if (!field.required || value) return undefined; * return message; * }; * * const someField = { required: true } as FormField; * * // Using validator without a custom message * const result1 = validateField(undefined, someField); * // result1 inferred as "Validation failed" | undefined * * // Using validator with a custom literal message * const result2 = validateField(undefined, someField, "Custom error" as const); * // result2 inferred as "Custom error" | undefined * ``` */ type Validator = { (value: unknown, field: FormField, message: Message): Message | undefined; (value: unknown, field: FormField): DefaultMessage | undefined; }; /** * Maps form field names to their validation error messages. * * @template F - Array of form fields. * @template V - Array of validators. * * For each field: * - If the field value is a single item, its error is an array of validator return types. * - If the field value is an array, its error is an array of arrays of validator return types * (one array of errors per element in the value array). */ type FieldErrors = { [K in Exclude]?: V[number] extends Validator ? ValidatorReturn[] | ValidatorReturn[][] : string[] | string[][]; }; /** * Extracts the literal type returned by a validator. * Returns `M | undefined` if `V` is `Validator`, otherwise `string | undefined`. */ type ValidatorReturn = V extends Validator ? M | undefined : string | undefined; declare const DEFAULT_VALIDATION_ERROR_REQUIRED: "Dit veld is verplicht"; /** * Validator that checks whether a value is set for a required field. * * Returns the provided `message` or `DEFAULT_VALIDATION_ERROR_REQUIRED` if validation fails. * Returns `undefined` if the field is not required or a value is present. */ declare const validateRequired: Validator; /** * Validates a form by running each field's value through all provided validators. * * Behavior: * - For scalar values: returns an array of validator results. * - For array values: returns an array of arrays of validator results (one per item). * * Produces the full, detailed `FieldErrors` structure. * To collapse these into representative error messages, use {@link getErrorFromErrors}. * * @template T - The object type representing form values. * @template F - The tuple/array type of form fields. * * @param values - The current form values, keyed by field name. * @param fields - The metadata definitions for each form field. * @param validators - An optional list of validators to run for each field. * @returns A `FieldErrors` object mapping field names to their corresponding validation errors. */ declare const validateForm: FormValidator; /** * Retrieves a normalized error message for a specific field from a `FieldErrors`-like object. * * This function is intended to be used with the output of {@link validateForm}. * It handles both single-value errors and array-of-errors (e.g., Formik-style array fields), * returning a single error message string for the given field if one exists. * * @param fields - The array of form field definitions. * @param errors - An object mapping field names to their errors. Each value can be: * - a string (single error) * - an array of strings (multiple errors per field) * @param field - The specific field whose error should be retrieved. * @returns A single error message string for the field, or `undefined` if no error exists. */ declare const getErrorFromErrors: (fields: F, errors: FieldErrors, field: F[number]) => ValidatorReturn[] | undefined; /** * Normalizes a record of error messages into a record of error arrays. * * This ensures that every field maps to an array of error strings, * which simplifies downstream validation or form handling logic. * * @typeParam T - The object shape whose keys represent error fields. * @param record - A record mapping keys of `T` to a single error message. * @returns A record mapping the same keys to arrays of error messages. * * @example * ```ts * const errors = { username: "Required", email: "Invalid" }; * const result = errors2errorsArray(errors); * // result: { username: ["Required"], email: ["Invalid"] } * ``` */ declare const errors2errorsArray: (record?: FieldErrors | Partial>) => Partial>; export { DEFAULT_VALIDATION_ERROR_REQUIRED, errors2errorsArray, getErrorFromErrors, validateForm, validateRequired }; export type { FieldErrors, FormValidator, Validator };