Skip to content

Commit f30f0fe

Browse files
author
pedro
committed
feat:更新库名和validator
1 parent 351f4a1 commit f30f0fe

5 files changed

Lines changed: 65 additions & 21 deletions

File tree

lib/core.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,14 @@ export class Lin {
9898
);
9999
set(ly, "path", `/${get(plugin, "name")}${get(ly, "path")}`);
100100
});
101-
// cont.stack[0].path
102101
pluginRp
103102
.use((cont as any).routes() as IMiddleware)
104103
.use((cont as any).allowedMethods() as IMiddleware);
105104
});
106105
} else {
107106
controllers.forEach(cont => {
108107
get(cont, "stack", []).forEach(ly => {
109-
consola.info(`loading a route: /plugin/${get(ly, "path")}`);
108+
consola.info(`loading a route: /plugin${get(ly, "path")}`);
110109
});
111110
pluginRp
112111
.use((cont as any).routes() as IMiddleware)

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * from "./core";
22
export * from "./enums";
33
export * from "./exception";
44
export * from "./extend";
5-
export * from "./class-validator";
5+
export * from "./lin-validator";
66
export * from "./extended-validator";
77
export * from "./jwt";
88
export * from "./log";
Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { get, isArray, set, unset } from "lodash";
33
import { ParametersException } from "./exception";
44
import { Context } from "koa";
55
import { extendedValidator } from "./extended-validator";
6-
import { getAllMethodNames } from "./util";
6+
import { getAllMethodNames, getAllFieldNames } from "./util";
77

88
/**
99
* 支持optional,支持array,支持nested object
1010
*/
11-
export class ClassValidator {
11+
export class LinValidator {
1212
/**
1313
* 装载数据的容器
1414
*/
@@ -52,7 +52,6 @@ export class ClassValidator {
5252
}
5353
throw new ParametersException({ msg: obj });
5454
} else {
55-
// 把validator挂载到ctx上
5655
ctx.v = this;
5756
return this;
5857
}
@@ -80,12 +79,22 @@ export class ClassValidator {
8079
// 筛选出是Rule或Rules的key
8180
// 添加规则校验 validateKey
8281
// default校验规则 throw
83-
let keys = Object.keys(this).filter(key => {
84-
const value = this[key];
85-
if (isArray(value)) {
86-
return value[0] instanceof Rule;
87-
} else {
88-
return value instanceof Rule;
82+
let keys = getAllFieldNames(this, {
83+
filter: key => {
84+
const value = this[key];
85+
if (isArray(value)) {
86+
if (value.length === 0) {
87+
return false;
88+
}
89+
for (const it of value) {
90+
if (!(it instanceof Rule)) {
91+
throw new Error("every item must be a instance of Rule");
92+
}
93+
}
94+
return true;
95+
} else {
96+
return value instanceof Rule;
97+
}
8998
}
9099
});
91100
// 此处进行别名替换
@@ -170,9 +179,11 @@ export class ClassValidator {
170179
}
171180
}
172181
}
173-
let validateFuncKeys: string[] = getAllMethodNames(this).filter(key => {
174-
return /validate([A-Z])\w+/g.test(key) && typeof this[key] === "function";
182+
let validateFuncKeys: string[] = getAllMethodNames(this, {
183+
filter: key =>
184+
/validate([A-Z])\w+/g.test(key) && typeof this[key] === "function"
175185
});
186+
176187
for (const validateFuncKey of validateFuncKeys) {
177188
// 最后校验规则函数
178189
const customerValidateFunc = get(this, validateFuncKey);
@@ -191,18 +202,33 @@ export class ClassValidator {
191202
key = validateFuncKey.replace("validate", "").toLowerCase();
192203
}
193204
this.errors.push({ key, message: validRes[1] });
205+
} else if (!validRes) {
206+
let key = validateFuncKey.replace("validate", "").toLowerCase();
207+
// 如果自定函数没有给出错误信息,那么错误信息为默认
208+
this.errors.push({ key, message: "参数错误" });
194209
}
195210
}
196211
return this.errors.length === 0;
197212
}
198213

199-
get(path: string, parsed = false) {
214+
/**
215+
* 取参数里的值;如果参数不能被解析,则返回没有被解析的值
216+
* @param path 参数所在的路径,如 a.b
217+
* @param parsed 是否取已经解析后的数据,默认为true
218+
* @param defaultVal 默认值,当路径指向的值不存在,取默认值
219+
*/
220+
get(path: string, parsed = true) {
200221
let defaultVal;
201222
if (arguments.length >= 3) {
202223
defaultVal = arguments[2];
203224
}
204225
if (parsed) {
205-
return get(this.parsed, path, defaultVal && defaultVal);
226+
const key = get(this.parsed, path, defaultVal && defaultVal);
227+
if (key) {
228+
return key;
229+
} else {
230+
return get(this.data, path, defaultVal && defaultVal);
231+
}
206232
} else {
207233
return get(this.data, path, defaultVal && defaultVal);
208234
}

lib/util.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,33 @@ export function decorateProp(decorators, type, target, key) {
157157
);
158158
}
159159

160-
export function getAllMethodNames(obj) {
160+
export interface ObjOptions {
161+
prefix?: string;
162+
filter?: (key: any) => boolean;
163+
}
164+
165+
export function getAllMethodNames(obj, option?: ObjOptions) {
161166
let methods = new Set();
162167
// tslint:disable-next-line:no-conditional-assignment
163168
while ((obj = Reflect.getPrototypeOf(obj))) {
164169
let keys = Reflect.ownKeys(obj);
165170
keys.forEach(k => methods.add(k));
166171
}
167-
return Array.from(methods.values());
172+
let keys = Array.from(methods.values());
173+
return prefixAndFilter(keys, option);
174+
}
175+
176+
export function getAllFieldNames(obj, option?: ObjOptions) {
177+
let keys = Reflect.ownKeys(obj);
178+
return prefixAndFilter(keys, option);
179+
}
180+
181+
function prefixAndFilter(keys: any[], option?: ObjOptions) {
182+
option &&
183+
option.prefix &&
184+
(keys = keys.filter(key => key.toString().startsWith(option.prefix)));
185+
option && option.filter && (keys = keys.filter(option.filter));
186+
return keys;
168187
}
169188

170189
export function getFiles(dir: string) {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "lin-cms",
3-
"version": "0.0.1-alpha6",
4-
"description": "The core library of Lin CMS, this package is just for test!",
2+
"name": "lin-mizar",
3+
"version": "0.0.1-alpha.1",
4+
"description": "The core library of Lin CMS",
55
"main": "lin/index.js",
66
"scripts": {
77
"tsc": " rm -rf lin/ && tsc",

0 commit comments

Comments
 (0)