Skip to content

Commit 72cde3c

Browse files
committed
First release of ElValidator
1 parent 144d75f commit 72cde3c

8 files changed

+814
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
node_modules
3+
4+
package-lock.json
5+
yarn.lock

elvalidator.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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+
*/

example2.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
// Without ElValidator
4+
5+
if(!Object.prototype.hasOwnProperty.call(req.body, 'name'))
6+
throw new Error('The "Name" field is required.');
7+
if(typeof req.body.name!=='string')
8+
throw new Error('The "Name" field must be a text field.');
9+
if(req.body.name.length<3)
10+
throw new Error('The "Name" field must have at least 3 characters.');
11+
12+
if(!Object.prototype.hasOwnProperty.call(req.body, 'tags'))
13+
throw new Error('The "Tags" field is required.');
14+
if(!Array.isArray(req.body.tags))
15+
throw new Error('The "Tags" field must be an array.');
16+
if(req.body.tags.some(tag=> {
17+
return typeof tag!='string' || tag.length<2 || !tag.match(/[a-z0-9_]+/);
18+
}))
19+
throw new Error('The "Tags" field has invalid value(s).');
20+
21+
req.body.tags = req.body.tags.map(tag=>tag.toLowerCase());
22+
23+
24+
25+
// With ElValidator
26+
let sanitized = await ElValidator.validate(req.body, {
27+
name : { type: String, required: true, minlength:3 },
28+
tags : [
29+
{ type: String, required:true, lowercase:true, minlength:2, match:/[a-z0-9_]+/ }
30+
],
31+
})

0 commit comments

Comments
 (0)