Skip to content

Commit b4a977b

Browse files
author
pedro
committed
feat:重构校验器
1 parent f30f0fe commit b4a977b

3 files changed

Lines changed: 38 additions & 89 deletions

File tree

lib/extend.ts

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import Application from "koa";
22
import { HttpException } from "./exception";
33
import consola from "consola";
44
import { toLine, unsets } from "./util";
5-
import { JsonMixin } from "./mixin";
6-
import { get, remove, findIndex, isArray, isObject, isMap, set } from "lodash";
5+
import { get, set } from "lodash";
76

87
/**
98
* 将返回的结果json序列化
@@ -18,83 +17,18 @@ export const json = (app: Application) => {
1817
unsets(obj, hide);
1918
let data = Object.create(null);
2019
if (obj instanceof HttpException) {
21-
transform1(obj, data);
20+
transform(obj, data);
2221
set(data, "url", this.request.url);
2322
this.status = obj.code;
2423
} else {
25-
// data = jsonUnmarshal(obj);
26-
// data = transform2(obj);
2724
data = obj;
2825
}
2926
this.body = JSON.stringify(data);
3027
};
3128
};
3229

33-
function jsonUnmarshal(obj: any) {
34-
let data = Object.create(null);
35-
if (isObject(obj)) {
36-
if (obj instanceof JsonMixin) {
37-
transform1(obj, data);
38-
} else if (isMap(obj)) {
39-
obj.forEach((v, k) => {
40-
if (isObject(v)) {
41-
data[toLine(k)] = jsonUnmarshal(v);
42-
} else {
43-
data[toLine(k)] = v;
44-
}
45-
});
46-
} else if (isArray(obj)) {
47-
obj.forEach(v => {
48-
if (isObject(v)) {
49-
v = jsonUnmarshal(v);
50-
}
51-
});
52-
} else {
53-
Object.keys(obj).forEach(key => {
54-
if (isObject(obj[key])) {
55-
data[toLine(key)] = jsonUnmarshal(obj[key]);
56-
} else {
57-
data[toLine(key)] = obj[key];
58-
}
59-
});
60-
}
61-
} else {
62-
data = obj;
63-
}
64-
return data;
65-
}
66-
67-
function transform2(obj: any) {
68-
let data;
69-
if (isArray(obj)) {
70-
data = [];
71-
obj.forEach((it, index) => {
72-
data[index] = transform2(it);
73-
});
74-
} else {
75-
data = Object.create(null);
76-
const fields: string[] = Object.keys(obj);
77-
fields.forEach(field => {
78-
data[toLine(field)] = get(obj, field);
79-
});
80-
}
81-
return data;
82-
}
83-
84-
function transform1(obj: any, data: any) {
85-
const fields: string[] = get(obj, "fields", []);
86-
fields.forEach(field => {
87-
data[toLine(field)] = get(obj, field);
88-
});
89-
}
90-
91-
function transform(obj: any, hide: Array<any>, data: any) {
30+
function transform(obj: any, data: any) {
9231
const fields: string[] = get(obj, "fields", []);
93-
if (isArray(hide) && hide.length > 0) {
94-
remove(fields, item => {
95-
return findIndex(hide, item as any) !== -1;
96-
});
97-
}
9832
fields.forEach(field => {
9933
data[toLine(field)] = get(obj, field);
10034
});

lib/lin-validator.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ export class LinValidator {
3131

3232
async validate(ctx: Context, alias?: {}) {
3333
this.alias = alias;
34-
const params = Object.assign(
35-
{},
36-
ctx.request.body,
37-
ctx.request.query,
38-
ctx.params,
39-
ctx.request.header
40-
);
41-
for (const it of Object.keys(params)) {
42-
set(this.data, it, get(params, it));
43-
}
34+
this.data = {
35+
body: ctx.request.body,
36+
query: ctx.request.query,
37+
path: ctx.params,
38+
header: ctx.request.header
39+
};
40+
this.parsed = {
41+
...this.data,
42+
default: {}
43+
};
4444
if (!(await this.checkRules())) {
4545
let obj = {};
4646
if (this.errors.length === 1) {
@@ -106,7 +106,9 @@ export class LinValidator {
106106
const value = this[key];
107107
let defaultVal;
108108
// 如果没有传入这个参数,检查这个校验链中是否有 isOptional 如果有通过,否则抛异常
109-
if (extendedValidator.isEmpty(this.data[key])) {
109+
// 去data下找key,二级目录查找 dataKey 为一级目录的路径
110+
const [dataKey, dataVal] = this.findInData(key);
111+
if (dataVal === void 0) {
110112
let msg: string | undefined;
111113
if (isArray(value)) {
112114
for (const it of value) {
@@ -132,7 +134,7 @@ export class LinValidator {
132134
if (!optional) {
133135
this.errors.push({ key, message: msg || `${key}不可为空` });
134136
} else {
135-
this.data[key] = defaultVal;
137+
this.parsed["default"][key] = defaultVal;
136138
}
137139
} else {
138140
if (isArray(value)) {
@@ -142,17 +144,17 @@ export class LinValidator {
142144
if (!stoppedFlag && !it.optional) {
143145
let valid: boolean;
144146
if (isAsyncFunction(it.validate)) {
145-
valid = await it.validate(this.data[key]);
147+
valid = await it.validate(this.data[dataKey][key]);
146148
} else {
147-
valid = it.validate(this.data[key]);
149+
valid = it.validate(this.data[dataKey][key]);
148150
}
149151
if (!valid) {
150152
errs.push(it.message);
151153
// 如果当前key已有错误,则置stoppedFlag为true,后续会直接跳过校验
152154
stoppedFlag = true;
153155
}
154156
}
155-
it.parsedValue && (this.parsed[key] = it.parsedValue);
157+
it.parsedValue && (this.parsed[dataKey][key] = it.parsedValue);
156158
}
157159
if (errs.length !== 0) {
158160
this.errors.push({ key, message: errs });
@@ -162,17 +164,17 @@ export class LinValidator {
162164
if (!stoppedFlag && !value.optional) {
163165
let valid: boolean;
164166
if (isAsyncFunction(value.validate)) {
165-
valid = await value.validate(this.data[key]);
167+
valid = await value.validate(this.data[dataKey][key]);
166168
} else {
167-
valid = value.validate(this.data[key]);
169+
valid = value.validate(this.data[dataKey][key]);
168170
}
169171
if (!valid) {
170172
errs.push(value.message);
171173
// 如果当前key已有错误,则置stoppedFlag为true,后续会直接跳过校验
172174
stoppedFlag = true;
173175
}
174176
}
175-
value.parsedValue && (this.parsed[key] = value.parsedValue);
177+
value.parsedValue && (this.parsed[dataKey][key] = value.parsedValue);
176178
if (errs.length !== 0) {
177179
this.errors.push({ key, message: errs });
178180
}
@@ -227,12 +229,25 @@ export class LinValidator {
227229
if (key) {
228230
return key;
229231
} else {
230-
return get(this.data, path, defaultVal && defaultVal);
232+
const index = path.lastIndexOf(".");
233+
const suffix = path.substring(index + 1, path.length);
234+
return get(this.parsed["default"], suffix, defaultVal && defaultVal);
231235
}
232236
} else {
233237
return get(this.data, path, defaultVal && defaultVal);
234238
}
235239
}
240+
241+
private findInData(key: string) {
242+
const keys = Object.keys(this.data);
243+
for (const k of keys) {
244+
const val = get(this.data[k], key);
245+
if (val !== void 0) {
246+
return [k, val];
247+
}
248+
}
249+
return [];
250+
}
236251
}
237252

238253
export class Rule {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lin-mizar",
3-
"version": "0.0.1-alpha.1",
3+
"version": "0.0.1-alpha.2",
44
"description": "The core library of Lin CMS",
55
"main": "lin/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)