-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
45 lines (35 loc) · 1.05 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
function parseField(field) {
return field
.split(/\[|\]/)
.filter((s) => s);
}
function getField(ctx, field) { //基于parseField()的结果查找属性
let val = ctx.body;
field.forEach((prop) => {
val = val[prop];
});
return val;
}
exports.required = (field) => {
field = parseField(field); //解析输入域一次
return (ctx, next) => {
if (getField(ctx, field)) { //每次收到请求都检查输入域是否有值
next(); //如果有,则进入下一个中间件
} else {
ctx.error(`${field.join(' ')} is required`); //如果没有,显示错误
ctx.redirect('back');
}
};
};
exports.lengthAbove = (field, len) => {
field = parseField(field);
return (ctx, next) => {
if (getField(ctx, field).length > len) {
next();
} else {
const fields = field.join(' ');
ctx.error(`${fields} must have more than ${len} characters`);
ctx.redirect('back');
}
}
}