Skip to content

Commit

Permalink
Merge pull request #224 from esbanarango/esbanarango/fix-object-valid…
Browse files Browse the repository at this point in the history
…ator

Fix object validator
  • Loading branch information
esbanarango authored Jan 3, 2023
2 parents 25bdb85 + c78972d commit fa25b3b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
16 changes: 9 additions & 7 deletions addon/decorators/object-validator.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import { A } from '@ember/array';
import { set } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import Validator from 'ember-model-validator/decorators/core-validator';

function objectValidator(Class) {
@Validator
class ObjectValidator extends Class {
@tracked
errors = {
_errors: A(),

addObject(error) {
this._errors.pushObject(error);
},
errorsFor(attribute) {
return A(this._errors.filterBy('attribute', attribute));
return A(this._errors.filter((error) => error.attribute === attribute));
},
};

clearErrors() {
set(this, 'errors._errors', A());
set(this, 'errors', { ...this.errors, _errors: A() });
set(this, 'validationErrors', {});
set(this, 'isValidNow', true);
}

pushErrors(errors) {
for (let attribute in errors) {
let messages = errors[attribute];
this.errors.addObject({ attribute, message: messages.flat() });
set(this, 'errors', {
...this.errors,
_errors: [...this.errors._errors, { attribute, message: messages.flat() }],
});
}
}

errorsFor(attribute) {
return this.errors.filterBy('attribute', attribute);
return this.errors.errorsFor(attribute);
}

_modelRelations() {}
Expand Down
2 changes: 1 addition & 1 deletion addon/initializers/register-version.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';

export function initialize() {
Ember.libraries.register('Ember Model Validator', '4.3.0');
Ember.libraries.register('Ember Model Validator', '4.4.0');
}

export default {
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/object-validator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ module('Unit | object-validator', function (hooks) {
assert.false(subject.validate());
assert.strictEqual(subject.get('errors').errorsFor('name').mapBy('message')[0][0], Messages.presenceMessage);
});

test('Re computed errors', function (assert) {
assert.expect(4);

interface ValidatorObjectClass extends ValidatedObject, EmberObject {}

@objectValidator
class ValidatorObjectClass extends EmberObject {
name = '';
_locale = 'pt-br';
validations = {
name: {
presence: true,
},
};
}
const subject = ValidatorObjectClass.create();
subject.name = '';

assert.false(subject.validate());
assert.strictEqual(subject.get('errors').errorsFor('name').mapBy('message')[0][0], Messages.presenceMessage);

subject.name = 'test';
assert.true(subject.validate());
assert.strictEqual(subject.get('errors').errorsFor('name').length, 0);
});
});

0 comments on commit fa25b3b

Please sign in to comment.