Skip to content

Commit 713bd28

Browse files
author
Lee Richmond
committed
Ensure relationships can have multiple errors
Previously we would clobber pre-existing errors, so only the latest error in the array would show. Now we correctly show everything.
1 parent 2abaca8 commit 713bd28

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/util/validation-errors.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,18 @@ export default class ValidationErrors {
5555
} else {
5656
let relatedAccumulator = {}
5757
this._processResource(relatedAccumulator, meta);
58-
relatedObject.errors = relatedAccumulator
59-
}
6058

59+
// make sure to assign a new error object, instead of mutating
60+
// the existing one, otherwise js frameworks with object tracking
61+
// won't be able to keep up. Validate vue.js when changing this code:
62+
let newErrs = {}
63+
Object.keys(relatedObject.errors).forEach((key) => {
64+
newErrs[key] = relatedObject.errors[key]
65+
});
66+
Object.keys(relatedAccumulator).forEach((key) => {
67+
newErrs[key] = relatedAccumulator[key]
68+
});
69+
relatedObject.errors = newErrs
70+
}
6171
}
6272
}

test/integration/validations-test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ const resetMocks = function() {
5959
}
6060
}
6161
}
62+
},
63+
{
64+
code: 'unprocessable_entity',
65+
status: '422',
66+
title: 'Validation Error',
67+
detail: 'base some error',
68+
meta: {
69+
relationship: {
70+
name: 'books',
71+
type: 'books',
72+
['temp-id']: 'abc1',
73+
relationship: {
74+
name: 'genre',
75+
type: 'genres',
76+
id: '1',
77+
attribute: 'base',
78+
message: 'some error'
79+
}
80+
}
81+
}
6282
}
6383
]
6484
}
@@ -176,8 +196,11 @@ describe('validations', function() {
176196
instance.save({ with: { books: 'genre' }}).then((success) => {
177197
expect(instance.isPersisted()).to.eq(false);
178198
expect(success).to.eq(false);
199+
200+
// note we're validating multiple properties
179201
expect(instance.books[0].genre.errors).to.deep.equal({
180202
name: 'cannot be blank',
203+
base: 'some error'
181204
});
182205
done();
183206
});

0 commit comments

Comments
 (0)