co.validation 1.0.0-RC.1 → 1.0.0-RC.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -184,6 +184,238 @@ describe('createObjectValidation', () => {
184
184
  }
185
185
  });
186
186
  });
187
+ it('supports property validation for valid object and only custom prop validation and omits extra properties', () => {
188
+ const validatedObject = {
189
+ login: 'login',
190
+ password: 'password',
191
+ 5: true,
192
+ 123: true,
193
+ '2020-03-05': {
194
+ hz: 1
195
+ },
196
+ '2020-03-a2': 'skip'
197
+ };
198
+ const validationResult = (0, _.createValidation)({
199
+ type: 'object',
200
+ customPropRules: [{
201
+ nameRule: {
202
+ type: 'number'
203
+ },
204
+ valueRule: {
205
+ type: 'boolean'
206
+ }
207
+ }, {
208
+ nameRule: {
209
+ type: 'regex',
210
+ regex: /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
211
+ },
212
+ valueRule: {
213
+ type: 'object'
214
+ }
215
+ }]
216
+ })(validatedObject);
217
+ expect(validationResult.errors).toEqual(undefined);
218
+ expect(validationResult.data).toEqual({
219
+ 5: true,
220
+ 123: true,
221
+ '2020-03-05': {
222
+ hz: 1
223
+ }
224
+ });
225
+ });
226
+ it('is beatiful', () => {
227
+ const validatedObject = {
228
+ weekly: {
229
+ mon: {
230
+ intervals: [{
231
+ from: '09:00',
232
+ to: '12:00'
233
+ }, {
234
+ from: '13:00',
235
+ to: '21:00'
236
+ }]
237
+ },
238
+ tue: {
239
+ intervals: [{
240
+ from: '09:00',
241
+ to: '12:00'
242
+ }, {
243
+ from: '13:00',
244
+ to: '21:00'
245
+ }]
246
+ },
247
+ wed: {
248
+ intervals: [{
249
+ from: '09:00',
250
+ to: '12:00'
251
+ }, {
252
+ from: '13:00',
253
+ to: '21:00'
254
+ }]
255
+ },
256
+ thu: {
257
+ intervals: [{
258
+ from: '09:00',
259
+ to: '12:00'
260
+ }, {
261
+ from: '13:00',
262
+ to: '21:00'
263
+ }]
264
+ },
265
+ fri: {
266
+ intervals: [{
267
+ from: '09:00',
268
+ to: '12:00'
269
+ }, {
270
+ from: '13:00',
271
+ to: '21:00'
272
+ }]
273
+ },
274
+ sat: {
275
+ intervals: [{
276
+ from: '09:00',
277
+ to: '12:00'
278
+ }, {
279
+ from: '13:00',
280
+ to: '17:00'
281
+ }]
282
+ },
283
+ sun: {
284
+ intervals: []
285
+ }
286
+ }
287
+ };
288
+ const daySchedule = {
289
+ type: 'all',
290
+ validators: [{
291
+ type: 'required'
292
+ }, {
293
+ type: 'object',
294
+ rules: {
295
+ intervals: {
296
+ type: 'all',
297
+ validators: [{
298
+ type: 'required'
299
+ }, {
300
+ type: 'array',
301
+ itemRule: {
302
+ type: 'all',
303
+ validators: [{
304
+ type: 'required'
305
+ }, {
306
+ type: 'object',
307
+ rules: {
308
+ from: {
309
+ type: 'all',
310
+ validators: [{
311
+ type: 'required'
312
+ }, {
313
+ type: 'regex',
314
+ regex: /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/
315
+ }]
316
+ },
317
+ to: {
318
+ type: 'all',
319
+ validators: [{
320
+ type: 'required'
321
+ }, {
322
+ type: 'regex',
323
+ regex: /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/
324
+ }]
325
+ }
326
+ }
327
+ }]
328
+ }
329
+ }]
330
+ }
331
+ }
332
+ }]
333
+ };
334
+ const validationResult = (0, _.createValidation)({
335
+ type: 'object',
336
+ rules: {
337
+ weekly: {
338
+ type: 'all',
339
+ validators: [{
340
+ type: 'required'
341
+ }, {
342
+ type: 'object',
343
+ rules: {
344
+ mon: daySchedule,
345
+ tue: daySchedule,
346
+ wed: daySchedule,
347
+ thu: daySchedule,
348
+ fri: daySchedule,
349
+ sat: daySchedule,
350
+ sun: daySchedule
351
+ }
352
+ }]
353
+ }
354
+ }
355
+ })(validatedObject);
356
+ expect(validationResult.errors).toEqual(undefined);
357
+ expect(validationResult.data).toEqual({
358
+ weekly: {
359
+ mon: {
360
+ intervals: [{
361
+ from: '09:00',
362
+ to: '12:00'
363
+ }, {
364
+ from: '13:00',
365
+ to: '21:00'
366
+ }]
367
+ },
368
+ tue: {
369
+ intervals: [{
370
+ from: '09:00',
371
+ to: '12:00'
372
+ }, {
373
+ from: '13:00',
374
+ to: '21:00'
375
+ }]
376
+ },
377
+ wed: {
378
+ intervals: [{
379
+ from: '09:00',
380
+ to: '12:00'
381
+ }, {
382
+ from: '13:00',
383
+ to: '21:00'
384
+ }]
385
+ },
386
+ thu: {
387
+ intervals: [{
388
+ from: '09:00',
389
+ to: '12:00'
390
+ }, {
391
+ from: '13:00',
392
+ to: '21:00'
393
+ }]
394
+ },
395
+ fri: {
396
+ intervals: [{
397
+ from: '09:00',
398
+ to: '12:00'
399
+ }, {
400
+ from: '13:00',
401
+ to: '21:00'
402
+ }]
403
+ },
404
+ sat: {
405
+ intervals: [{
406
+ from: '09:00',
407
+ to: '12:00'
408
+ }, {
409
+ from: '13:00',
410
+ to: '17:00'
411
+ }]
412
+ },
413
+ sun: {
414
+ intervals: []
415
+ }
416
+ }
417
+ });
418
+ });
187
419
  it('supports property validation for invalidvalid object and custom prop validation and omits extra properties', () => {
188
420
  const validatedObject = {
189
421
  login: 'login',
@@ -291,7 +291,7 @@ describe('createValidation', () => {
291
291
  type: 'number',
292
292
  getMessage: () => 'Number'
293
293
  })('1234')).toEqual({
294
- data: '1234',
294
+ data: 1234,
295
295
  errors: undefined
296
296
  });
297
297
  expect((0, _.createValidation)({
@@ -345,6 +345,13 @@ describe('createValidation', () => {
345
345
  });
346
346
  });
347
347
  it('supports boolean', () => {
348
+ expect((0, _.createValidation)({
349
+ type: 'boolean',
350
+ getMessage: () => 'Boolean'
351
+ })(undefined)).toEqual({
352
+ data: undefined,
353
+ errors: undefined
354
+ });
348
355
  expect((0, _.createValidation)({
349
356
  type: 'boolean',
350
357
  getMessage: () => 'Boolean'
@@ -556,8 +563,8 @@ describe('createValidation', () => {
556
563
  expect((0, _.createValidation)({
557
564
  type: 'all',
558
565
  validators: [{
559
- type: 'number',
560
- getMessage: () => 'Number'
566
+ type: 'email',
567
+ getMessage: () => 'Email'
561
568
  }, {
562
569
  type: 'maxLength',
563
570
  max: 1,
@@ -565,7 +572,7 @@ describe('createValidation', () => {
565
572
  }]
566
573
  })('xz')).toEqual({
567
574
  data: undefined,
568
- errors: ['Number', 'MaxLength']
575
+ errors: ['Email', 'MaxLength']
569
576
  });
570
577
  expect((0, _.createValidation)({
571
578
  type: 'all',
@@ -573,19 +580,19 @@ describe('createValidation', () => {
573
580
  type: 'number',
574
581
  getMessage: () => 'Number'
575
582
  }, {
576
- type: 'maxLength',
577
- max: 1,
578
- getMessage: () => 'MaxLength'
583
+ type: 'maxValue',
584
+ max: 11,
585
+ getMessage: () => 'MaxValue'
579
586
  }]
580
587
  })('12')).toEqual({
581
588
  data: undefined,
582
- errors: ['MaxLength']
589
+ errors: ['MaxValue']
583
590
  });
584
591
  expect((0, _.createValidation)({
585
592
  type: 'all',
586
593
  validators: [{
587
- type: 'number',
588
- getMessage: () => 'Number'
594
+ type: 'email',
595
+ getMessage: () => 'Email'
589
596
  }, {
590
597
  type: 'maxLength',
591
598
  max: 1,
@@ -593,7 +600,7 @@ describe('createValidation', () => {
593
600
  }]
594
601
  })('x')).toEqual({
595
602
  data: undefined,
596
- errors: ['Number']
603
+ errors: ['Email']
597
604
  });
598
605
  expect((0, _.createValidation)({
599
606
  type: 'all',
@@ -601,12 +608,12 @@ describe('createValidation', () => {
601
608
  type: 'number',
602
609
  getMessage: () => 'Number'
603
610
  }, {
604
- type: 'maxLength',
611
+ type: 'maxValue',
605
612
  max: 1,
606
- getMessage: () => 'MaxLength'
613
+ getMessage: () => 'MaxValue'
607
614
  }]
608
615
  })('1')).toEqual({
609
- data: '1',
616
+ data: 1,
610
617
  errors: undefined
611
618
  });
612
619
  });
@@ -37,6 +37,14 @@ const registrationFormValidation = (0, _.createValidation)({
37
37
  type: 'email'
38
38
  }]
39
39
  },
40
+ age: {
41
+ type: 'all',
42
+ validators: [{
43
+ type: 'required'
44
+ }, {
45
+ type: 'number'
46
+ }]
47
+ },
40
48
  hobbies: {
41
49
  type: 'array',
42
50
  itemRule: {
@@ -82,7 +90,8 @@ describe('registrationFormValidation', () => {
82
90
  expect(validationResult.errors).toEqual({
83
91
  firstName: ['Required'],
84
92
  lastName: ['Required'],
85
- email: ['Required']
93
+ email: ['Required'],
94
+ age: ['Required']
86
95
  });
87
96
  expect(validationResult.data).toEqual(undefined);
88
97
  });
@@ -94,7 +103,8 @@ describe('registrationFormValidation', () => {
94
103
  expect(validationResult.errors).toEqual({
95
104
  firstName: ['Required'],
96
105
  lastName: ['Required'],
97
- email: ['Required']
106
+ email: ['Required'],
107
+ age: ['Required']
98
108
  });
99
109
  expect(validationResult.data).toEqual(validatedObject);
100
110
  });
@@ -125,6 +135,7 @@ describe('registrationFormValidation', () => {
125
135
  firstName: 'firstName',
126
136
  lastName: 'lastName',
127
137
  email: 'test@mail.com',
138
+ age: '12',
128
139
  hobbies: [1, {
129
140
  title: 'title'
130
141
  }, {
@@ -166,6 +177,7 @@ describe('registrationFormValidation', () => {
166
177
  firstName: 'firstName',
167
178
  lastName: 'lastName',
168
179
  email: 'test@mail.com',
180
+ age: 12,
169
181
  hobbies: [{
170
182
  title: 'title'
171
183
  }, {
@@ -190,7 +190,7 @@ const createObjectValidation = rule => {
190
190
  }
191
191
 
192
192
  return {
193
- data: fieldValidations ? Object.getOwnPropertyNames(resultData).length > 0 ? resultData : undefined : object,
193
+ data: fieldValidations || customFieldValidations ? Object.getOwnPropertyNames(resultData).length > 0 ? resultData : undefined : object,
194
194
  errors: Object.getOwnPropertyNames(resultErrors).length > 0 ? resultErrors : undefined
195
195
  };
196
196
  };
package/lib/operators.js CHANGED
@@ -57,6 +57,8 @@ const all = (...validators) => {
57
57
  } else {
58
58
  validationErrors.push(currentResult.errors);
59
59
  }
60
+ } else {
61
+ value = currentResult.data;
60
62
  }
61
63
 
62
64
  return validationErrors;
package/lib/validators.js CHANGED
@@ -59,7 +59,7 @@ const number = ({
59
59
  }) => value => value && isNaN(Number(value)) ? {
60
60
  errors: (0, _messageProcessor.default)(getMessage, 'Must be a number')
61
61
  } : {
62
- data: value
62
+ data: Number(value)
63
63
  };
64
64
 
65
65
  exports.number = number;
@@ -118,7 +118,7 @@ exports.string = string;
118
118
 
119
119
  const boolean = ({
120
120
  getMessage
121
- }) => value => typeof value === 'boolean' || typeof value === 'string' && {
121
+ }) => value => value === undefined || value === null || typeof value === 'boolean' || typeof value === 'string' && {
122
122
  'true': true,
123
123
  'false': true
124
124
  }[value.toLowerCase()] ? {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co.validation",
3
- "version": "1.0.0-RC.1",
3
+ "version": "1.0.0-RC.5",
4
4
  "description": "JSON based fluent validation for browser, node.js, redux-form and anything javascript compatible.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {