-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.js
76 lines (70 loc) · 1.48 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const {
validatorFactory,
validators: {
values: { isString, isBoolean, isNumber },
general: { nullable },
},
} = require('./');
const example = async () => {
const userCreateValidation = validatorFactory({
check: {
id: [isString].map(nullable),
firstName: [isString],
lastName: [isString],
age: [isNumber].map(nullable),
admin: [isBoolean],
},
});
const validUser = {
firstName: 'John',
lastName: 'McValiduser',
admin: true,
};
const invalidUser = {
firstName: 'Invalid',
lastName: 'Invalidface',
admin: 'false',
age: 'a literal ferry',
};
try {
const res = await userCreateValidation(validUser, {});
console.log(JSON.stringify(res, null, 2));
/*
{
"firstName": "John",
"lastName": "McValiduser",
"admin": true
}
*/
} catch (ex) {
console.log(JSON.stringify(ex, null, 2));
}
try {
const res = await userCreateValidation(invalidUser, {});
console.log(JSON.stringify(res, null, 2));
} catch (ex) {
console.log(JSON.stringify(ex, null, 2));
/*
{
"name": "ValidationError",
"data": {
"age": [
{
"expected": "a number",
"received": "not a number",
"key": "age"
}
],
"admin": [
{
"expected": "to be a bool",
"received": "false",
"key": "admin"
}
]
}
}
*/
}
};
example();