forked from aditosoftware/nodepki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.js
49 lines (36 loc) · 1.09 KB
/
validator.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
/**
* Validator for API inputs
* Utilizes AJV
*/
var log = require('fancy-log');
var Ajv = require('ajv');
var ajv = Ajv({allErrors: true});
var validator = {};
validator.checkAPI = function(schema, data) {
var valid = ajv.validate(schema, data);
if(valid) {
return { success: true };
} else {
var errors = [];
ajv.errors.forEach(function(error) {
var message = '';
switch(error.keyword) {
case 'required':
// requirement not fulfilled.
message = 'Property \'' + error.params.missingProperty + '\' is missing.';
break;
case 'type':
message = 'Wrong type: ' + error.dataPath + ' ' + error.message;
break;
default:
message = 'Unknown input error. :(';
}
var pusherror = {
message: message
}
errors.push(pusherror);
});
return { success: false, errors: errors };
}
};
module.exports = validator;