import type { ImmutableArray, PossibleArray } from "./array.js"; import type { Data } from "./data.js"; import type { ImmutableDictionary } from "./dictionary.js"; import type { AnyCaller } from "./function.js"; /** Object that can validate an unknown value with its `validate()` method. */ export interface Validator { /** * `validate()` method accepts an unsafe value and returns a valid value. * * @param unsafeValue A potentially invalid value. * * @return Valid value. * * @throws `Error` If the value is invalid and cannot be fixed. * @throws `Feedback` If the value is invalid and cannot be fixed and we want to explain why to an end user. */ validate(unsafeValue: unknown): T; } /** Extract the type from a validator. */ export type ValidatorType = X extends Validator ? Y : never; /** A set of named validators in `{ name: Validator }` format. */ export type Validators = { readonly [K in keyof T]: Validator; }; /** Extract the type from a set of validators. */ export type ValidatorsType = { readonly [K in keyof T]: ValidatorType; }; /** Require a valid value for a given validator, or return `undefined` if the value could not be validated. */ export declare function getValid(value: unknown, validator: Validator): T | undefined; /** Require a valid value for a given validator, or throw `RequiredError` if the value could not be validated. */ export declare function requireValid(value: unknown, validator: Validator, caller?: AnyCaller): T; /** * Validate an iterable set of items with a validator. * * @yield Valid items. * @throw Feedback if one or more items did not validate. */ export declare function validateItems(unsafeItems: PossibleArray, validator: Validator): Iterable; /** * Validate an array of items. * * @return Array with valid items. * @throw Feedback if one or more entry values did not validate. */ export declare function validateArray(unsafeArray: PossibleArray, validator: Validator): ImmutableArray; /** * Validate the values of the entries in a dictionary object. * * @throw Feedback if one or more entry values did not validate. */ export declare function validateDictionary(unsafeDictionary: ImmutableDictionary, validator: Validator): ImmutableDictionary; /** * Validate a data object with a set of validators. * - Defined props in the object will be validated against the corresponding validator. * - `undefined` props in the object will be set to the default value of that prop. * - `undefined` props after validation will not be set in the output object. * * @return Valid object. * @throw Feedback if one or more props did not validate. */ export declare function validateData(unsafeData: Data, validators: Validators): T;