1
- import { isNil , isNumber , isString , isArray } from "lodash" ;
1
+ import { defaults , isNil , isNumber , isString , isArray , isFunction } from "lodash" ;
2
2
import fecha from "fecha" ;
3
3
4
- function checkEmpty ( value , required ) {
5
- if ( isNil ( value ) || value === "" ) {
6
- if ( required )
7
- return [ msg ( resources . fieldIsRequired ) ] ;
8
- else
9
- return [ ] ;
10
- }
11
- return null ;
12
- }
13
-
14
- function msg ( text ) {
15
- if ( text != null && arguments . length > 1 )
16
- for ( let i = 1 ; i < arguments . length ; i ++ )
17
- text = text . replace ( / \{ \d + ?\} / , arguments [ i ] ) ;
18
-
19
- return text ;
20
- }
21
-
22
4
let resources = {
23
5
fieldIsRequired : "This field is required!" ,
24
6
invalidFormat : "Invalid format!" ,
@@ -50,143 +32,162 @@ let resources = {
50
32
invalidTextContainSpec : "Invalid text! Cannot contains special characters"
51
33
} ;
52
34
35
+
36
+ function checkEmpty ( value , required , messages = resources ) {
37
+ if ( isNil ( value ) || value === "" ) {
38
+ if ( required )
39
+ return [ msg ( messages . fieldIsRequired ) ] ;
40
+ else
41
+ return [ ] ;
42
+ }
43
+ return null ;
44
+ }
45
+
46
+ function msg ( text ) {
47
+ if ( text != null && arguments . length > 1 )
48
+ for ( let i = 1 ; i < arguments . length ; i ++ )
49
+ text = text . replace ( "{" + ( i - 1 ) + "}" , arguments [ i ] ) ;
50
+
51
+ return text ;
52
+ }
53
+
53
54
module . exports = {
54
55
55
56
resources,
56
57
57
- required ( value , field ) {
58
- return checkEmpty ( value , field . required ) ;
58
+ required ( value , field , model , messages = resources ) {
59
+ return checkEmpty ( value , field . required , messages ) ;
59
60
} ,
60
61
61
- number ( value , field ) {
62
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
62
+ number ( value , field , model , messages = resources ) {
63
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
63
64
64
65
let err = [ ] ;
65
66
if ( isNumber ( value ) ) {
66
67
if ( ! isNil ( field . min ) && value < field . min )
67
- err . push ( msg ( resources . numberTooSmall , field . min ) ) ;
68
+ err . push ( msg ( messages . numberTooSmall , field . min ) ) ;
68
69
69
70
if ( ! isNil ( field . max ) && value > field . max )
70
- err . push ( msg ( resources . numberTooBig , field . max ) ) ;
71
+ err . push ( msg ( messages . numberTooBig , field . max ) ) ;
71
72
72
73
} else
73
- err . push ( msg ( resources . invalidNumber ) ) ;
74
+ err . push ( msg ( messages . invalidNumber ) ) ;
74
75
75
76
return err ;
76
77
} ,
77
78
78
- integer ( value , field ) {
79
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
79
+ integer ( value , field , model , messages = resources ) {
80
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
80
81
81
82
if ( ! ( Number ( value ) === value && value % 1 === 0 ) )
82
- return [ msg ( resources . invalidNumber ) ] ;
83
+ return [ msg ( messages . invalidNumber ) ] ;
83
84
} ,
84
85
85
- double ( value , field ) {
86
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
86
+ double ( value , field , model , messages = resources ) {
87
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
87
88
88
89
if ( ! isNumber ( value ) || isNaN ( value ) )
89
- return [ msg ( resources . invalidNumber ) ] ;
90
+ return [ msg ( messages . invalidNumber ) ] ;
90
91
} ,
91
92
92
- string ( value , field ) {
93
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
93
+ string ( value , field , model , messages = resources ) {
94
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
94
95
95
96
let err = [ ] ;
96
97
if ( isString ( value ) ) {
97
98
if ( ! isNil ( field . min ) && value . length < field . min )
98
- err . push ( msg ( resources . textTooSmall , value . length , field . min ) ) ;
99
+ err . push ( msg ( messages . textTooSmall , value . length , field . min ) ) ;
99
100
100
101
if ( ! isNil ( field . max ) && value . length > field . max )
101
- err . push ( msg ( resources . textTooBig , value . length , field . max ) ) ;
102
+ err . push ( msg ( messages . textTooBig , value . length , field . max ) ) ;
102
103
103
104
} else
104
- err . push ( msg ( resources . thisNotText ) ) ;
105
+ err . push ( msg ( messages . thisNotText ) ) ;
105
106
106
107
return err ;
107
108
} ,
108
109
109
- array ( value , field ) {
110
+ array ( value , field , model , messages = resources ) {
110
111
if ( field . required ) {
111
112
112
113
if ( ! isArray ( value ) )
113
- return [ msg ( resources . thisNotArray ) ] ;
114
+ return [ msg ( messages . thisNotArray ) ] ;
114
115
115
116
if ( value . length == 0 )
116
- return [ msg ( resources . fieldIsRequired ) ] ;
117
+ return [ msg ( messages . fieldIsRequired ) ] ;
117
118
}
118
119
119
120
if ( ! isNil ( value ) ) {
120
121
if ( ! isNil ( field . min ) )
121
122
if ( value . length < field . min )
122
- return [ msg ( resources . selectMinItems , field . min ) ] ;
123
+ return [ msg ( messages . selectMinItems , field . min ) ] ;
123
124
124
125
if ( ! isNil ( field . max ) )
125
126
if ( value . length > field . max )
126
- return [ msg ( resources . selectMaxItems , field . max ) ] ;
127
+ return [ msg ( messages . selectMaxItems , field . max ) ] ;
127
128
}
128
129
} ,
129
130
130
- date ( value , field ) {
131
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
131
+ date ( value , field , model , messages = resources ) {
132
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
132
133
133
134
let m = new Date ( value ) ;
134
135
if ( ! m )
135
- return [ msg ( resources . invalidDate ) ] ;
136
+ return [ msg ( messages . invalidDate ) ] ;
136
137
137
138
let err = [ ] ;
138
139
139
140
if ( ! isNil ( field . min ) ) {
140
141
let min = new Date ( field . min ) ;
141
142
if ( m . valueOf ( ) < min . valueOf ( ) )
142
- err . push ( msg ( resources . dateIsEarly , fecha . format ( m ) , fecha . format ( min ) ) ) ;
143
+ err . push ( msg ( messages . dateIsEarly , fecha . format ( m ) , fecha . format ( min ) ) ) ;
143
144
}
144
145
145
146
if ( ! isNil ( field . max ) ) {
146
147
let max = new Date ( field . max ) ;
147
148
if ( m . valueOf ( ) > max . valueOf ( ) )
148
- err . push ( msg ( resources . dateIsLate , fecha . format ( m ) , fecha . format ( max ) ) ) ;
149
+ err . push ( msg ( messages . dateIsLate , fecha . format ( m ) , fecha . format ( max ) ) ) ;
149
150
}
150
151
151
152
return err ;
152
153
} ,
153
154
154
- regexp ( value , field ) {
155
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
155
+ regexp ( value , field , model , messages = resources ) {
156
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
156
157
157
158
if ( ! isNil ( field . pattern ) ) {
158
159
let re = new RegExp ( field . pattern ) ;
159
160
if ( ! re . test ( value ) )
160
- return [ msg ( resources . invalidFormat ) ] ;
161
+ return [ msg ( messages . invalidFormat ) ] ;
161
162
}
162
163
} ,
163
164
164
- email ( value , field ) {
165
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
165
+ email ( value , field , model , messages = resources ) {
166
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
166
167
167
168
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 , } ) ) $ / ;
168
169
if ( ! re . test ( value ) )
169
- return [ msg ( resources . invalidEmail ) ] ;
170
+ return [ msg ( messages . invalidEmail ) ] ;
170
171
} ,
171
172
172
- url ( value , field ) {
173
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
173
+ url ( value , field , model , messages = resources ) {
174
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
174
175
175
176
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;
176
177
if ( ! re . test ( value ) )
177
- return [ msg ( resources . invalidURL ) ] ;
178
+ return [ msg ( messages . invalidURL ) ] ;
178
179
} ,
179
180
180
- creditCard ( value , field ) {
181
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
181
+ creditCard ( value , field , model , messages = resources ) {
182
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
182
183
183
184
/* From validator.js code
184
185
https://github.com/chriso/validator.js/blob/master/src/lib/isCreditCard.js
185
186
*/
186
187
const creditCard = / ^ (?: 4 [ 0 - 9 ] { 12 } (?: [ 0 - 9 ] { 3 } ) ? | 5 [ 1 - 5 ] [ 0 - 9 ] { 14 } | 6 (?: 0 1 1 | 5 [ 0 - 9 ] [ 0 - 9 ] ) [ 0 - 9 ] { 12 } | 3 [ 4 7 ] [ 0 - 9 ] { 13 } | 3 (?: 0 [ 0 - 5 ] | [ 6 8 ] [ 0 - 9 ] ) [ 0 - 9 ] { 11 } | (?: 2 1 3 1 | 1 8 0 0 | 3 5 \d { 3 } ) \d { 11 } ) $ / ;
187
188
const sanitized = value . replace ( / [ ^ 0 - 9 ] + / g, "" ) ;
188
189
if ( ! creditCard . test ( sanitized ) ) {
189
- return [ msg ( resources . invalidCard ) ] ;
190
+ return [ msg ( messages . invalidCard ) ] ;
190
191
}
191
192
let sum = 0 ;
192
193
let digit ;
@@ -209,22 +210,29 @@ module.exports = {
209
210
}
210
211
211
212
if ( ! ( ( sum % 10 ) === 0 ? sanitized : false ) )
212
- return [ msg ( resources . invalidCardNumber ) ] ;
213
+ return [ msg ( messages . invalidCardNumber ) ] ;
213
214
} ,
214
215
215
- alpha ( value , field ) {
216
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
216
+ alpha ( value , field , model , messages = resources ) {
217
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
217
218
218
219
let re = / ^ [ a - z A - Z ] * $ / ;
219
220
if ( ! re . test ( value ) )
220
- return [ msg ( resources . invalidTextContainNumber ) ] ;
221
+ return [ msg ( messages . invalidTextContainNumber ) ] ;
221
222
} ,
222
223
223
- alphaNumeric ( value , field ) {
224
- let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
224
+ alphaNumeric ( value , field , model , messages = resources ) {
225
+ let res = checkEmpty ( value , field . required , messages ) ; if ( res != null ) return res ;
225
226
226
227
let re = / ^ [ a - z A - Z 0 - 9 ] * $ / ;
227
228
if ( ! re . test ( value ) )
228
- return [ msg ( resources . invalidTextContainSpec ) ] ;
229
+ return [ msg ( messages . invalidTextContainSpec ) ] ;
229
230
}
230
231
} ;
232
+
233
+ Object . keys ( module . exports ) . forEach ( name => {
234
+ const fn = module . exports [ name ] ;
235
+ if ( isFunction ( fn ) ) {
236
+ fn . locale = customMessages => ( value , field , model ) => fn ( value , field , model , defaults ( customMessages , resources ) ) ;
237
+ }
238
+ } ) ;
0 commit comments