-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
100 lines (95 loc) · 3.95 KB
/
validate.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const validator = require("validator")
const pointTemplate = {
required : {
"category" : {
validate: (val) => typeof val === 'string' && validator.isUppercase(val),
error: "- category is not an uppercase string"
},
"email" : {
validate: (val) => typeof val === 'string' && validator.isEmail(val),
error: "- email is not a proper email"
},
"description" : {
validate: (val) => typeof val === 'string',
error: "- description is not a string"
},
"latitude" : {
validate: (val) => typeof val === 'number',
error: "- latitude is not a number"
},
"longitude" : {
validate: (val) => typeof val === 'number',
error: "- longitude is not a number"
}
},
optional : {
"img" : {
validate: (val) => typeof val === 'string' && validator.isURL(val),
error: "- img is not a proper url"
}
}
}
let validateData = function(body, template){
let required = template.required
let optional = template.optional
// check that the length of the body is within the required and optional lengths
let valid = Object.keys(body).length >= Object.keys(required).length &&
Object.keys(body).length <= Object.keys(required).length + Object.keys(optional).length;
if (valid){
// check that there are no extra body fields that shouldn't be there and they're valid
Object.keys(body).every((key,index) => {
let val = body[key]
valid = valid && ((key in required && required[key].validate(val)) || (key in optional && optional[key].validate(val)))
return valid
});
}
if (valid){
// check that all of the required fields are there
Object.keys(required).every((key,index) => {
valid = valid && key in body
return valid
});
}
return valid
}
let whyInvalidData = function(body, template){
let required = template.required
let optional = template.optional
let error = ["Required: " + Object.keys(required).join(", "),
"Optional: " + Object.keys(optional).join(", ")]
// check that the length of the body is within the required and optional lengths
let valid = Object.keys(body).length >= Object.keys(required).length &&
Object.keys(body).length <= Object.keys(required).length + Object.keys(optional).length;
if (valid){
// check that there are no extra body fields that shouldn't be there and they're valid
Object.keys(body).forEach((key,index) => {
let val = body[key]
if(key in required){
if(!required[key].validate(val)){
error.push(required[key].error)
}
}else if(key in optional){
if(!optional[key].validate(val)){
error.push(optional[key].error)
}
}else{
error.push("- " + key + " does not belong.")
}
});
// check that all of the required fields are there
Object.keys(required).every((key,index) => {
valid = valid && key in body
return valid
});
if(!valid){
error.push("- not all required parameters exist.")
}
}else{
error.push(Object.keys(body).length < Object.keys(required).length ?
"- not enough parameters.":
"- too many parameters.")
}
return error.join("\n")
}
module.exports.validatePoint = (body) => validateData(body, pointTemplate)
module.exports.whyInvalidPoint = (body) => whyInvalidData(body, pointTemplate)