Skip to content

Commit

Permalink
feat: Add support for object validator access as hash
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed Aug 1, 2023
1 parent 4eeada5 commit e654106
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
8 changes: 7 additions & 1 deletion addon/decorators/object-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ function objectValidator(Class) {
};

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

pushErrors(errors) {
let errorsObj = {};
for (let attribute in errors) {
let messages = errors[attribute];
if (!errorsObj[attribute]) {
errorsObj[attribute] = A([]);
}
errorsObj[attribute].push({ message: messages.flat() });
set(this, 'errors', {
...this.errors,
...errorsObj,
_errors: [...this.errors._errors, { attribute, message: messages.flat() }],
});
}
Expand Down
56 changes: 54 additions & 2 deletions tests/unit/object-validator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,36 @@ import Messages from 'ember-model-validator/messages/pt-br';

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { tracked } from '@glimmer/tracking';

module('Unit | object-validator', function (hooks) {
setupTest(hooks);

test('Validator on Simple Object', function (assert) {
test('Validator on Simple POJO', function (assert) {
assert.expect(2);

interface ValidatorObjectClass extends ValidatedObject, EmberObject {}
@objectValidator
class ValidatorObjectPojo {
@tracked name = '';
_locale = 'pt-br';
validations = {
name: {
presence: true,
},
};
}

interface ValidatorObjectPojo extends ValidatedObject, EmberObject {}

const subject = new ValidatorObjectPojo();
subject.name = '';

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

test('Validator on Simple EmberObject', function (assert) {
assert.expect(2);

@objectValidator
class ValidatorObjectClass extends EmberObject {
Expand All @@ -23,6 +45,9 @@ module('Unit | object-validator', function (hooks) {
},
};
}

interface ValidatorObjectClass extends ValidatedObject, EmberObject {}

const subject = ValidatorObjectClass.create();
subject.name = '';

Expand Down Expand Up @@ -55,4 +80,31 @@ module('Unit | object-validator', function (hooks) {
assert.true(subject.validate());
assert.strictEqual(subject.get('errors').errorsFor('name').length, 0);
});

test('Access objects as a hash', function (assert) {
assert.expect(5);

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.errors['name'].mapBy('message')[0][0], Messages.presenceMessage);

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

0 comments on commit e654106

Please sign in to comment.