@@ -8,41 +8,209 @@ A runtime and compile-time type checker library.
8
8
9
9
10
10
### any
11
+ Creates a validator instance that is _ always_ true.
12
+ This is useful for specifying that an object _ must_ have a given parameter, but you don't care what type that parameter is.
13
+ ###### Example:
14
+ ``` typescript
15
+ import * as v from ' validtyped' ;
16
+
17
+ const data: any = getData ();
18
+
19
+ const validator = v .object ({ a: v .string (), b: v .any () });
20
+
21
+ if (validator .isValid (data )) doThing (data ); // typeof data => `{ a: string, b: any }`
22
+ else throw new Error (' oops!' ); // typeof data => `any`
23
+ ```
11
24
12
25
### array
26
+ Creates a ` Validator ` instance that matches on arrays of the given type.
27
+
28
+ | Param | Description |
29
+ | --- | --- |
30
+ | v | A ` Validator ` instance that specifies the shape of the array elements. |
31
+ ###### Example:
32
+ ``` typescript
33
+ import * as v from ' validtyped' ;
34
+
35
+ const validator = v .array ( v .number () );
36
+ const data: any = getData ();
37
+
38
+ if (validator .isValid (data )) doThing (data ); // typeof data = `number[]`;
39
+ else throw new Error (' oops!' ); // typeof data = `any`
40
+ ```
13
41
14
42
### boolean
43
+ Creates a validator instance that is true when the given type is a boolean.
44
+ ###### Example:
45
+ ``` typescript
46
+ import * as v from ' validtyped' ;
47
+
48
+ const validator = v .boolean ();
49
+ const data: any = getData ();
50
+
51
+ if (validator .isValid (data )) doThing (data ); // typeof data => `boolean`
52
+ else throw new Error (' oops!' ); // typeof data => `any`
53
+ ```
15
54
16
55
### intersect
56
+ Creates a ` Validator ` instance that matches on _ both_ of the given possible types.
57
+
58
+ | Param | Description |
59
+ | --- | --- |
60
+ | v1 | A ` Validator ` instance that the specified type must match. |
61
+ | v2 | A ` Validator ` instance that the specified type must match. |
62
+ ###### Example:
63
+ ``` typescript
64
+ import * as v from ' validtyped' ;
65
+
66
+ const validator = v .intersect ( v .object ({ a: v .string () }), v .object ({ b: v .number () }) );
67
+ const data: any = getData ();
68
+
69
+ if (validator .isValid (data )) doThing (data ); // typeof data = `{ a: string, b: number }`
70
+ else throw new Error (' oops!' ); // typeof data = `any`
71
+ ```
17
72
18
73
### nominal
74
+ Creates a validator instance that passes type checking through to the given validator.
75
+ Operates as an identity function for the runtime ` Validator ` type, but annotates the compile-time encapsulated ` Validator<T> `
76
+ with a nominal string.
77
+
78
+ This is useful for marking ` id ` s or other fields as being a _ specific_ type that should not be edited.
79
+
80
+ | Param | Description |
81
+ | --- | --- |
82
+ | v | The validator instance that will be passed through for checking runtime correctness. |
83
+ | s | The nominal type tag for compile-time types. |
84
+ ###### Example:
85
+ ``` typescript
86
+ import * as v from ' validtyped' ;
87
+
88
+ const validator = v .nominal (v .string (), ' id' );
89
+ const data: any = getData ();
90
+
91
+ if (validator .isValid (data )) doThing (data ); // typeof data => `Nominal<string, 'id'>`
92
+ else throw new Error (' oops!' ); // typeof data => `any`
93
+
94
+ const id = ' user-id' as Nominal <string , ' id' >;
95
+ const newId = id + ' -new' ; // typeof data => `string`. Since we've modified this type, it can no longer be an `id`.
96
+ ```
19
97
20
98
### number
99
+ Creates a validator instance that is true when the given type is a number.
100
+ ###### Example:
101
+ ``` typescript
102
+ import * as v from ' validtyped' ;
103
+
104
+ const validator = v .number ();
105
+ const data: any = getData ();
106
+
107
+ if (validator .isValid (data )) doThing (data ); // typeof data => `number`
108
+ else throw new Error (' oops!' ); // typeof data => `any`
109
+ ```
21
110
22
111
### object
112
+ Creates a validator instance that validates the given data is an object, and every property of that object matches the given schemas.
113
+ By default, all listed properties ** are required** .
114
+ By default, unlisted properties are also allowed.
115
+
116
+ | Param | Description |
117
+ | --- | --- |
118
+ | o | An object whose keys will be required keys of the valid type and whose properties are ` Validator ` instances matching the valid property's types. |
119
+ | opts | [ optional] An options object |
120
+ | opts.optional | [ optional] A list of keys that should be marked as optional in the valid type. |
121
+ ###### Example:
122
+ ``` typescript
123
+ import * as v from ' validtyped' ;
124
+
125
+ const validator = v .object ({ a: v .string (), b: v .number () });
126
+ const data: any = getData ();
127
+
128
+ if (validator .isValid (data )) doThing (data ); // typeof data = `{ a: string, b: number }`;
129
+ else throw new Error (' oops!' ); // typeof data = `any`
130
+ ```
23
131
24
132
### partial
133
+ Creates a ` Validator ` instance that matches on objects with no required keys, but mandated types for certain keys if they _ do_ exist.
134
+
135
+ | Param | Description |
136
+ | --- | --- |
137
+ | v | A ` Validator ` instance that specifies an object type. |
138
+ ###### Example:
139
+ ``` typescript
140
+ import * as v from ' validtyped' ;
141
+
142
+ const validator = v .partial ( v .object ({
143
+ a: v .string (),
144
+ b: v .number (),
145
+ }));
146
+ const data: any = getData ();
147
+
148
+ if (validator .isValid (data )) doThing (data ); // typeof data = `{ a?: string, b?: number }`;
149
+ else throw new Error (' oops!' ); // typeof data = `any`
150
+ ```
25
151
26
152
### record
153
+ Creates a ` Validator ` instance that matches on objects with _ any_ keys, and whose properties match the given ` Validator ` .
154
+
155
+ This is useful for "dictionary"-like objects, for instance a record of users by userId.
156
+
157
+ | Param | Description |
158
+ | --- | --- |
159
+ | types | A ` Validator ` instance that specifies the right-side types of the record. |
160
+ ###### Example:
161
+ ``` typescript
162
+ import * as v from ' validtyped' ;
163
+
164
+ const validator = v .record ( v .number () );
165
+ const data: any = getData ();
166
+
167
+ if (validator .isValid (data )) doThing (data ); // typeof data = `Record<string, number>`;
168
+ else throw new Error (' oops!' ); // typeof data = `any`
169
+ ```
27
170
28
171
### string
29
172
Creates a validator instance that is true when the given type is a string.
30
173
31
174
| Param | Description |
32
175
| --- | --- |
33
176
| union | [ optional] an array of possible string literals that the valid data could take. |
177
+ ###### Example:
178
+ ``` typescript
179
+ import * as v from ' validtyped' ;
180
+
181
+ const validator = v .string ();
182
+ const data: any = getData ();
183
+
184
+ if (validator .isValid (data )) doThing (data ); // typeof data => `string`
185
+ else throw new Error (' oops!' ); // typeof data => `any`
186
+ ```
34
187
35
188
### union
189
+ Creates a ` Validator ` instance that matches on any one of the given list of possible types.
190
+
191
+ | Param | Description |
192
+ | --- | --- |
193
+ | v | A list of ` Validator ` instances that specify the possible types that the valid type could take. |
194
+ ###### Example:
195
+ ``` typescript
196
+ import * as v from ' validtyped' ;
197
+
198
+ const validator = v .union ([ v .string (), v .number () ]);
199
+ const data: any = getData ();
200
+
201
+ if (validator .isValid (data )) doThing (data ); // typeof data = `number | string`
202
+ else throw new Error (' oops!' ); // typeof data = `any`
203
+ ```
36
204
37
205
### Validator
38
- A ` Valdiator <T>` instance is an encapsulated pair of some TS type ` T ` and a corresponding JSON schema.
206
+ A ` Validator <T>` instance is an encapsulated pair of some TS type ` T ` and a corresponding JSON schema.
39
207
40
208
A ` Validator ` already knows how to map from ` T ` to its json schema: using a json schema validator.
41
209
Additionally, a ` Validator ` can perform simple algebraic operations with other ` Validator ` s resulting in more complex validatable types.
42
210
43
211
A ` Validator ` will always maintain a valid json schema representation of the type, ` T ` , that it encapsulates.
44
212
The underlying json schema can always be accessed with ` getSchema() ` .
45
- #### Example
213
+ ###### Example:
46
214
``` typescript
47
215
const stringValidator = new Validator <string >({ type: ' string' });
48
216
const numberValidator = new Validator <number >({ type: ' number' });
@@ -55,8 +223,8 @@ Creates a new validator that is true whenever the data matches `this` _and_ `v`.
55
223
56
224
| Param | Description |
57
225
| --- | --- |
58
- | v | Another validator instance whose type will form an intersection with ` this ` encapsulated type. |
59
- #### Example
226
+ | other | Another validator instance whose type will form an intersection with ` this ` encapsulated type. |
227
+ ###### Example:
60
228
``` typescript
61
229
import * as v from ' validtyped' ;
62
230
@@ -69,6 +237,13 @@ const and = v1.and(v2).and(v3); // { a: string, b: number, c: boolean }
69
237
70
238
### Validator.getSchema
71
239
Returns the underlying JSON schema.
240
+ ###### Example:
241
+ ``` typescript
242
+ import * as v from ' validtyped' ;
243
+
244
+ const schema = v .string ().getSchema ();
245
+ console .log (schema ); // { type: 'string' }
246
+ ```
72
247
73
248
### Validator.isValid
74
249
Predicate returning ` true ` if the given data matches the JSON schema.
@@ -77,13 +252,32 @@ Acts as a type guard for the encapsulated typescript type.
77
252
| Param | Description |
78
253
| --- | --- |
79
254
| thing | Any data of unknown type which will be validated. |
255
+ ###### Example:
256
+ ``` typescript
257
+ import * as v from ' validtyped' ;
258
+
259
+ const userIdModel = v .nominal (v .string (), ' userId' );
260
+ const userModel = v .object ({
261
+ name: v .string (),
262
+ id: userIdModel ,
263
+ });
264
+
265
+ const x: any = getUserData ();
266
+ if (userModel .isValid (x )) doThing (x );
267
+ ```
80
268
81
269
### Validator.or
82
270
Creates a new validator that is true whenever the data matches ` this ` _ or_ ` v ` .
83
271
84
272
| Param | Description |
85
273
| --- | --- |
86
- | v | Another validator instance whose type will form a union with ` this ` encapsulated type. |
274
+ | other | Another validator instance whose type will form a union with ` this ` encapsulated type. |
275
+ ###### Example:
276
+ ``` typescript
277
+ import * as v from ' validtyped' ;
278
+
279
+ const or = v .string ().or (v .number ()).or (v .boolean ()); // string | number | boolean
280
+ ```
87
281
88
282
### Validator.setSchemaMetaData
89
283
Add meta-data to the underlying JSON schema.
@@ -93,6 +287,13 @@ but supplies the `getSchema` method to have a complete JSON schema with meta-dat
93
287
| Param | Description |
94
288
| --- | --- |
95
289
| meta | JSON schema meta-data (name, description, etc.) |
290
+ ###### Example:
291
+ ``` typescript
292
+ import * as v from ' validtyped' ;
293
+
294
+ const validator = v .string ();
295
+ validator .setSchemaMetaData ({ name: ' string validator' });
296
+ ```
96
297
97
298
### Validator.validate
98
299
Takes data of unknown type and returns a discriminated union with either the data as the valid type,
@@ -101,6 +302,25 @@ or an error object describing what part of the data did not match.
101
302
| Param | Description |
102
303
| --- | --- |
103
304
| data | Any data of unknown type which will be validated. |
305
+ ###### Example:
306
+ ``` typescript
307
+ import * as v from ' validtyped' ;
308
+
309
+ const stringModel = v .string ();
310
+
311
+ const result = stringModel .validate (22 );
312
+ if (result .valid ) doThing (result .data );
313
+ else logger .error (... result .errors );
314
+ ```
104
315
105
316
### ValidType
317
+ Returns the encapsulated type of a ` Validator ` type.
318
+ ###### Example:
319
+ ``` typescript
320
+ import * as v from ' validtyped' ;
321
+
322
+ const validator = v .string ();
323
+
324
+ type x = v .ValidType <typeof validator >; // `string`
325
+ ```
106
326
0 commit comments