UNPKG

3.88 kBTypeScriptView Raw
1import { Failure, Branch, Path } from './struct-error';
2import { Superstruct } from './superstruct';
3/**
4 * A symbol to set on `Struct` objects to test them against later.
5 */
6export declare const STRUCT: unique symbol;
7/**
8 * Check if a value is a `Struct` object.
9 */
10export declare const isStruct: (value: any) => value is Struct;
11/**
12 * This abstract `Struct` factory creates a generic struct that validates values
13 * against a `Validator` function.
14 */
15export declare const createStruct: (props: {
16 kind: string;
17 type: string;
18 defaults: () => any;
19 struct: Superstruct;
20}) => Struct;
21/**
22 * `Struct` validators encapsulate the validation logic for a specific type of
23 * data (either custom or built-in). They have a set of methods that allow you
24 * to validate input in various ways, while producing detailed errors.
25 *
26 * They are created by the [[Superstruct]] factory functions. You can call them
27 * directly for the simple case, or use one of their validation methods.
28 *
29 * ```js
30 * const Struct = struct({
31 * id: 'number',
32 * name: 'string',
33 * })
34 *
35 * const result = Struct(data) // Throws if invalid!
36 *
37 * const [error, result] = Struct.validate(data)
38 *
39 * if (Struct.test(data)) {
40 * // ...
41 * }
42 * ```
43 */
44export interface Struct {
45 /**
46 * All structs are functions that are shorthand for calling [[Struct.assert]].
47 */
48 (value: any): any;
49 /**
50 * The struct's name.
51 *
52 * ```js
53 * 'object'
54 * 'union'
55 * 'email'
56 * ```
57 */
58 kind: string;
59 /**
60 * A string representing the type of the struct. These strings are purely for
61 * user-facing error messages, and aren't canonical. They are similar to the
62 * syntax that TypeScript uses.
63 *
64 * ```js
65 * '{id,name,email}'
66 * 'string | number'
67 * 'email'
68 * ```
69 */
70 type: string;
71 /**
72 * Get the default value for a struct.
73 *
74 * ```js
75 * const defaults = Struct.default()
76 * ```
77 */
78 default(): any;
79 /**
80 * Run the low-level validation function a struct, returning a tuple that
81 * contains either a list of [[Failure]] objects, or a resulting value.
82 *
83 * This method is fairly low-level and not for normal use.
84 *
85 * ```js
86 * const [failures, result] = Struct.check(value, branch, path)
87 * ```
88 */
89 check(value: any, branch: Branch, path: Path): [Failure[]?, any?];
90 /**
91 * Validate a `value`, returning the resulting value, and throwing an error if
92 * validation fails.
93 *
94 * ```js
95 * try {
96 * const result = Struct.assert(value)
97 * // ...
98 * } catch (e) {
99 * // ...
100 * }
101 * ```
102 */
103 assert(value: any): any;
104 /**
105 * Validate a `value`, returning a boolean indicating whether it's valid.
106 *
107 * Note: Using this method does not give you access to the defaults that may
108 * be associated with a struct, so it doesn't guarantee that the value you
109 * have passes, just that the value with defaults passes.
110 *
111 * ```js
112 * if (Struct.test(value)) {
113 * // ...
114 * }
115 * ```
116 */
117 test(value: any): boolean;
118 /**
119 * Validate a `value` returning a tuple containing an error if the validation
120 * fails, or the resulting value if it succeeds.
121 *
122 * ```js
123 * const [error, result] = Struct.validate(value)
124 * ```
125 */
126 validate(value: any): [Error?, any?];
127 /**
128 * Create a low-level [[Failure]] object for the struct.
129 *
130 * ```js
131 * const failure = Struct.fail({ value, branch, path })
132 * ```
133 */
134 fail(obj: {
135 value: any;
136 branch: Branch;
137 path: Path;
138 type?: string;
139 }): Failure;
140}