1
- import { defaults , isNil , isNumber , isString , isArray , isFunction } from "lodash" ;
1
+ import { defaults , isNil , isNumber , isInteger , isString , isArray , isFunction } from "lodash" ;
2
2
import fecha from "fecha" ;
3
3
4
4
let resources = {
@@ -8,6 +8,7 @@ let resources = {
8
8
numberTooSmall : "The number is too small! Minimum: {0}" ,
9
9
numberTooBig : "The number is too big! Maximum: {0}" ,
10
10
invalidNumber : "Invalid number" ,
11
+ invalidInteger : "The value is not an integer" ,
11
12
12
13
textTooSmall : "The length of text is too small! Current: {0}, Minimum: {1}" ,
13
14
textTooBig : "The length of text is too big! Current: {0}, Maximum: {1}" ,
@@ -35,23 +36,26 @@ let resources = {
35
36
36
37
function checkEmpty ( value , required , messages = resources ) {
37
38
if ( isNil ( value ) || value === "" ) {
38
- if ( required )
39
+ if ( required ) {
39
40
return [ msg ( messages . fieldIsRequired ) ] ;
40
- else
41
+ } else {
41
42
return [ ] ;
43
+ }
42
44
}
43
45
return null ;
44
46
}
45
47
46
48
function msg ( text ) {
47
- if ( text != null && arguments . length > 1 )
48
- for ( let i = 1 ; i < arguments . length ; i ++ )
49
+ if ( text != null && arguments . length > 1 ) {
50
+ for ( let i = 1 ; i < arguments . length ; i ++ ) {
49
51
text = text . replace ( "{" + ( i - 1 ) + "}" , arguments [ i ] ) ;
52
+ }
53
+ }
50
54
51
55
return text ;
52
56
}
53
57
54
- module . exports = {
58
+ const validators = {
55
59
56
60
resources,
57
61
@@ -60,47 +64,59 @@ module.exports = {
60
64
} ,
61
65
62
66
number ( value , field , model , messages = resources ) {
63
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
67
+ let res = checkEmpty ( value , field . required , messages ) ;
68
+ if ( res != null ) return res ;
64
69
65
70
let err = [ ] ;
66
71
if ( isNumber ( value ) ) {
67
- if ( ! isNil ( field . min ) && value < field . min )
72
+ if ( ! isNil ( field . min ) && value < field . min ) {
68
73
err . push ( msg ( messages . numberTooSmall , field . min ) ) ;
74
+ }
69
75
70
- if ( ! isNil ( field . max ) && value > field . max )
76
+ if ( ! isNil ( field . max ) && value > field . max ) {
71
77
err . push ( msg ( messages . numberTooBig , field . max ) ) ;
72
-
73
- } else
78
+ }
79
+ } else {
74
80
err . push ( msg ( messages . invalidNumber ) ) ;
81
+ }
75
82
76
83
return err ;
77
84
} ,
78
85
79
86
integer ( value , field , model , messages = resources ) {
80
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
87
+ let res = checkEmpty ( value , field . required , messages ) ;
88
+ if ( res != null ) return res ;
89
+ let errs = validators . number ( value , field , model , messages ) ;
81
90
82
- if ( ! ( Number ( value ) === value && value % 1 === 0 ) )
83
- return [ msg ( messages . invalidNumber ) ] ;
91
+ if ( ! isInteger ( value ) ) {
92
+ errs . push ( msg ( messages . invalidInteger ) ) ;
93
+ }
94
+
95
+ return errs ;
84
96
} ,
85
97
86
98
double ( value , field , model , messages = resources ) {
87
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
99
+ let res = checkEmpty ( value , field . required , messages ) ;
100
+ if ( res != null ) return res ;
88
101
89
- if ( ! isNumber ( value ) || isNaN ( value ) )
102
+ if ( ! isNumber ( value ) || isNaN ( value ) ) {
90
103
return [ msg ( messages . invalidNumber ) ] ;
104
+ }
91
105
} ,
92
106
93
107
string ( value , field , model , messages = resources ) {
94
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
108
+ let res = checkEmpty ( value , field . required , messages ) ;
109
+ if ( res != null ) return res ;
95
110
96
111
let err = [ ] ;
97
112
if ( isString ( value ) ) {
98
- if ( ! isNil ( field . min ) && value . length < field . min )
113
+ if ( ! isNil ( field . min ) && value . length < field . min ) {
99
114
err . push ( msg ( messages . textTooSmall , value . length , field . min ) ) ;
115
+ }
100
116
101
- if ( ! isNil ( field . max ) && value . length > field . max )
117
+ if ( ! isNil ( field . max ) && value . length > field . max ) {
102
118
err . push ( msg ( messages . textTooBig , value . length , field . max ) ) ;
103
-
119
+ }
104
120
} else
105
121
err . push ( msg ( messages . thisNotText ) ) ;
106
122
@@ -109,77 +125,89 @@ module.exports = {
109
125
110
126
array ( value , field , model , messages = resources ) {
111
127
if ( field . required ) {
112
-
113
- if ( ! isArray ( value ) )
128
+ if ( ! isArray ( value ) ) {
114
129
return [ msg ( messages . thisNotArray ) ] ;
130
+ }
115
131
116
- if ( value . length == 0 )
132
+ if ( value . length == 0 ) {
117
133
return [ msg ( messages . fieldIsRequired ) ] ;
134
+ }
118
135
}
119
136
120
137
if ( ! isNil ( value ) ) {
121
- if ( ! isNil ( field . min ) )
122
- if ( value . length < field . min )
123
- return [ msg ( messages . selectMinItems , field . min ) ] ;
138
+ if ( ! isNil ( field . min ) && value . length < field . min ) {
139
+ return [ msg ( messages . selectMinItems , field . min ) ] ;
140
+ }
124
141
125
- if ( ! isNil ( field . max ) )
126
- if ( value . length > field . max )
127
- return [ msg ( messages . selectMaxItems , field . max ) ] ;
142
+ if ( ! isNil ( field . max ) && value . length > field . max ) {
143
+ return [ msg ( messages . selectMaxItems , field . max ) ] ;
144
+ }
128
145
}
129
146
} ,
130
147
131
148
date ( value , field , model , messages = resources ) {
132
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
149
+ let res = checkEmpty ( value , field . required , messages ) ;
150
+ if ( res != null ) return res ;
133
151
134
152
let m = new Date ( value ) ;
135
- if ( ! m )
153
+ if ( ! m ) {
136
154
return [ msg ( messages . invalidDate ) ] ;
155
+ }
137
156
138
157
let err = [ ] ;
139
158
140
159
if ( ! isNil ( field . min ) ) {
141
160
let min = new Date ( field . min ) ;
142
- if ( m . valueOf ( ) < min . valueOf ( ) )
161
+ if ( m . valueOf ( ) < min . valueOf ( ) ) {
143
162
err . push ( msg ( messages . dateIsEarly , fecha . format ( m ) , fecha . format ( min ) ) ) ;
163
+ }
144
164
}
145
165
146
166
if ( ! isNil ( field . max ) ) {
147
167
let max = new Date ( field . max ) ;
148
- if ( m . valueOf ( ) > max . valueOf ( ) )
168
+ if ( m . valueOf ( ) > max . valueOf ( ) ) {
149
169
err . push ( msg ( messages . dateIsLate , fecha . format ( m ) , fecha . format ( max ) ) ) ;
170
+ }
150
171
}
151
172
152
173
return err ;
153
174
} ,
154
175
155
176
regexp ( value , field , model , messages = resources ) {
156
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
177
+ let res = checkEmpty ( value , field . required , messages ) ;
178
+ if ( res != null ) return res ;
157
179
158
180
if ( ! isNil ( field . pattern ) ) {
159
181
let re = new RegExp ( field . pattern ) ;
160
- if ( ! re . test ( value ) )
182
+ if ( ! re . test ( value ) ) {
161
183
return [ msg ( messages . invalidFormat ) ] ;
184
+ }
162
185
}
163
186
} ,
164
187
165
188
email ( value , field , model , messages = resources ) {
166
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
189
+ let res = checkEmpty ( value , field . required , messages ) ;
190
+ if ( res != null ) return res ;
167
191
168
192
let re = / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ) * ) | ( " .+ " ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } ] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / ; // eslint-disable-line no-useless-escape
169
- if ( ! re . test ( value ) )
193
+ if ( ! re . test ( value ) ) {
170
194
return [ msg ( messages . invalidEmail ) ] ;
195
+ }
171
196
} ,
172
197
173
198
url ( value , field , model , messages = resources ) {
174
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
199
+ let res = checkEmpty ( value , field . required , messages ) ;
200
+ if ( res != null ) return res ;
175
201
176
202
let re = / h t t p s ? : \/ \/ ( w w w \. ) ? [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 2 , 256 } \. [ a - z ] { 2 , 4 } \b ( [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & / / = ] * ) / g; // eslint-disable-line no-useless-escape
177
- if ( ! re . test ( value ) )
203
+ if ( ! re . test ( value ) ) {
178
204
return [ msg ( messages . invalidURL ) ] ;
205
+ }
179
206
} ,
180
207
181
208
creditCard ( value , field , model , messages = resources ) {
182
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
209
+ let res = checkEmpty ( value , field . required , messages ) ;
210
+ if ( res != null ) return res ;
183
211
184
212
/* From validator.js code
185
213
https://github.com/chriso/validator.js/blob/master/src/lib/isCreditCard.js
@@ -209,27 +237,34 @@ module.exports = {
209
237
shouldDouble = ! shouldDouble ;
210
238
}
211
239
212
- if ( ! ( ( sum % 10 ) === 0 ? sanitized : false ) )
240
+ if ( ! ( ( sum % 10 ) === 0 ? sanitized : false ) ) {
213
241
return [ msg ( messages . invalidCardNumber ) ] ;
242
+ }
214
243
} ,
215
244
216
245
alpha ( value , field , model , messages = resources ) {
217
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
246
+ let res = checkEmpty ( value , field . required , messages ) ;
247
+ if ( res != null ) return res ;
218
248
219
249
let re = / ^ [ a - z A - Z ] * $ / ;
220
- if ( ! re . test ( value ) )
250
+ if ( ! re . test ( value ) ) {
221
251
return [ msg ( messages . invalidTextContainNumber ) ] ;
252
+ }
222
253
} ,
223
254
224
255
alphaNumeric ( value , field , model , messages = resources ) {
225
- let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
256
+ let res = checkEmpty ( value , field . required , messages ) ;
257
+ if ( res != null ) return res ;
226
258
227
259
let re = / ^ [ a - z A - Z 0 - 9 ] * $ / ;
228
- if ( ! re . test ( value ) )
260
+ if ( ! re . test ( value ) ) {
229
261
return [ msg ( messages . invalidTextContainSpec ) ] ;
262
+ }
230
263
}
231
264
} ;
232
265
266
+ module . exports = validators ;
267
+
233
268
Object . keys ( module . exports ) . forEach ( name => {
234
269
const fn = module . exports [ name ] ;
235
270
if ( isFunction ( fn ) ) {
0 commit comments