import { Brand, Failure, Result } from '../base'; import { ConstraintTrait, ValidatorTraits } from './traits'; /** * Type for a validation function, which validates that a supplied `unknown` * value is a valid value of type ``, possibly as influenced by * an optionally-supplied validation context of type ``. * @public */ export type ValidatorFunc = (from: unknown, context?: TC, self?: Validator) => boolean | Failure; /** * Options that apply to any {@link Validation.Validator | Validator}. * @public */ export interface ValidatorOptions { defaultContext?: TC; } /** * A {@link Validation.Constraint | Constraint} function returns * `true` if the supplied value meets the constraint. Can return * {@link Failure} with an error message or simply return `false` * for a default message. * @public */ export type Constraint = (val: T) => boolean | Failure; /** * Formats an incoming error message and value that failed validation. * @param val - The value that failed validation. * @param message - The default error message, if any. * @param context - Optional validation context. * @returns The formatted error message. * @public */ export type ValidationErrorFormatter = (val: unknown, message?: string, context?: TC) => string; /** * In-place validation that a supplied unknown matches some * required characteristics (type, values, etc). * @public */ export interface Validator { /** * {@link Validation.ValidatorTraits | Traits} describing this validation. */ readonly traits: ValidatorTraits; /** * Indicates whether this element is explicitly optional. */ readonly isOptional: boolean; /** * The brand for a branded type. */ readonly brand: string | undefined; /** * Tests to see if a supplied `unknown` value matches this validation. All * validate calls are guaranteed to return the entity passed in on Success. * @param from - The `unknown` value to be tested. * @param context - Optional validation context. * @returns {@link Success} with the typed, validated value, * or {@link Failure} with an error message if validation fails. */ validate(from: unknown, context?: TC): Result; /** * Tests to see if a supplied 'unknown' value matches this validation. In * contrast to {@link Validator.validate | validate}, makes no guarantees * about the identity of the returned value. * @param from - The `unknown` value to be tested. * @param context - Optional validation context. * @returns {@link Success} with the typed, conversion value, * or {@link Failure} with an error message if conversion fails. */ convert(from: unknown, context?: TC): Result; /** * Tests to see if a supplied `unknown` value matches this * validation. Accepts `undefined`. * @param from - The `unknown` value to be tested. * @param context - Optional validation context. * @returns {@link Success} with the typed, validated value, * or {@link Failure} with an error message if validation fails. */ validateOptional(from: unknown, context?: TC): Result; /** * Non-throwing type guard * @param from - The value to be tested. * @param context - Optional validation context. */ guard(from: unknown, context?: TC): from is T; /** * Creates an {@link Validation.Validator | in-place validator} * which is derived from this one but which also matches `undefined`. */ optional(): Validator; /** * Creates an {@link Validation.Validator | in-place validator} * which is derived from this one but which applies additional constraints. * @param constraint - the constraint to be applied * @param trait - As optional {@link Validation.ConstraintTrait | ConstraintTrait} * to be applied to the resulting {@link Validation.Validator | Validator}. * @returns A new {@link Validation.Validator | Validator}. */ withConstraint(constraint: Constraint, trait?: ConstraintTrait): Validator; /** * Creates a new {@link Validation.Validator | in-place validator} which * is derived from this one but which matches a branded result. * @param brand - The brand to be applied. */ withBrand(brand: B): Validator, TC>; /** * Creates a new {@link Validation.Validator | in-place validator} which * is derived from this one but which returns an error message supplied * by the provided formatter if an error occurs. * @param formatter - The error message formatter to be applied. * @returns A new {@link Validation.Validator | Validator}. */ withFormattedError(formatter: ValidationErrorFormatter): Validator; } //# sourceMappingURL=validator.d.ts.map