@@ -4,14 +4,55 @@ import moment from "moment";
44function checkEmpty ( value , required ) {
55 if ( isNil ( value ) || value === "" ) {
66 if ( required )
7- return [ "This field is required!" ] ;
7+ return [ msg ( resources . fieldIsRequired ) ] ;
88 else
99 return [ ] ;
1010 }
1111 return null ;
1212}
1313
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+ let resources = {
23+ fieldIsRequired : "This field is required!" ,
24+ invalidFormat : "Invalid format!" ,
25+
26+ numberTooSmall : "The number is too small! Minimum: {0}" ,
27+ numberTooBig : "The number is too big! Maximum: {0}" ,
28+ invalidNumber : "Invalid number" ,
29+
30+ textTooSmall : "The length of text is too small! Current: {0}, Minimum: {1}" ,
31+ textTooBig : "The length of text is too big! Current: {0}, Maximum: {1}" ,
32+ thisNotText : "This is not a text!" ,
33+
34+ thisNotArray : "This is not an array!" ,
35+
36+ selectMinItems : "Select minimum {0} items!" ,
37+ selectMaxItems : "Select maximum {0} items!" ,
38+
39+ invalidDate : "Invalid date!" ,
40+ dateIsEarly : "The date is too early! Current: {0}, Minimum: {1}" ,
41+ dateIsLate : "The date is too late! Current: {0}, Maximum: {1}" ,
42+
43+ invalidEmail : "Invalid e-mail address!" ,
44+ invalidURL : "Invalid URL!" ,
45+
46+ invalidCard : "Invalid card format!" ,
47+ invalidCardNumber : "Invalid card number!" ,
48+
49+ invalidTextContainNumber : "Invalid text! Cannot contains numbers or special characters" ,
50+ invalidTextContainSpec : "Invalid text! Cannot contains special characters"
51+ } ;
52+
1453module . exports = {
54+
55+ resources,
1556
1657 required ( value , field ) {
1758 return checkEmpty ( value , field . required ) ;
@@ -23,13 +64,13 @@ module.exports = {
2364 let err = [ ] ;
2465 if ( isNumber ( value ) ) {
2566 if ( ! isNil ( field . min ) && value < field . min )
26- err . push ( "The number is too small! Minimum: " + field . min ) ;
67+ err . push ( msg ( resources . numberTooSmall , field . min ) ) ;
2768
2869 if ( ! isNil ( field . max ) && value > field . max )
29- err . push ( "The number is too big! Maximum: " + field . max ) ;
70+ err . push ( msg ( resources . numberTooBig , field . max ) ) ;
3071
3172 } else
32- err . push ( "This is not a number!" ) ;
73+ err . push ( msg ( resources . invalidNumber ) ) ;
3374
3475 return err ;
3576 } ,
@@ -38,14 +79,14 @@ module.exports = {
3879 let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
3980
4081 if ( ! ( Number ( value ) === value && value % 1 === 0 ) )
41- return [ "Invalid number!" ] ;
82+ return [ msg ( resources . invalidNumber ) ] ;
4283 } ,
4384
4485 double ( value , field ) {
4586 let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
4687
4788 if ( ! ( Number ( value ) === value && value % 1 !== 0 ) )
48- return [ "Invalid number!" ] ;
89+ return [ msg ( resources . invalidNumber ) ] ;
4990 } ,
5091
5192 string ( value , field ) {
@@ -54,13 +95,13 @@ module.exports = {
5495 let err = [ ] ;
5596 if ( isString ( value ) ) {
5697 if ( ! isNil ( field . min ) && value . length < field . min )
57- err . push ( `The length of text is too small! Current: ${ value . length } , Minimum: ${ field . min } ` ) ;
98+ err . push ( msg ( resources . textTooSmall , value . length , field . min ) ) ;
5899
59100 if ( ! isNil ( field . max ) && value . length > field . max )
60- err . push ( `The length of text is too big! Current: ${ value . length } , Maximum: ${ field . max } ` ) ;
101+ err . push ( msg ( resources . textTooBig , value . length , field . max ) ) ;
61102
62103 } else
63- err . push ( "This is not a text!" ) ;
104+ err . push ( msg ( resources . thisNotText ) ) ;
64105
65106 return err ;
66107 } ,
@@ -69,20 +110,20 @@ module.exports = {
69110 if ( field . required ) {
70111
71112 if ( ! isArray ( value ) )
72- return [ "Value is not an array!" ] ;
113+ return [ msg ( resources . thisNotArray ) ] ;
73114
74115 if ( value . length == 0 )
75- return [ "This field is required!" ] ;
116+ return [ msg ( resources . fieldIsRequired ) ] ;
76117 }
77118
78119 if ( ! isNil ( value ) ) {
79120 if ( ! isNil ( field . min ) )
80121 if ( value . length < field . min )
81- return [ "Select minimum " + field . min + " items!" ] ;
122+ return [ msg ( resources . selectMinItems , field . min ) ] ;
82123
83124 if ( ! isNil ( field . max ) )
84125 if ( value . length > field . max )
85- return [ "Select maximum " + field . max + " items!" ] ;
126+ return [ msg ( resources . selectMaxItems , field . max ) ] ;
86127 }
87128 } ,
88129
@@ -91,20 +132,20 @@ module.exports = {
91132
92133 let m = moment ( value ) ;
93134 if ( ! m . isValid ( ) )
94- return [ "Invalid date!" ] ;
135+ return [ msg ( resources . invalidDate ) ] ;
95136
96137 let err = [ ] ;
97138
98139 if ( ! isNil ( field . min ) ) {
99140 let min = moment ( field . min ) ;
100141 if ( m . isBefore ( min ) )
101- err . push ( `The date is too early! Current: ${ m . format ( "L" ) } , Minimum: ${ min . format ( "L" ) } ` ) ;
142+ err . push ( msg ( resources . dateIsEarly , m . format ( "L" ) , min . format ( "L" ) ) ) ;
102143 }
103144
104145 if ( ! isNil ( field . max ) ) {
105146 let max = moment ( field . max ) ;
106147 if ( m . isAfter ( max ) )
107- err . push ( `The date is too late! Current: ${ m . format ( "L" ) } , Maximum: ${ max . format ( "L" ) } ` ) ;
148+ err . push ( msg ( resources . dateIsLate , m . format ( "L" ) , max . format ( "L" ) ) ) ;
108149 }
109150
110151 return err ;
@@ -116,7 +157,7 @@ module.exports = {
116157 if ( ! isNil ( field . pattern ) ) {
117158 let re = new RegExp ( field . pattern ) ;
118159 if ( ! re . test ( value ) )
119- return [ "Invalid format!" ] ;
160+ return [ msg ( resources . invalidFormat ) ] ;
120161 }
121162 } ,
122163
@@ -125,15 +166,15 @@ module.exports = {
125166
126167 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 , } ) ) $ / ;
127168 if ( ! re . test ( value ) )
128- return [ "Invalid e-mail address!" ] ;
169+ return [ msg ( resources . invalidEmail ) ] ;
129170 } ,
130171
131172 url ( value , field ) {
132173 let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
133174
134175 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;
135176 if ( ! re . test ( value ) )
136- return [ "Invalid URL!" ] ;
177+ return [ msg ( resources . invalidURL ) ] ;
137178 } ,
138179
139180 creditCard ( value , field ) {
@@ -145,7 +186,7 @@ module.exports = {
145186 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 } ) $ / ;
146187 const sanitized = value . replace ( / [ ^ 0 - 9 ] + / g, "" ) ;
147188 if ( ! creditCard . test ( sanitized ) ) {
148- return [ "Invalid card format!" ] ;
189+ return [ msg ( resources . invalidCard ) ] ;
149190 }
150191 let sum = 0 ;
151192 let digit ;
@@ -168,22 +209,22 @@ module.exports = {
168209 }
169210
170211 if ( ! ( ( sum % 10 ) === 0 ? sanitized : false ) )
171- return [ "Invalid card number!" ] ;
212+ return [ msg ( resources . invalidCardNumber ) ] ;
172213 } ,
173214
174215 alpha ( value , field ) {
175216 let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
176217
177218 let re = / ^ [ a - z A - Z ] * $ / ;
178219 if ( ! re . test ( value ) )
179- return [ "Invalid text! Cannot contains numbers or special characters" ] ;
220+ return [ msg ( resources . invalidTextContainNumber ) ] ;
180221 } ,
181222
182223 alphaNumeric ( value , field ) {
183224 let res = checkEmpty ( value , field . required ) ; if ( res != null ) return res ;
184225
185226 let re = / ^ [ a - z A - Z 0 - 9 ] * $ / ;
186227 if ( ! re . test ( value ) )
187- return [ "Invalid text! Cannot contains special characters" ] ;
228+ return [ msg ( resources . invalidTextContainSpec ) ] ;
188229 }
189230} ;
0 commit comments