You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library provides a number of strongly-typed assertions which can be combined to validate the type of each property.
85
61
86
-
An assertion may take another assertion as its last argument; if assertion check passes, it calls the next assertion. For example, `isString(minLength(1, maxLength(10)))` first checks if the value is a string, then checks if its length is at least 1, and then checks that its length is no more than 10. If `isString` fails, `minLength` isn't run. Chaining assertions in this way allows for complex values to be validated.
87
-
88
-
Some assertions require other assertions to come before it. For example, `minLength` can't be used by itself because it needs another assertion to check that the value has the `length` property - so something like `isString(minLength(1))` or `isArray(minLength(0))`.
89
-
90
-
## Handling Validation Errors ##
91
-
92
-
Errors will always be of the type `ValidationError`, which has a number of useful properties:
93
-
94
-
-`errorCode`: A string which is one of a set of error codes, e.g. `NOT_STRING`. Useful for producing custom error messages or triggering certain error logic.
95
-
-`message`: A human-readable error message, with more information as to why the validation failed
96
-
-`path`: An array of objects that describe the path to the value that caused the validation to fail. Each object is either an `ArrayIndexPathNode` (which has an `index` property) or `KeyPathNode` (which has a `key` property).
97
-
98
-
There's a `toString` method which prints this information in a human-readable format.
99
-
100
-
## Documentation ##
62
+
An assertion may take another assertion as its last argument; if assertion check passes, it calls the next assertion. For example, `isString(minLength(1, maxLength(10)))` first checks if the value is a string, then checks if its length is at least 1, and then checks that its length is no more than 10. If `isString` fails, `minLength` isn't run. Chaining assertions in this way allows for complex validation.
63
+
64
+
Some assertions require other assertions to come before it. For example, `minLength` can't be used by itself because it needs another assertion to check that the value has the `length` property - so something like `isString(minLength(1))` or `isArray(minLength(1))`.
Checks that `arg` conforms to the type `T` using the given `validator`. Returns an object that conforms to `T` or throws an error.
164
152
165
-
### assertThat ###
166
-
Used to start an assertion chain.
167
-
168
153
### optional ###
169
154
Used when the property may not present on the object, or its value is undefined. Example:
170
155
@@ -174,7 +159,7 @@ interface IFoo {
174
159
}
175
160
176
161
const fooValidator:Validator<IFoo> = {
177
-
bar: assertThat('bar', optional(isString())),
162
+
bar: optional(isString()),
178
163
};
179
164
180
165
// Both of these are acceptable
@@ -202,7 +187,7 @@ interface IFoo {
202
187
}
203
188
204
189
const fooValidator:Validator<IFoo> = {
205
-
bar: assertThat('bar', nullable(isString())),
190
+
bar: nullable(isString()),
206
191
};
207
192
```
208
193
@@ -216,7 +201,7 @@ interface IFoo {
216
201
}
217
202
218
203
const fooValidator:Validator<IFoo> = {
219
-
bar: assertThat('bar', defaultsTo('baz', isString())),
204
+
bar: defaultsTo('baz', isString()),
220
205
};
221
206
222
207
const foo =validate({}, fooValidator);
@@ -236,14 +221,15 @@ interface IFoo {
236
221
}
237
222
238
223
const fooValidator:Validator<IFoo> = {
239
-
bar: assertThat('bar', onErrorDefaultsTo('baz', isString())),
224
+
bar: onErrorDefaultsTo('baz', isString()),
240
225
};
241
226
242
227
const foo =validate({bar: 123}, fooValidator);
243
228
244
229
console.log(foo);
245
230
// {bar: 'baz'}
246
231
```
232
+
247
233
### isBoolean ###
248
234
Throws an error if the value is not a boolean.
249
235
@@ -259,7 +245,7 @@ interface IFoo {
259
245
}
260
246
261
247
const fooValidator:Validator<IFoo> = {
262
-
bar: assertThat('bar', isNuber(min(0)))
248
+
bar: isNuber(min(0))
263
249
};
264
250
265
251
// This will throw an error
@@ -281,7 +267,7 @@ interface IFoo {
281
267
}
282
268
283
269
const fooValidator:Validator<IFoo> = {
284
-
bar: assertThat('bar', isStirng(matches(/^[a-z]+$/)))
270
+
bar: isStirng(matches(/^[a-z]+$/))
285
271
};
286
272
287
273
// This will throw an error
@@ -297,7 +283,7 @@ interface IFoo {
297
283
}
298
284
299
285
const fooValidator:Validator<IFoo> = {
300
-
bar: assertThat('bar', isStirng(minLength(1)))
286
+
bar: isStirng(minLength(1))
301
287
};
302
288
303
289
// This will throw an error
@@ -319,7 +305,7 @@ interface IFoo {
319
305
}
320
306
321
307
const fooValidator:Validator<IFoo> = {
322
-
bar: assertThat('bar', isArray())
308
+
bar: isArray()
323
309
};
324
310
325
311
// This is valid
@@ -342,7 +328,7 @@ interface IFoo {
342
328
}
343
329
344
330
const fooValidator:Validator<IFoo> = {
345
-
bar: assertThat('bar', isArray(eachItem(isNumber())))
331
+
bar: isArray(eachItem(isNumber()))
346
332
};
347
333
348
334
// This is valid
@@ -365,7 +351,7 @@ interface IFoo {
365
351
}
366
352
367
353
const fooValidator:Validator<IFoo> = {
368
-
bar: assertThat('bar', isObject())
354
+
bar: isObject()
369
355
};
370
356
371
357
// This is valid, but it wouldn't be safe to access properties on foo.bar
@@ -392,11 +378,11 @@ interface IFoo {
392
378
}
393
379
394
380
const barValidator:Validator<IBar> = {
395
-
baz: assertThat('baz', isNumber())
381
+
baz: isNumber()
396
382
};
397
383
398
384
const fooValidator:Validator<IFoo> = {
399
-
bar: assertThat('bar', conformsTo(barValidator))
385
+
bar: conformsTo(barValidator)
400
386
};
401
387
402
388
// This is valid
@@ -425,11 +411,23 @@ interface IFoo {
425
411
}
426
412
427
413
const fooValidator:Validator<IFoo> = {
428
-
bar: assertThat('bar', equals<Bar>('A', 'B', 'C'))
414
+
bar: equals<Bar>('A', 'B', 'C')
429
415
};
430
416
431
417
// Throws an error
432
418
validate({
433
419
bar: 'D'
434
420
}, fooValidator);
435
421
```
422
+
423
+
### Handling Validation Errors ###
424
+
425
+
Errors will always be of the type `ValidationErrorCollection`, which has a property `error: ValidationError[]`.
426
+
427
+
The `ValidationError` type has a number of useful properties:
428
+
429
+
-`errorCode`: A string which is one of a set of error codes, e.g. `NOT_STRING`. Useful for producing custom error messages or triggering certain error logic.
430
+
-`message`: A human-readable error message, with more information as to why the validation failed
431
+
-`path`: An array of objects that describe the path to the value that caused the validation to fail. Each object is either an `ArrayIndexPathNode` (which has an `index` property) or `KeyPathNode` (which has a `key` property).
432
+
433
+
The `ValidationErrorCollection.toString()` method prints this information in a human-readable format. The name of the root object defaults to `$root`, but this can be changed by passing a string, e.g. `err.toString('this')`.
0 commit comments