Skip to content

Commit 2abaca8

Browse files
author
Lee Richmond
committed
camelize error keys by default
This only makes sense - the error keys should match the attribute names
1 parent 38310ba commit 2abaca8

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/util/validation-errors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Model from '../model';
2+
import { camelize } from './string'
23

34
export default class ValidationErrors {
45
model: Model;
@@ -32,7 +33,13 @@ export default class ValidationErrors {
3233
}
3334

3435
private _processResource(errorsAccumulator: object, meta: Object) {
35-
errorsAccumulator[meta['attribute']] = meta['message'];
36+
let attribute = meta['attribute']
37+
38+
if (this.model.klass.camelizeKeys) {
39+
attribute = camelize(attribute)
40+
}
41+
42+
errorsAccumulator[attribute] = meta['message'];
3643
}
3744

3845
private _processRelationship(model: Model, meta: Object) {

test/integration/validations-test.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { expect, sinon, fetchMock } from '../test-helper';
22
import { Author, Book, Genre } from '../fixtures';
33
import uuid from '../../src/util/uuid';
44

5-
let serverResponse;
6-
75
const resetMocks = function() {
86
fetchMock.restore();
97

@@ -66,10 +64,6 @@ const resetMocks = function() {
6664
}
6765
}
6866
});
69-
70-
fetchMock.post('http://example.com/api/v1/people', function(url, payload) {
71-
return serverResponse;
72-
});
7367
}
7468

7569
let instance;
@@ -97,19 +91,50 @@ describe('validations', function() {
9791
uuid.generate['restore']();
9892
});
9993

100-
// todo on next save, remove errs
10194
it('applies errors to the instance', function(done) {
10295
instance.save({ with: { books: 'genre' }}).then((success) => {
10396
expect(instance.isPersisted()).to.eq(false);
10497
expect(success).to.eq(false);
10598
expect(instance.errors).to.deep.equal({
106-
first_name: 'cannot be blank',
107-
last_name: 'cannot be blank'
99+
firstName: 'cannot be blank',
100+
lastName: 'cannot be blank'
108101
});
109102
done();
110103
});
111104
});
112105

106+
describe('when camelizeKeys is false', function() {
107+
beforeEach(function() {
108+
instance.klass.camelizeKeys = false
109+
});
110+
111+
afterEach(function() {
112+
instance.klass.camelizeKeys = true
113+
});
114+
115+
it('does not camelize the error keys', function() {
116+
instance.save({ with: { books: 'genre' }}).then((success) => {
117+
expect(instance.errors).to.deep.equal({
118+
first_name: 'cannot be blank',
119+
last_name: 'cannot be blank'
120+
});
121+
});
122+
});
123+
});
124+
125+
it('clears errors on save', function(done) {
126+
fetchMock.restore()
127+
fetchMock.mock({
128+
matcher: '*',
129+
response: { data: { id: '1', type: 'employees'} }
130+
});
131+
instance.errors = { foo: 'bar' }
132+
instance.save().then(() => {
133+
expect(instance.errors).to.deep.eq({})
134+
done()
135+
});
136+
});
137+
113138
it('instantiates a new error object instance after save', function(done) {
114139
let originalErrors = instance.errors = {foo: 'bar'};
115140
let result = instance.save({ with: { books: 'genre' }});

0 commit comments

Comments
 (0)