-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.js
57 lines (47 loc) · 1.42 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
50
51
52
53
54
55
56
57
const Ajv = require('ajv');
const ajv = new Ajv();
ajv.addKeyword('booleanstr', {
type: 'string',
compile: function (schema) {
return function (data) {
return data == true || data == false;
};
}
})
const Validator = function () {
this.datatypes = {};
this.validated = true;
}
Validator.prototype.validate = function (input) {
Object.keys(this.datatypes).forEach((ele) => {
this.validated = ajv.validate(this.datatypes[ele], input[ele]);
});
}
Validator.prototype.addDataType = function (key, validators) {
this.datatypes[key] = validators;
}
Validator.prototype.convertType2Validator = function (datatype) {
const mType = [];
const datatypeVal = { 'type': mType };
if (datatype.includes('INT')) mType.push('integer');
else if (datatype.includes('VARCHAR')) mType.push('string');
else if (datatype.includes('DOUBLE')) mType.push('number');
else if (datatype.includes('BOOLEAN')) {
datatypeVal['booleanstr'] = null;
mType.push('string');
}
return datatypeVal;
}
Validator.prototype.addNull = function (datatypes) {
datatypes['type'].push('null');
}
Validator.prototype.isValid = function () {
return this.validated;
}
Validator.prototype.getDataTypes = function () {
return this.datatypes;
}
Validator.prototype.setDataTypes = function (datatypes) {
this.datatypes = datatypes;
}
module.exports = { Validator, };