Skip to content

Commit d3bacb1

Browse files
author
pedro
committed
feat:更新validator的写法
1 parent 6c6e1e9 commit d3bacb1

9 files changed

Lines changed: 118 additions & 77 deletions

File tree

app/api/cms/admin.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,10 @@ admin.linPost(
217217
throw new Failed({
218218
msg: "新建分组失败"
219219
});
220-
} else {
221-
ctx.success({
222-
msg: "新建分组成功"
223-
});
224220
}
221+
ctx.success({
222+
msg: "新建分组成功"
223+
});
225224
}
226225
);
227226

app/api/cms/log.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const { LinRouter, groupRequired, NotFound } = require("lin-mizar");
44
const { LogFindValidator } = require("../../validators/log");
55
const { PaginateValidator } = require("../../validators/common");
6-
const { get } = require("lodash");
76
const { LogDao } = require("../../dao/log");
87

98
const log = new LinRouter({
@@ -47,7 +46,7 @@ log.linGet(
4746
groupRequired,
4847
async ctx => {
4948
const v = await new LogFindValidator().validate(ctx);
50-
const keyword = get(ctx.request.query, "keyword", "");
49+
const keyword = v.get("query.keyword", false, "");
5150
const { rows, total } = await logDao.searchLogs(v, keyword);
5251
if (total < 1) {
5352
throw new NotFound({

app/api/cms/user.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,15 @@ user.linPut(
101101
v.get("body.old_password"),
102102
v.get("body.new_password")
103103
);
104-
if (ok) {
105-
user.save();
106-
ctx.success({
107-
msg: "密码修改成功"
108-
});
109-
} else {
104+
if (!ok) {
110105
throw new Failed({
111-
msg: "修改密码失败"
106+
msg: "修改密码失败,你可能输入了错误的旧密码"
112107
});
113108
}
109+
user.save();
110+
ctx.success({
111+
msg: "密码修改成功"
112+
});
114113
}
115114
);
116115

app/libs/util.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,18 @@ function getSafeParamId (ctx) {
1313
return id;
1414
}
1515

16-
module.exports = { getSafeParamId };
16+
function isOptional (val) {
17+
// undefined , null , "" , " ", 皆通过
18+
if (val === void 0) {
19+
return true;
20+
}
21+
if (val === null) {
22+
return true;
23+
}
24+
if (typeof val === "string") {
25+
return val === "" || val.trim() === "";
26+
}
27+
return false;
28+
}
29+
30+
module.exports = { getSafeParamId, isOptional };

app/plugins/notify/app/validators.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
const { LinValidator, Rule } = require("lin-mizar");
4-
const { extendedValidator } = require("lin-mizar/lin/extended-validator");
4+
const validator = require("validator");
55

66
class EventsValidator extends LinValidator {
77
constructor () {
@@ -14,16 +14,20 @@ class EventsValidator extends LinValidator {
1414
class IdsValidator extends LinValidator {
1515
constructor () {
1616
super();
17-
this.ids = new Rule(this.checkIds, "每个id值必须为正整数");
17+
this.ids = new Rule("isNotEmpty", "每个id值必须为正整数");
1818
}
1919

20-
checkIds (ids) {
20+
validateIds (data) {
21+
const ids = data.body.ids;
2122
if (!Array.isArray(ids)) {
22-
return false;
23+
return [false, "每个id值必须为正整数"];
2324
}
24-
for (const id of ids) {
25-
if (!extendedValidator.isInt2(id)) {
26-
return false;
25+
for (let id of ids) {
26+
if (typeof id === "number") {
27+
id = String(id);
28+
}
29+
if (!validator.isInt(id, { min: 1 })) {
30+
return [false, "每个id值必须为正整数"];
2731
}
2832
}
2933
return true;

app/validators/admin.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

3-
const { Rule, LinValidator } = require("lin-mizar");
4-
const { extendedValidator } = require("lin-mizar/lin/extended-validator");
3+
const { Rule, LinValidator, isNotEmpty } = require("lin-mizar");
54
const { PaginateValidator, PositiveIdValidator } = require("./common");
65

76
class AdminUsersValidator extends PaginateValidator {
@@ -22,17 +21,19 @@ class ResetPasswordValidator extends PositiveIdValidator {
2221
"密码长度必须在6~22位之间,包含字符、数字和 _ ",
2322
/^[A-Za-z0-9_*&$#@]{6,22}$/
2423
);
25-
this.confirm_password = new Rule(
26-
this.passwordCheck.bind(this),
27-
"两次输入密码不一致"
28-
);
24+
this.confirm_password = new Rule("isNotEmpty", "确认密码不可为空");
2925
}
3026

31-
passwordCheck (val) {
32-
if (!this.data.body.new_password || !this.data.body.confirm_password) {
33-
return false;
27+
validateConfirmPassword (data) {
28+
if (!data.body.new_password || !data.body.confirm_password) {
29+
return [false, "两次输入的密码不一致,请重新输入"];
30+
}
31+
let ok = data.body.new_password === data.body.confirm_password;
32+
if (ok) {
33+
return ok;
34+
} else {
35+
return [false, "两次输入的密码不一致,请重新输入"];
3436
}
35-
return this.data.body.new_password === this.data.body.confirm_password;
3637
}
3738
}
3839

@@ -58,15 +59,17 @@ class RemoveAuthsValidator extends LinValidator {
5859
constructor () {
5960
super();
6061
this.group_id = new Rule("isInt", "分组id必须正整数");
61-
this.auths = new Rule(this.checkAuths, "请输入auths字段");
62+
this.auths = new Rule("isNotEmpty", "请输入auths字段");
6263
}
63-
checkAuths (auths) {
64+
65+
validateAuths (data) {
66+
const auths = data.body.auths;
6467
if (!Array.isArray(auths)) {
65-
return false;
68+
return [false, "auths必须为非空数组"];
6669
}
67-
for (const auth in auths) {
68-
if (!extendedValidator.isNotEmpty(auth)) {
69-
return false;
70+
for (const auth of auths) {
71+
if (!isNotEmpty(auth)) {
72+
return [false, "auths必须为非空数组"];
7073
}
7174
}
7275
return true;
@@ -77,16 +80,17 @@ class DispatchAuthsValidator extends LinValidator {
7780
constructor () {
7881
super();
7982
this.group_id = new Rule("isInt", "分组id必须正整数");
80-
this.auths = new Rule(this.checkAuths, "请输入auths字段");
83+
this.auths = new Rule("isNotEmpty", "请输入auths字段");
8184
}
8285

83-
checkAuths (auths) {
86+
validateAuths (data) {
87+
const auths = data.body.auths;
8488
if (!Array.isArray(auths)) {
85-
return false;
89+
return [false, "auths必须为非空数组"];
8690
}
87-
for (const auth in auths) {
88-
if (!extendedValidator.isNotEmpty(auth)) {
89-
return false;
91+
for (const auth of auths) {
92+
if (!isNotEmpty(auth)) {
93+
return [false, "auths必须为非空数组"];
9094
}
9195
}
9296
return true;
@@ -98,19 +102,17 @@ class NewGroupValidator extends LinValidator {
98102
super();
99103
this.name = new Rule("isNotEmpty", "请输入分组名称");
100104
this.info = new Rule("isOptional");
101-
this.auths = new Rule(this.checkAuths, "请输入auths字段");
105+
this.auths = new Rule("isNotEmpty", "请输入auths字段");
102106
}
103107

104-
checkAuths (auths) {
108+
validateAuths (data) {
109+
const auths = data.body.auths;
105110
if (!Array.isArray(auths)) {
106-
return false;
107-
}
108-
if (auths.length === 0) {
109-
return true;
111+
return [false, "auths必须为非空数组"];
110112
}
111-
for (const auth in auths) {
112-
if (!extendedValidator.isNotEmpty(auth)) {
113-
return false;
113+
for (const auth of auths) {
114+
if (!isNotEmpty(auth)) {
115+
return [false, "auths必须为非空数组"];
114116
}
115117
}
116118
return true;

app/validators/log.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,39 @@
22

33
const { Rule, checkDateFormat } = require("lin-mizar");
44
const { PaginateValidator } = require("./common");
5+
const { isOptional } = require("../libs/util");
56

67
class LogFindValidator extends PaginateValidator {
78
constructor () {
89
super();
910
this.name = new Rule("isOptional");
10-
this.start = [
11-
new Rule("isOptional"),
12-
new Rule(checkDateFormat, "请输入正确格式开始时间")
13-
];
14-
this.end = [
15-
new Rule("isOptional"),
16-
new Rule(checkDateFormat, "请输入正确格式开始时间")
17-
];
11+
}
12+
13+
validateStart (data) {
14+
const start = data.query.start;
15+
// 如果 start 为可选
16+
if (isOptional(start)) {
17+
return true;
18+
}
19+
const ok = checkDateFormat(start);
20+
if (ok) {
21+
return ok;
22+
} else {
23+
return [false, "请输入正确格式开始时间", "start"];
24+
}
25+
}
26+
27+
validateEnd (data) {
28+
const end = data.query.start;
29+
if (isOptional(end)) {
30+
return true;
31+
}
32+
const ok = checkDateFormat(end);
33+
if (ok) {
34+
return ok;
35+
} else {
36+
return [false, "请输入正确格式结束时间", "end"];
37+
}
1838
}
1939
}
2040

app/validators/user.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ class RegisterValidator extends LinValidator {
2323
/^[A-Za-z0-9_*&$#@]{6,22}$/
2424
)
2525
];
26-
this.confirm_password = [
27-
new Rule(this.passwordCheck.bind(this), "两次密码输入不一致")
28-
];
26+
this.confirm_password = new Rule("isNotEmpty", "确认密码不可为空");
2927
}
3028

31-
passwordCheck (val) {
32-
if (!this.data.body.password || !this.data.body.confirm_password) {
33-
return false;
29+
validateConfirmPassword (data) {
30+
if (!data.body.password || !data.body.confirm_password) {
31+
return [false, "两次输入的密码不一致,请重新输入"];
32+
}
33+
let ok = data.body.password === data.body.confirm_password;
34+
if (ok) {
35+
return ok;
36+
} else {
37+
return [false, "两次输入的密码不一致,请重新输入"];
3438
}
35-
let ok = this.data.body.password === this.data.body.confirm_password;
36-
return ok;
3739
}
3840
}
3941

@@ -62,18 +64,20 @@ class ChangePasswordValidator extends LinValidator {
6264
"matches",
6365
"密码长度必须在6~22位之间,包含字符、数字和 _ "
6466
);
65-
this.confirm_password = new Rule(
66-
this.passwordCheck.bind(this),
67-
"两次输入密码不一致"
68-
);
67+
this.confirm_password = new Rule("isNotEmpty", "确认密码不可为空");
6968
this.old_password = new Rule("isNotEmpty", "请输入旧密码");
7069
}
7170

72-
passwordCheck (val) {
73-
if (!this.data.body.new_password || !this.data.body.confirm_password) {
74-
return false;
71+
validateConfirmPassword (data) {
72+
if (!data.body.new_password || !data.body.confirm_password) {
73+
return [false, "两次输入的密码不一致,请重新输入"];
74+
}
75+
let ok = data.body.new_password === data.body.confirm_password;
76+
if (ok) {
77+
return ok;
78+
} else {
79+
return [false, "两次输入的密码不一致,请重新输入"];
7580
}
76-
return this.data.body.new_password === this.data.body.confirm_password;
7781
}
7882
}
7983

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
"@koa/cors": "^2.2.3",
4040
"koa": "^2.7.0",
4141
"koa-bodyparser": "^4.2.1",
42-
"lin-mizar": "^0.0.1-alpha.22"
42+
"lin-mizar": "^0.0.1-alpha.25"
4343
}
4444
}

0 commit comments

Comments
 (0)