-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.js
More file actions
31 lines (23 loc) · 1 KB
/
example2.js
File metadata and controls
31 lines (23 loc) · 1 KB
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
// Without ElValidator
if(!Object.prototype.hasOwnProperty.call(req.body, 'name'))
throw new Error('The "Name" field is required.');
if(typeof req.body.name!=='string')
throw new Error('The "Name" field must be a text field.');
if(req.body.name.length<3)
throw new Error('The "Name" field must have at least 3 characters.');
if(!Object.prototype.hasOwnProperty.call(req.body, 'tags'))
throw new Error('The "Tags" field is required.');
if(!Array.isArray(req.body.tags))
throw new Error('The "Tags" field must be an array.');
if(req.body.tags.some(tag=> {
return typeof tag!='string' || tag.length<2 || !tag.match(/[a-z0-9_]+/);
}))
throw new Error('The "Tags" field has invalid value(s).');
req.body.tags = req.body.tags.map(tag=>tag.toLowerCase());
// With ElValidator
let sanitized = await ElValidator.validate(req.body, {
name : { type: String, required: true, minLength:3 },
tags : [
{ type: String, required:true, lowercase:true, minLength:2, match:/[a-z0-9_]+/ }
],
})