import { Language } from "../../translations/types.js"; /** * Result of a validation. */ export interface ValidationResult { /** * Status of the validation. True if all validators passed, false otherwise. */ isValid: boolean; /** * Individual validation step's messages, e.g. "Cannot exceed 20 characters" or "Must contain at least one capital letter" */ errorMessages: ValidationMessage[]; /** * Whether validation should be shown immediately, instead of waiting for the user to leave the form field */ showMessageImmediately: boolean; /** * The value that was validated, after transformation according to the validator's `preValidation` function. */ value: T; } /** * Get the validation message, if any, for a given {@link ValidationResult}. * Combines the field's message prefix and the message from the first failed validation step. * * @param result The {@link ValidationResult} for a form field * @param messagePrefix The validation message prefix for a form field * @returns A validation message (empty string if no errors) */ export declare function getValidationMessage(result: ValidationResult | undefined, messagePrefix: ValidationMessage, lang: Language): string; /** * Result of a validation step */ export interface ValidationStepResult { isValid: boolean; errorMessage: ValidationMessage; } /** * Validation step function */ export type ValidationStep = (value: T, allValues: TAll) => ValidationStepResult; export type ValidationMessage = string | ((lang: Language) => string); export interface NecessityConfig { isRequired: boolean; missingMessage?: ValidationMessage; } /** * Configuration object for a validation pipeline */ export interface ValidatorConfig { /** * A list of validation steps which are run from left to right */ steps: ValidationStep[]; /** * A function that transforms the value before validation. If not supplied, the default is value.trim() for string values. */ preValidation: (value: T) => T; /** * A function which, if it returns true, shows the error message immediately instead of waiting for user to move on to the next field. */ showMessageImmediately?: (value: T) => boolean; /** * Validation message prefix, e.g. "Invalid name." */ messagePrefix: ValidationMessage; /** * Necessity of the field (required or optional) */ necessity: NecessityConfig | ((values: TAll) => NecessityConfig); } export declare function stripWhitespace(value: string): string; /** * Create a complete validation config using supplied partial config and the default config. * @param config Configuration for the validation pipeline. Is merged with default config. * @returns A {@link ValidatorConfig} that can be run using runValidation on input values * * @example export const validateRegNo = validation({ steps: [hasLength(REG_NO_FULL_LENGTH), matchesRegex(REG_NO_PATTERN)], preValidation: stripWhitespace, }); */ export declare const validation: (config?: Partial>) => ValidatorConfig; export declare function runValidation(rawValue: T, allValues: TAll, { necessity, preValidation, steps, showMessageImmediately, }: ValidatorConfig): ValidationResult; /** * Modifies a validator to make the field required. * * @param validator A {@link ValidatorConfig} describing a validation * @param message Validation message when the value is missing * @returns The modified validator config */ export declare const required: (validator?: ValidatorConfig, message?: ValidationMessage) => ValidatorConfig; /** * Validator map. Each property is a validator for the corresponding property in the input object. */ export type ValidatorMap = { [K in keyof T]: ValidatorConfig; }; /** * Validation data object. Each property contains validation results for the corresponding property in the input object. */ export type ValidationData = { [K in keyof T]: ValidationResult; }; /** * Object validation options */ export interface ObjectValidationOptions { /** * Whether to show necessity indicator on required or optional form fields. By default, indicator will * be shown on the least used type of filed. E.g. if there are more required fields than optional, * indicator will be shown on optional fields. * * If all properties have the same necessity, the indicator will not be shown regardless of this setting. */ showNecessityOn: "required" | "optional"; } /** * Result of object validation. */ export type ObjectValidationResult = { /** * Overall validity of the object. True if all properties are valid. */ isValid: boolean; /** * Validation data for all properties */ validation: ValidationData; /** * Transformed properties (e.g. without leading/trailing whitespace) */ transformed?: T; /** * Valid, transformed properties (e.g. without leading/trailing whitespace) */ validated: Partial; } & ({ isValid: true; validated: T; } | { isValid: false; }); /** * Object validator function */ export type ObjectValidator = (object: T) => ObjectValidationResult; /** * Create a validation function for an object, given a validation map for properties of that object * @param validators A {@link ValidatorMap} to validate against * @returns A validation function that can be run on input objects and returns a {@link ObjectValidationResult} */ export declare const createObjectValidator: (validators: ValidatorMap) => ObjectValidator; export type ShouldShowError = { [K in keyof T]?: boolean; }; /** * A representation of user input of multiple properties with validation */ export type UserInput = { /** * The raw user input */ raw: T; /** * Map of whether to show error message for a given field */ showError: ShouldShowError; } & ObjectValidationResult; /** * Create the initial {@link UserInput} representation given an object of user input * @param obj The raw user input * @returns Initial validation state, before running validation */ export declare function initialUserInput(obj: T, validator: ObjectValidator): UserInput; /** * Figure out if an object validator has only required fields, only optional fields, or a mix of both * @param validators The {@link ValidatorMap} for an object * @returns "required" if only required fields, "optional" if only optional fields, otherwise "mixed". */ export declare function getFieldNecessities(values: T, validators: ValidatorMap): "optional" | "required" | "mixed"; export declare function addNecessityIndicator(values: T, validators: ValidatorMap, showNecessityOn: "required" | "optional", lang: Language, helperText?: string, field?: keyof T): string | undefined; //# sourceMappingURL=validation.d.ts.map