This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
formSchemas.js
executable file
·128 lines (119 loc) · 2.66 KB
/
formSchemas.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const API = require("./api");
const isValidDate = require("./utils/").isValidDate;
const currencySchema = (errorMessageString = "errors.currency") => {
return {
isCurrency: {
errorMessage: errorMessageString,
options: { allow_negatives: false }
}
};
};
const yesNoSchema = (errorMessageString = "errors.yesNo") => {
return {
isIn: {
errorMessage: errorMessageString,
options: [["Yes", "No"]]
}
};
};
/**
* Runs an array of validators over a value
* this is a workaround I found to allow multiple custom validators
* but still maintain individual error messages
*/
const validationArray = validators => {
let errors = [];
return {
custom: {
errorMessage: () => {
return errors.length ? errors[0] : "value is invalid";
},
options: (value, { req }, opts) => {
errors = [];
const results = validators.map(validator => {
const result = validator.validate(value, req, opts);
// If validation failed set current Error
if (result === false) {
const errorMessage =
validator.errorMessage || `${value} is invalid`;
errors.push(errorMessage);
}
return result;
});
return results.every(value => value === true);
}
}
};
};
const loginSchema = {
code: {
isLength: {
errorMessage: "errors.login.length",
options: { min: 9, max: 9 }
},
isAlphanumeric: {
errorMessage: "errors.login.alphanumeric"
},
customSanitizer: {
options: value => {
return value ? value.toUpperCase() : value;
}
}
}
};
const emailSchema = {
email: {
isLength: {
errorMessage: "errors.email.length",
options: { min: 3, max: 200 }
}
}
};
const nameSchema = {
fullname: {
isLength: {
errorMessage: "errors.fullname.length",
options: { min: 3, max: 200 }
}
},
email: {
isLength: {
errorMessage: "errors.email.length",
options: { min: 3, max: 200 }
}
},
expiry: {
customSanitizer: {
options: value => {
//We want to remove any spaces, dash or underscores
return value ? value.replace(/[_]*/g, "") : value;
}
},
custom: {
options: (value, { req }) => {
return isValidDate(value);
},
errorMessage: "errors.expiry.date"
}
},
confirm: yesNoSchema()
};
const reviewSchema = {
review: {
isIn: {
errorMessage: "errors.review",
options: [["review"]]
}
}
};
const authSchema = {
auth: currencySchema()
};
module.exports = {
emailSchema,
loginSchema,
currencySchema,
reviewSchema,
authSchema,
nameSchema
};