import { StructSchema } from './utils.js'; import { StructError, Failure } from './error.js'; /** * `Struct` objects encapsulate the validation logic for a specific type of * values. Once constructed, you use the `assert`, `is` or `validate` helpers to * validate unknown input data against the struct. */ export declare class Struct { readonly TYPE: T; type: string; schema: S; coercer: (value: unknown, context: Context) => unknown; validator: (value: unknown, context: Context) => Iterable; refiner: (value: T, context: Context) => Iterable; entries: (value: unknown, context: Context) => Iterable<[string | number, unknown, Struct | Struct]>; constructor(props: { type: string; schema: S; coercer?: Coercer; validator?: Validator; refiner?: Refiner; entries?: Struct['entries']; }); /** * Assert that a value passes the struct's validation, throwing if it doesn't. */ assert(value: unknown, message?: string): asserts value is T; /** * Create a value with the struct's coercion logic, then validate it. */ create(value: unknown, message?: string): T; /** * Check if a value passes the struct's validation. */ is(value: unknown): value is T; /** * Mask a value, coercing and validating it, but returning only the subset of * properties defined by the struct's schema. Masking applies recursively to * props of `object` structs only. */ mask(value: unknown, message?: string): T; /** * Validate a value with the struct's validation logic, returning a tuple * representing the result. * * You may optionally pass `true` for the `coerce` argument to coerce * the value before attempting to validate it. If you do, the result will * contain the coerced result when successful. Also, `mask` will turn on * masking of the unknown `object` props recursively if passed. */ validate(value: unknown, options?: { coerce?: boolean; mask?: boolean; message?: string; }): [StructError, undefined] | [undefined, T]; } /** * Assert that a value passes a struct, throwing if it doesn't. */ export declare function assert(value: unknown, struct: Struct, message?: string): asserts value is T; /** * Create a value with the coercion logic of struct and validate it. */ export declare function create(value: unknown, struct: Struct, message?: string): T; /** * Mask a value, returning only the subset of properties defined by a struct. */ export declare function mask(value: unknown, struct: Struct, message?: string): T; /** * Check if a value passes a struct. */ export declare function is(value: unknown, struct: Struct): value is T; /** * Validate a value against a struct, returning an error if invalid, or the * value (with potential coercion) if valid. */ export declare function validate(value: unknown, struct: Struct, options?: { coerce?: boolean; mask?: boolean; message?: string; }): [StructError, undefined] | [undefined, T]; /** * A `Context` contains information about the current location of the * validation inside the initial input value. It also carries `mask` * since it's a run-time flag determining how the validation was invoked * (via `mask()` or via `validate()`), plus it applies recursively * to all of the nested structs. */ export type Context = { branch: Array; path: Array; mask?: boolean; }; /** * A type utility to extract the type from a `Struct` class. */ export type Infer> = T['TYPE']; /** * A type utility to describe that a struct represents a TypeScript type. */ export type Describe = Struct>; /** * A `Result` is returned from validation functions. */ export type Result = boolean | string | Partial | Iterable>; /** * A `Coercer` takes an unknown value and optionally coerces it. */ export type Coercer = (value: T, context: Context) => unknown; /** * A `Validator` takes an unknown value and validates it. */ export type Validator = (value: unknown, context: Context) => Result; /** * A `Refiner` takes a value of a known type and validates it against a further * constraint. */ export type Refiner = (value: T, context: Context) => Result; //# sourceMappingURL=struct.d.ts.map