1
1
"use strict" ;
2
- var __values = ( this && this . __values ) || function ( o ) {
3
- var s = typeof Symbol === "function" && Symbol . iterator , m = s && o [ s ] , i = 0 ;
4
- if ( m ) return m . call ( o ) ;
5
- if ( o && typeof o . length === "number" ) return {
6
- next : function ( ) {
7
- if ( o && i >= o . length ) o = void 0 ;
8
- return { value : o && o [ i ++ ] , done : ! o } ;
9
- }
10
- } ;
11
- throw new TypeError ( s ? "Object is not iterable." : "Symbol.iterator is not defined." ) ;
12
- } ;
13
2
var __read = ( this && this . __read ) || function ( o , n ) {
14
3
var m = typeof Symbol === "function" && o [ Symbol . iterator ] ;
15
4
if ( ! m ) return o ;
@@ -26,50 +15,68 @@ var __read = (this && this.__read) || function (o, n) {
26
15
}
27
16
return ar ;
28
17
} ;
18
+ var __values = ( this && this . __values ) || function ( o ) {
19
+ var s = typeof Symbol === "function" && Symbol . iterator , m = s && o [ s ] , i = 0 ;
20
+ if ( m ) return m . call ( o ) ;
21
+ if ( o && typeof o . length === "number" ) return {
22
+ next : function ( ) {
23
+ if ( o && i >= o . length ) o = void 0 ;
24
+ return { value : o && o [ i ++ ] , done : ! o } ;
25
+ }
26
+ } ;
27
+ throw new TypeError ( s ? "Object is not iterable." : "Symbol.iterator is not defined." ) ;
28
+ } ;
29
29
var __spread = ( this && this . __spread ) || function ( ) {
30
30
for ( var ar = [ ] , i = 0 ; i < arguments . length ; i ++ ) ar = ar . concat ( __read ( arguments [ i ] ) ) ;
31
31
return ar ;
32
32
} ;
33
33
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
34
- exports . toValue = exports . formatData = exports . parseBody = exports . parseData = exports . checkLength = exports . validSign = exports . filterData = void 0 ;
34
+ exports . toValue = exports . formatData = exports . parseBody = exports . parseData = exports . checkLength = exports . errorInfo = exports . validSign = exports . filterData = void 0 ;
35
35
var lodash_1 = require ( "lodash" ) ;
36
36
var rule_judgment_1 = require ( "rule-judgment" ) ;
37
37
var MD5 = require ( "md5.js" ) ;
38
38
function filterData ( options , customize ) {
39
- return function ( data ) {
39
+ return function ( data , errorCode ) {
40
40
var e_1 , _a ;
41
41
var values = { } ;
42
- try {
43
- for ( var options_1 = __values ( options ) , options_1_1 = options_1 . next ( ) ; ! options_1_1 . done ; options_1_1 = options_1 . next ( ) ) {
44
- var item = options_1_1 . value ;
45
- var key = item . key , type = item . type , rules = item . rules , format = item . format , defaultValue = item . defaultValue , md5 = item . md5 , separator = item . separator ;
46
- var value = data [ key ] ;
47
- if ( / \[ \] $ / . test ( type ) && ! lodash_1 . isArray ( value ) ) {
48
- value = toValue ( 'string' ) ( value || '' ) . split ( separator || / \, / ) ;
42
+ var _loop_1 = function ( item ) {
43
+ var key = item . key , type = item . type , rules = item . rules , format = item . format , defaultValue = item . defaultValue , md5 = item . md5 , separator = item . separator ;
44
+ var value = data [ key ] ;
45
+ if ( / \[ \] $ / . test ( type ) && ! lodash_1 . isArray ( value ) ) {
46
+ value = toValue ( 'string' ) ( value || '' ) . split ( separator || / \, / ) ;
47
+ }
48
+ if ( / \[ \] $ / . test ( type ) && lodash_1 . isArray ( value ) ) {
49
+ var _a = __read ( type . match ( / ( \S + ) ( \[ \] ) $ / ) , 2 ) , itype = _a [ 1 ] ;
50
+ value = lodash_1 . compact ( value ) . map ( toValue ( itype ) ) ;
51
+ if ( rules ) {
52
+ value . forEach ( function ( v ) { return validateRule ( rules || [ ] , customize ) ( v , errorCode ) ; } ) ;
49
53
}
50
- if ( / \[ \] $ / . test ( type ) && lodash_1 . isArray ( value ) ) {
51
- var _b = __read ( type . match ( / ( \S + ) ( \[ \] ) $ / ) , 2 ) , itype = _b [ 1 ] ;
52
- value = lodash_1 . compact ( value ) . map ( toValue ( itype ) ) ;
53
- rules && value . forEach ( validateRule ( rules , customize ) ) ;
54
- if ( defaultValue && value . length === 0 ) {
55
- value = defaultValue ;
56
- }
57
- if ( format ) {
58
- value = value . map ( formatData ( format , customize ) ) ;
59
- }
54
+ if ( defaultValue && value . length === 0 ) {
55
+ value = defaultValue ;
60
56
}
61
- else {
62
- value = toValue ( type ) ( value ) ;
63
- rules && validateRule ( rules , customize ) ( value ) ;
64
- value = value || defaultValue ;
65
- if ( format ) {
66
- value = formatData ( format , customize ) ( value ) ;
67
- }
68
- if ( md5 ) {
69
- value = new MD5 ( ) . update ( lodash_1 . template ( md5 ) ( values ) ) . digest ( 'hex' ) ;
70
- }
57
+ if ( format ) {
58
+ value = value . map ( formatData ( format , customize ) ) ;
71
59
}
72
- lodash_1 . set ( values , key , value ) ;
60
+ }
61
+ else {
62
+ value = toValue ( type ) ( value ) ;
63
+ if ( rules ) {
64
+ validateRule ( rules , customize ) ( value , errorCode ) ;
65
+ }
66
+ value = value || defaultValue ;
67
+ if ( format ) {
68
+ value = formatData ( format , customize ) ( value ) ;
69
+ }
70
+ if ( md5 ) {
71
+ value = new MD5 ( ) . update ( lodash_1 . template ( md5 ) ( values ) ) . digest ( 'hex' ) ;
72
+ }
73
+ }
74
+ lodash_1 . set ( values , key , value ) ;
75
+ } ;
76
+ try {
77
+ for ( var options_1 = __values ( options ) , options_1_1 = options_1 . next ( ) ; ! options_1_1 . done ; options_1_1 = options_1 . next ( ) ) {
78
+ var item = options_1_1 . value ;
79
+ _loop_1 ( item ) ;
73
80
}
74
81
}
75
82
catch ( e_1_1 ) { e_1 = { error : e_1_1 } ; }
@@ -92,26 +99,26 @@ function validSign(options, sign) {
92
99
}
93
100
exports . validSign = validSign ;
94
101
function validateRule ( rules , customize ) {
95
- return function ( value ) {
102
+ return function ( value , errorCode ) {
96
103
var e_2 , _a ;
97
104
try {
98
105
for ( var rules_1 = __values ( rules ) , rules_1_1 = rules_1 . next ( ) ; ! rules_1_1 . done ; rules_1_1 = rules_1 . next ( ) ) {
99
106
var rule = rules_1_1 . value ;
100
- var required = rule . required , message = rule . message , min = rule . min , max = rule . max , pattern = rule . pattern , validator = rule . validator ;
107
+ var required = rule . required , message = rule . message , min = rule . min , max = rule . max , pattern = rule . pattern , validator = rule . validator , code = rule . code ;
101
108
if ( required && ( lodash_1 . isUndefined ( value ) || value === '' ) ) {
102
- throw new Error ( message ) ;
109
+ throw errorInfo ( message , code || errorCode ) ;
103
110
}
104
111
if ( lodash_1 . isString ( value ) ) {
105
112
if ( min && checkLength ( value ) < min ) {
106
- throw new Error ( message ) ;
113
+ throw errorInfo ( message , code || errorCode ) ;
107
114
}
108
115
if ( max && checkLength ( value ) > max ) {
109
- throw new Error ( message ) ;
116
+ throw errorInfo ( message , code || errorCode ) ;
110
117
}
111
118
if ( pattern ) {
112
119
var reg = getRegexp ( pattern ) ;
113
120
if ( ! reg . test ( value ) ) {
114
- throw new Error ( message ) ;
121
+ throw errorInfo ( message , code || errorCode ) ;
115
122
}
116
123
}
117
124
}
@@ -122,7 +129,7 @@ function validateRule(rules, customize) {
122
129
}
123
130
if ( validator && lodash_1 . isFunction ( validator ) ) {
124
131
if ( ! validator ( value ) || String ( value ) === 'Invalid Date' ) {
125
- throw new Error ( message ) ;
132
+ throw errorInfo ( message , code || errorCode ) ;
126
133
}
127
134
}
128
135
}
@@ -136,6 +143,14 @@ function validateRule(rules, customize) {
136
143
}
137
144
} ;
138
145
}
146
+ function errorInfo ( message , code ) {
147
+ var error = new Error ( message ) ;
148
+ if ( code ) {
149
+ error . code = code ;
150
+ }
151
+ return error ;
152
+ }
153
+ exports . errorInfo = errorInfo ;
139
154
function checkLength ( str ) {
140
155
var e_3 , _a ;
141
156
var size = 0 ;
0 commit comments