@@ -15,32 +15,69 @@ import {DecimalValidator} from './js/validators/DecimalValidator';
1515import { ConfirmationValidator } from './js/validators/ConfirmationValidator.js' ;
1616import { RegexValidator } from './js/validators/RegexValidator.js' ;
1717
18- function testSuccess ( response )
19- {
18+ function testSuccess ( response ) {
2019 expect ( response ) . toHaveProperty ( 'errors' , [ ] ) ;
2120}
2221
23- function testFailure ( response , errors , potentiallyValid = false )
24- {
22+ function testFailure ( response , errors , potentiallyValid = false ) {
2523 expect ( response ) . toHaveProperty ( 'errors' , errors ) ;
2624 expect ( response ) . toHaveProperty ( 'potentiallyValid' , potentiallyValid ) ;
2725}
2826
2927test (
3028 'deserialize' ,
31- ( ) =>
32- {
29+ ( ) => {
3330 let v = Validator . fromJsonObject ( { t : 'String' , c : { 'minLength' : 2 , 'maxLength' : 5 } } ) ;
3431 expect ( v ) . toBeInstanceOf ( StringValidator ) ;
3532 expect ( v . _minLength ) . toStrictEqual ( 2 ) ;
3633 expect ( v . _maxLength ) . toStrictEqual ( 5 ) ;
3734 }
3835) ;
3936
37+ test (
38+ 'TranslatedStringValidator' ,
39+ ( ) => {
40+ let v = new StringValidator ( 2 , 5 ) ;
41+ v . dictionary = {
42+ invalid : 'kein gültiger Wert' ,
43+ min : 'muss mindestens sein %s characters' ,
44+ max : 'darf nicht mehr als sein %s characters'
45+ } ;
46+
47+ testFailure ( v . validate ( ) , [ 'kein gültiger Wert' ] ) ;
48+ testFailure ( v . validate ( '' ) , [ 'muss mindestens sein 2 characters' ] ) ;
49+ testFailure ( v . validate ( 'testtest' ) , [ 'darf nicht mehr als sein 5 characters' ] ) ;
50+ }
51+ ) ;
52+
53+ test (
54+ 'MissinKeyTranslatedStringValidator' ,
55+ ( ) => {
56+ let v = new StringValidator ( 2 , 5 ) ;
57+ v . dictionary = {
58+ invalid : 'kein gültiger Wert' ,
59+ min : 'muss mindestens sein %s characters'
60+ // max is missing
61+ } ;
62+
63+ testFailure ( v . validate ( ) , [ 'kein gültiger Wert' ] ) ;
64+ testFailure ( v . validate ( '' ) , [ 'muss mindestens sein 2 characters' ] ) ;
65+ testFailure ( v . validate ( 'testesttest' ) , [ 'must be no more than 5 characters' ] ) ; // default error message
66+ }
67+ )
68+
69+ test (
70+ 'TranslatedRequiredValidator' ,
71+ ( ) => {
72+ let v = new RequiredValidator ( ) ;
73+ v . dictionary = { invalid : 'kein gültiger Wert' } ;
74+ testFailure ( v . validate ( ) , [ 'kein gültiger Wert' ] ) ;
75+ }
76+ ) ;
77+
4078test (
4179 'StringValidator' ,
42- ( ) =>
43- {
80+ ( ) => {
4481 let v = new StringValidator ( ) ;
4582 testSuccess ( v . validate ( 'test' ) ) ;
4683 testSuccess ( v . validate ( '' ) ) ;
70107
71108test (
72109 'BoolValidator' ,
73- ( ) =>
74- {
110+ ( ) => {
75111 const v = new BoolValidator ( ) ;
76112 testFailure ( v . validate ( 'test' ) , [ 'Invalid boolean value' ] ) ;
77113 testFailure ( v . validate ( '' ) , [ 'Invalid boolean value' ] ) ;
90126
91127test (
92128 'EnumValidator' ,
93- ( ) =>
94- {
129+ ( ) => {
95130 let v = new EnumValidator ( ) ;
96131 testSuccess ( v . validate ( '' ) ) ;
97132 testFailure ( v . validate ( 'test' ) , [ 'not a valid value' ] ) ;
@@ -117,8 +152,7 @@ test(
117152
118153test (
119154 'ConstEnumValidator' ,
120- ( ) =>
121- {
155+ ( ) => {
122156 let v = new ConstEnumValidator ( ) ;
123157 testSuccess ( v . validate ( '' ) ) ;
124158 testFailure ( v . validate ( 'test' ) , [ 'not a valid value' ] ) ;
@@ -144,8 +178,7 @@ test(
144178
145179test (
146180 'EqualValidator' ,
147- ( ) =>
148- {
181+ ( ) => {
149182 let v = new EqualValidator ( 'test' ) ;
150183 testFailure ( v . validate ( '' ) , [ 'value does not match' ] ) ;
151184 testSuccess ( v . validate ( 'test' ) ) ;
@@ -157,8 +190,7 @@ test(
157190
158191test (
159192 'NotEqualValidator' ,
160- ( ) =>
161- {
193+ ( ) => {
162194 let v = new NotEqualValidator ( 'test' ) ;
163195 testFailure ( v . validate ( 'test' ) , [ 'value must not match' ] ) ;
164196 testSuccess ( v . validate ( '' ) ) ;
@@ -170,8 +202,7 @@ test(
170202
171203test (
172204 'RequiredValidator' ,
173- ( ) =>
174- {
205+ ( ) => {
175206 let v = new RequiredValidator ( ) ;
176207 testSuccess ( v . validate ( true ) ) ;
177208 testSuccess ( v . validate ( false ) ) ;
@@ -189,8 +220,7 @@ test(
189220
190221test (
191222 'EmailValidator' ,
192- ( ) =>
193- {
223+ ( ) => {
194224 let v = new EmailValidator ( ) ;
195225 testSuccess ( v . validate ( '[email protected] ' ) ) ; 196226 testSuccess ( v . validate ( '[email protected] ' ) ) ; @@ -202,8 +232,7 @@ test(
202232
203233test (
204234 'IPv4Validator' ,
205- ( ) =>
206- {
235+ ( ) => {
207236 let v = new IPv4Validator ( ) ;
208237 testSuccess ( v . validate ( '0.0.0.0' ) ) ;
209238 testSuccess ( v . validate ( '255.255.255.255' ) ) ;
@@ -222,8 +251,7 @@ test(
222251
223252test (
224253 'NumberValidator' ,
225- ( ) =>
226- {
254+ ( ) => {
227255 let v = new NumberValidator ( ) ;
228256 testFailure ( v . validate ( 'test' ) , [ 'must be a number' ] ) ;
229257 testSuccess ( v . validate ( 1 ) ) ;
@@ -246,28 +274,26 @@ test(
246274
247275test (
248276 'RegexValidator' ,
249- ( ) =>
250- {
277+ ( ) => {
251278 let v = new RegexValidator ( 'not valid regex' ) ;
252279 testFailure ( v . validate ( 'test' ) , [ 'not a valid regular expression' ] ) ;
253280
254281 v = new RegexValidator ( { } ) ;
255282 testFailure ( v . validate ( 'test' ) , [ 'not a valid regular expression' ] ) ;
256283
257- v = new RegexValidator ( '/test/' , 'not test' ) ;
258- testFailure ( v . validate ( 'abc' ) , [ 'not test ' ] ) ;
284+ v = new RegexValidator ( '/test/' ) ;
285+ testFailure ( v . validate ( 'abc' ) , [ 'not a valid regular expression ' ] ) ;
259286
260287 v = new RegexValidator ( '/test/' ) ;
261- testFailure ( v . validate ( 'abc' ) , [ 'does not match regular expression' ] ) ;
262- testFailure ( v . validate ( '1' ) , [ 'does not match regular expression' ] ) ;
288+ testFailure ( v . validate ( 'abc' ) , [ 'not a valid regular expression' ] ) ;
289+ testFailure ( v . validate ( '1' ) , [ 'not a valid regular expression' ] ) ;
263290 testSuccess ( v . validate ( 'test' ) ) ;
264291 }
265292) ;
266293
267294test (
268295 'IntegerValidator' ,
269- ( ) =>
270- {
296+ ( ) => {
271297 let v = new IntegerValidator ( ) ;
272298 testFailure ( v . validate ( 'test' ) , [ 'must be a number' ] ) ;
273299 testSuccess ( v . validate ( 1 ) ) ;
@@ -296,8 +322,7 @@ test(
296322
297323test (
298324 'DecimalValidator' ,
299- ( ) =>
300- {
325+ ( ) => {
301326 let v = new DecimalValidator ( ) ;
302327 testFailure ( v . validate ( 'test' ) , [ 'must be a number' ] ) ;
303328 testSuccess ( v . validate ( 1 ) ) ;
@@ -328,8 +353,7 @@ test(
328353
329354test (
330355 'ConfirmationValidator' ,
331- ( ) =>
332- {
356+ ( ) => {
333357 let v = new ConfirmationValidator ( 'field2' ) ;
334358 v . setData ( { 'field1' : 10 } ) ;
335359 testFailure ( v . validate ( v . getData ( ) [ 'field1' ] ) , [ 'value does not match' ] ) ;
0 commit comments