1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export 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 | */
|
26 | export 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 | */
|
39 | export 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 |
|
49 |
|
50 | export 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 |
|
59 |
|
60 | export declare type StructResult = boolean | Iterable<StructFailure>;
|
61 |
|
62 |
|
63 |
|
64 | export declare type StructType<T extends Struct<any>> = Parameters<T['refiner']>[0];
|
65 |
|
66 |
|
67 |
|
68 | export declare function assert<T>(value: unknown, struct: Struct<T>): asserts value is T;
|
69 |
|
70 |
|
71 |
|
72 | export declare function coerce<T>(value: unknown, struct: Struct<T>): T;
|
73 |
|
74 |
|
75 |
|
76 | export declare function is<T>(value: unknown, struct: Struct<T>): value is T;
|
77 |
|
78 |
|
79 |
|
80 | export declare function validate<T>(value: unknown, struct: Struct<T>, coercing?: boolean): [StructError, undefined] | [undefined, T];
|