|
1 |
| -//import ElValidator from 'elvalidator'; |
2 |
| -import ElValidator from './index.js'; |
3 |
| - |
4 |
| -// Set up the validator object |
5 |
| -let validator = new ElValidator({ |
6 |
| - name : { type: String, required:true, trim:true, minLength:3 }, |
7 |
| - age : { type: Number, required:true, integer:true, min:18, max:100 }, |
8 |
| - agreedTelemetry : { type: Boolean, default:false }, |
9 |
| - |
10 |
| - // Array example: |
11 |
| - tags : [ |
12 |
| - { type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ } |
13 |
| - ], |
14 |
| - |
15 |
| - // Object example: |
16 |
| - settings : { |
17 |
| - darkMode : { type: Boolean, required: true, }, |
18 |
| - codeEditor : { type: String, default:'atom', enum:['atom', 'vstudio', 'notepad++'] }, |
19 |
| - extras : { type: Object, required:true, } |
20 |
| - }, |
21 |
| - stringOrBool : { |
22 |
| - $or : [ |
23 |
| - { type: String }, |
24 |
| - { type: Boolean }, |
25 |
| - ] |
26 |
| - }, |
27 |
| - arrayWithOptions : { |
28 |
| - type : [ |
29 |
| - { type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ } |
30 |
| - ], |
31 |
| - minEntries : 0, |
32 |
| - maxEntries : 20, |
33 |
| - uniqueValues : true, |
34 |
| - }, |
35 |
| - custom : { type: String, validator: async (value) => { |
36 |
| - if(value.length%2 == 1) |
37 |
| - throw new Error('The number of characters must be odd.'); |
38 |
| - return '->'+value; |
39 |
| - }}, |
40 |
| -}, { |
41 |
| - strictMode : true, |
42 |
| - throwUnkownFields : false, |
43 |
| - accumulateErrors : false, |
44 |
| -}); |
45 |
| - |
46 |
| -// Validate/Sanitize a data object |
47 |
| -var sanitizedData = await validator.validate({ |
48 |
| - name : 'Yassine', |
49 |
| - age : 27, |
50 |
| - |
51 |
| - tags : ['programmer', 'javascript'], |
52 |
| - settings : { |
53 |
| - darkMode : false, |
54 |
| - //codeEditor : 'atom', |
55 |
| - extras : { |
56 |
| - one : '1', |
57 |
| - two : 'anything', |
58 |
| - } |
59 |
| - }, |
60 |
| - other : 'unknown field', |
61 |
| - stringOrBool : false, |
62 |
| - arrayWithOptions : ['hello', 'world', 'tag', 'WillDownCase', 'TAG', 'last'], |
63 |
| - custom : 'node' |
64 |
| -}); |
65 |
| - |
66 |
| -console.log(sanitizedData); |
67 |
| - |
68 |
| - |
69 |
| - |
70 |
| -/* |
71 |
| -OUTPUT: |
72 |
| -
|
73 |
| -{ |
74 |
| - name: 'Yassine', |
75 |
| - age: 27, |
76 |
| - agreedTelemetry: false, |
77 |
| - tags: [ 'programmer', 'javascript' ], |
78 |
| - settings: { |
79 |
| - darkMode: false, |
80 |
| - codeEditor: 'atom', |
81 |
| - extras: { one: '1', two: 'anything' } |
82 |
| - }, |
83 |
| - stringOrBool: false, |
84 |
| - arrayWithOptions: [ 'hello', 'world', 'tag', 'willdowncase', 'last' ], |
85 |
| - custom: '->node' |
86 |
| -} |
87 |
| -
|
88 |
| -*/ |
| 1 | +//import ElValidator from 'elvalidator'; |
| 2 | +import ElValidator from './index.js'; |
| 3 | + |
| 4 | +// Set up the validator object |
| 5 | +let validator = new ElValidator({ |
| 6 | + name : { type: String, required:true, trim:true, minLength:3 }, |
| 7 | + age : { type: Number, required:true, integer:true, min:18, max:100 }, |
| 8 | + agreedTelemetry : { type: Boolean, default:false }, |
| 9 | + |
| 10 | + // Array example: |
| 11 | + tags : [ |
| 12 | + { type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ } |
| 13 | + ], |
| 14 | + |
| 15 | + // Object example: |
| 16 | + settings : { |
| 17 | + darkMode : { type: Boolean, required: true, }, |
| 18 | + codeEditor : { type: String, default:'atom', enum:['atom', 'vstudio', 'notepad++'] }, |
| 19 | + extras : { type: Object, required:true, } |
| 20 | + }, |
| 21 | + stringOrBool : { |
| 22 | + $or : [ |
| 23 | + { type: String }, |
| 24 | + { type: Boolean }, |
| 25 | + ] |
| 26 | + }, |
| 27 | + arrayWithOptions : { |
| 28 | + type : [ |
| 29 | + { type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ } |
| 30 | + ], |
| 31 | + minEntries : 0, |
| 32 | + maxEntries : 20, |
| 33 | + uniqueValues : true, |
| 34 | + }, |
| 35 | + custom : { type: String, validator: async (value) => { |
| 36 | + if(value.length%2 == 1) |
| 37 | + throw new Error('The number of characters must be odd.'); |
| 38 | + return '->'+value; |
| 39 | + }}, |
| 40 | +}, { |
| 41 | + strictMode : true, |
| 42 | + throwUnkownFields : false, |
| 43 | + accumulateErrors : false, |
| 44 | +}); |
| 45 | + |
| 46 | +// Validate/Sanitize a data object |
| 47 | +var sanitizedData = await validator.validate({ |
| 48 | + name : 'Yassine', |
| 49 | + age : 27, |
| 50 | + |
| 51 | + tags : ['programmer', 'javascript'], |
| 52 | + settings : { |
| 53 | + darkMode : false, |
| 54 | + //codeEditor : 'atom', |
| 55 | + extras : { |
| 56 | + one : '1', |
| 57 | + two : 'anything', |
| 58 | + } |
| 59 | + }, |
| 60 | + other : 'unknown field', |
| 61 | + stringOrBool : false, |
| 62 | + arrayWithOptions : ['hello', 'world', 'tag', 'WillDownCase', 'TAG', 'last'], |
| 63 | + custom : 'node' |
| 64 | +}); |
| 65 | + |
| 66 | +console.log(sanitizedData); |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +/* |
| 71 | +OUTPUT: |
| 72 | +
|
| 73 | +{ |
| 74 | + name: 'Yassine', |
| 75 | + age: 27, |
| 76 | + agreedTelemetry: false, |
| 77 | + tags: [ 'programmer', 'javascript' ], |
| 78 | + settings: { |
| 79 | + darkMode: false, |
| 80 | + codeEditor: 'atom', |
| 81 | + extras: { one: '1', two: 'anything' } |
| 82 | + }, |
| 83 | + stringOrBool: false, |
| 84 | + arrayWithOptions: [ 'hello', 'world', 'tag', 'willdowncase', 'last' ], |
| 85 | + custom: '->node' |
| 86 | +} |
| 87 | +
|
| 88 | +*/ |
0 commit comments