Skip to content

Commit 058dad7

Browse files
author
Andreas Krummsdorf
committed
feat: Save nested error messages in correct order
ref: flatiron/revalidator#98
1 parent 6ffd99a commit 058dad7

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

lib/revalidator.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@
606606

607607
});
608608

609-
nestedErrors.unshift(0, 0);
609+
nestedErrors.unshift(errors.length, 0);
610610
Array.prototype.splice.apply(errors, nestedErrors);
611611
}
612612

@@ -641,7 +641,7 @@
641641
nestedErrors.forEach(function (e) {
642642
e.property = property + '.' + e.property;
643643
});
644-
nestedErrors.unshift(0, 0);
644+
nestedErrors.unshift(errors.length, 0);
645645
Array.prototype.splice.apply(errors, nestedErrors);
646646
}
647647
break;

test/tests.spec.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ describe('Validator', function () {
388388
expected: 'string',
389389
actual: 'number',
390390
message: 'must be of string type',
391-
property: 'arr.2'
391+
property: 'arr.0'
392392
},
393393
{
394394
attribute: 'type',
@@ -402,7 +402,7 @@ describe('Validator', function () {
402402
expected: 'string',
403403
actual: 'number',
404404
message: 'must be of string type',
405-
property: 'arr.0'
405+
property: 'arr.2'
406406
}
407407
]);
408408

@@ -477,7 +477,7 @@ describe('Validator', function () {
477477
expect(result.errors).toEqual([
478478
{
479479
attribute: 'type',
480-
property: '2',
480+
property: '0',
481481
expected: 'string',
482482
actual: 'number',
483483
message: 'must be of string type'
@@ -491,7 +491,7 @@ describe('Validator', function () {
491491
},
492492
{
493493
attribute: 'type',
494-
property: '0',
494+
property: '2',
495495
expected: 'string',
496496
actual: 'number',
497497
message: 'must be of string type'
@@ -514,8 +514,8 @@ describe('Validator', function () {
514514
var result = val.validate({arr: ['2', 1, '3', 4, '5']}, schema);
515515
expect(result.valid).toBeFalsy();
516516
expect(result.errors.length).toBe(2);
517-
expect(result.errors[0].property).toBe('arr.3');
518-
expect(result.errors[1].property).toBe('arr.1');
517+
expect(result.errors[0].property).toBe('arr.1');
518+
expect(result.errors[1].property).toBe('arr.3');
519519
});
520520

521521
it('should return the array index in the error message when array in array', function () {
@@ -536,8 +536,8 @@ describe('Validator', function () {
536536
var result = val.validate({arr: [['2', 1, '3'], [4, '5']]}, schema);
537537
expect(result.valid).toBeFalsy();
538538
expect(result.errors.length).toBe(2);
539-
expect(result.errors[0].property).toBe('arr.1.0');
540-
expect(result.errors[1].property).toBe('arr.0.1');
539+
expect(result.errors[0].property).toBe('arr.0.1');
540+
expect(result.errors[1].property).toBe('arr.1.0');
541541
});
542542

543543
it('should return the array index in the error message when object in array', function () {
@@ -684,13 +684,13 @@ describe('Validator', function () {
684684
}
685685
}, schema);
686686
expect(result.valid).toBeFalsy();
687-
expect(result.errors[1].property).toBe('person.addresses.1.street');
688-
expect(result.errors[0].property).toBe('person.addresses.2.users.1');
687+
expect(result.errors[0].property).toBe('person.addresses.1.street');
688+
expect(result.errors[1].property).toBe('person.addresses.2.users.1');
689689

690690
result = val.validate({arrayWithArrays: [[1, 2, 3], [4, '5', 6], ['7', 8, 9]]}, schema);
691691
expect(result.valid).toBeFalsy();
692-
expect(result.errors[0].property).toBe('arrayWithArrays.2.0');
693-
expect(result.errors[1].property).toBe('arrayWithArrays.1.1');
692+
expect(result.errors[0].property).toBe('arrayWithArrays.1.1');
693+
expect(result.errors[1].property).toBe('arrayWithArrays.2.0');
694694

695695
});
696696

@@ -761,8 +761,8 @@ describe('Validator', function () {
761761
codes: [1, 2, 3]
762762
}, {name: 'wayne', codes: [1, 2, '3', 4]}], schema);
763763
expect(result.valid).toBeFalsy();
764-
expect(result.errors[1].property).toBe('0.codes.1');
765-
expect(result.errors[0].property).toBe('2.codes.2');
764+
expect(result.errors[0].property).toBe('0.codes.1');
765+
expect(result.errors[1].property).toBe('2.codes.2');
766766

767767
result = val.validate([{
768768
name: 'wayne',
@@ -778,14 +778,14 @@ describe('Validator', function () {
778778
}
779779
], schema);
780780
expect(result.valid).toBeFalsy();
781-
expect(result.errors[1].property).toBe('0.addresses.1.street');
782-
expect(result.errors[0].property).toBe('0.addresses.2.users.1');
781+
expect(result.errors[0].property).toBe('0.addresses.1.street');
782+
expect(result.errors[1].property).toBe('0.addresses.2.users.1');
783783

784784
result = val.validate([{arrayWithArrays: [[1, 2, 3], [4, '5', 6], ['7', 8, 9]]}, {arrayWithArrays: [[1, 2, 3], [4, 5, 6], ['7', 8, 9]]}], schema);
785785
expect(result.valid).toBeFalsy();
786+
expect(result.errors[0].property).toBe('0.arrayWithArrays.1.1');
786787
expect(result.errors[1].property).toBe('0.arrayWithArrays.2.0');
787-
expect(result.errors[2].property).toBe('0.arrayWithArrays.1.1');
788-
expect(result.errors[0].property).toBe('1.arrayWithArrays.2.0');
788+
expect(result.errors[2].property).toBe('1.arrayWithArrays.2.0');
789789

790790
});
791791

@@ -802,8 +802,8 @@ describe('Validator', function () {
802802
var result = val.validate([['2', 1, '3'], [4, '5']], schema);
803803
expect(result.valid).toBeFalsy();
804804
expect(result.errors.length).toBe(2);
805-
expect(result.errors[1].property).toBe('0.1');
806-
expect(result.errors[0].property).toBe('1.0');
805+
expect(result.errors[0].property).toBe('0.1');
806+
expect(result.errors[1].property).toBe('1.0');
807807
});
808808
});
809809

@@ -1129,9 +1129,9 @@ describe('Validator', function () {
11291129
result = val.validate(data, schema, {strictRequired: true});
11301130

11311131
expect(result.valid).toBeFalsy();
1132-
expect(result.errors[0].property).toBe('names.3');
1132+
expect(result.errors[0].property).toBe('name');
11331133
expect(result.errors[0].message).toBe('is required');
1134-
expect(result.errors[1].property).toBe('name');
1134+
expect(result.errors[1].property).toBe('names.3');
11351135
expect(result.errors[1].message).toBe('is required');
11361136
});
11371137

0 commit comments

Comments
 (0)