-
Notifications
You must be signed in to change notification settings - Fork 1
/
write-rules.js
44 lines (38 loc) · 1.05 KB
/
write-rules.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
const { boolTypes, numberTypes, dateTypes } = require('./constants');
module.exports = function(info, tab) {
let content = `${tab}static rules() {
${tab}${tab}return {
`;
Object.keys(info).forEach(col => {
content += `${tab.repeat(3)}${col}: ${getJoiRule(info[col])}
`;
});
content = content.slice(0, -2);
content += '\n';
content += `${tab.repeat(2)}};
${tab}}
`;
return content;
};
function getJoiRule(info) {
let rule;
if (boolTypes.indexOf(info.type) > -1 && info.length === 1) {
rule = 'joi.boolean()';
} else if (numberTypes.indexOf(info.type) > -1) {
rule = 'joi.number()';
if (info.type.indexOf('int') > -1) {
rule += '.integer()';
}
} else if (dateTypes.indexOf(info.type) > -1) {
rule = 'joi.date()';
} else {
rule = 'joi.string()';
if (info.length && !Number.isNaN(info.length)) {
rule += `.max(${info.length})`;
}
}
if (info.nullable) {
rule += '.allow(null)';
}
return rule += ',';
}