Skip to content

Commit

Permalink
Fix ds-error validator for handling nested objects (#111)
Browse files Browse the repository at this point in the history
This change broke the ds-error validator behaviour when handling nested objects f829984
  • Loading branch information
herzzanu authored Jan 6, 2022
1 parent beac648 commit 7382dcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion addon/ds-error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import validationError from 'ember-validators/utils/validation-error';
import { get } from '@ember/object';

/**
* @class DS Error
Expand All @@ -15,7 +16,7 @@ import validationError from 'ember-validators/utils/validation-error';
export default function validateDsError(value, options, model, attribute) {
let { path, key } = getPathAndKey(attribute);

let errors = model[path];
let errors = get(model, path);

if (errors && errors.has && errors.has(key)) {
let errorsFor = errors.errorsFor(key);
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/validators/ds-error-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ test('it works', function (assert) {
assert.strictEqual(processResult(result), 'Username is not unique');
});

test('it works with nested objects', function (assert) {
assert.expect(2);

model = EmberObject.create({
foo: {
errors: new DSErrors(),
username: null,
},
});

result = validate(undefined, undefined, model, 'foo.username');
assert.true(processResult(result));

model.get('foo.errors').set('username', 'Username is not unique');

result = validate(undefined, undefined, model, 'foo.username');
assert.strictEqual(processResult(result), 'Username is not unique');
});

test('gets last message', function (assert) {
assert.expect(2);

Expand Down

0 comments on commit 7382dcb

Please sign in to comment.