@@ -3,12 +3,12 @@ import { get, isArray, set, unset } from "lodash";
33import { ParametersException } from "./exception" ;
44import { Context } from "koa" ;
55import { 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 / v a l i d a t e ( [ A - Z ] ) \w + / g. test ( key ) && typeof this [ key ] === "function" ;
182+ let validateFuncKeys : string [ ] = getAllMethodNames ( this , {
183+ filter : key =>
184+ / v a l i d a t e ( [ 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 }
0 commit comments