UNPKG

3.04 kBTypeScriptView Raw
1/**
2 * `Struct` objects encapsulate the schema for a specific data type (with
3 * optional coercion). You can then use the `assert`, `is` or `validate` helpers
4 * to validate unknown data against a struct.
5 */
6export declare class Struct<T, S = any> {
7 type: string;
8 schema: S;
9 coercer: (value: unknown) => unknown;
10 validator: (value: unknown, context: StructContext) => StructResult;
11 refiner: (value: T, context: StructContext) => StructResult;
12 constructor(props: {
13 type: Struct<T>['type'];
14 schema: S;
15 coercer?: Struct<T>['coercer'];
16 validator?: Struct<T>['validator'];
17 refiner?: Struct<T>['refiner'];
18 });
19}
20/**
21 * `StructError` objects are thrown (or returned) by Superstruct when its
22 * validation fails. The error represents the first error encountered during
23 * validation. But they also have an `error.failures` property that holds
24 * information for all of the failures encountered.
25 */
26export declare class StructError extends TypeError {
27 value: any;
28 type: string;
29 path: Array<number | string>;
30 branch: Array<any>;
31 failures: () => Iterable<StructFailure>;
32 [key: string]: any;
33 constructor(failure: StructFailure, iterable: Iterable<StructFailure>);
34}
35/**
36 * A `StructContext` contains information about the current value being
37 * validated as well as helper functions for failures and recursive validating.
38 */
39export declare type StructContext = {
40 value: any;
41 type: string;
42 branch: Array<any>;
43 path: Array<string | number>;
44 fail: (props?: Partial<StructFailure>) => StructFailure;
45 check: (value: any, struct: Struct<any> | Struct<never>, parent?: any, key?: string | number) => Iterable<StructFailure>;
46};
47/**
48 * A `StructFailure` represents a single specific failure in validation.
49 */
50export declare type StructFailure = {
51 value: StructContext['value'];
52 type: StructContext['type'];
53 branch: StructContext['branch'];
54 path: StructContext['path'];
55 [key: string]: any;
56};
57/**
58 * A `StructResult` is returned from validation functions.
59 */
60export declare type StructResult = boolean | Iterable<StructFailure>;
61/**
62 * A type utility to extract the type from a `Struct` class.
63 */
64export declare type StructType<T extends Struct<any>> = Parameters<T['refiner']>[0];
65/**
66 * Assert that a value passes a `Struct`, throwing if it doesn't.
67 */
68export declare function assert<T>(value: unknown, struct: Struct<T>): asserts value is T;
69/**
70 * Coerce a value with the coercion logic of `Struct` and validate it.
71 */
72export declare function coerce<T>(value: unknown, struct: Struct<T>): T;
73/**
74 * Check if a value passes a `Struct`.
75 */
76export declare function is<T>(value: unknown, struct: Struct<T>): value is T;
77/**
78 * Validate a value against a `Struct`, returning an error if invalid.
79 */
80export declare function validate<T>(value: unknown, struct: Struct<T>, coercing?: boolean): [StructError, undefined] | [undefined, T];