UNPKG

1.32 kBTypeScriptView Raw
1import { Struct } from './struct';
2/**
3 * A `StructContext` contains information about the current value being
4 * validated as well as helper functions for failures and recursive validating.
5 */
6export declare type Context = {
7 value: any;
8 type: string;
9 refinement: string | undefined;
10 branch: Array<any>;
11 path: Array<string | number>;
12 fail: (props?: Partial<Failure>) => Failure;
13 check: <T, S>(value: any, struct: Struct<T, S>, parent?: any, key?: string | number) => Iterable<Failure>;
14};
15/**
16 * A `StructFailure` represents a single specific failure in validation.
17 */
18export declare type Failure = {
19 value: Context['value'];
20 type: Context['type'];
21 refinement: Context['refinement'];
22 branch: Context['branch'];
23 path: Context['path'];
24 [key: string]: any;
25};
26/**
27 * A type utility to extract the type from a `Struct` class.
28 */
29export declare type Infer<T extends Struct<any, any>> = T['TYPE'];
30/**
31 * A `StructResult` is returned from validation functions.
32 */
33export declare type Result = boolean | Iterable<Failure>;
34export declare type Coercer = (value: unknown) => unknown;
35export declare type Validator = (value: unknown, context: Context) => Result;
36export declare type Refiner<T> = (value: T, context: Context) => Result;