Skip to content

Commit

Permalink
Merge pull request #303 from esbanarango/esbanarango/add-support-for-…
Browse files Browse the repository at this point in the history
…object-validator-access-as-hash

feat: Add support for object validator access as hash
  • Loading branch information
esbanarango authored Aug 1, 2023
2 parents 4eeada5 + 8d689a0 commit f526fbb
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 20 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^2.8.1",
"@embroider/test-setup": "^2.0.2",
"@glimmer/component": "^1.1.2",
Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/adapters/application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default class ApplicationAdapter extends JSONAPIAdapter {}
6 changes: 5 additions & 1 deletion tests/dummy/app/models/async-model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Model from '@ember-data/model';
import { belongsTo } from '@ember-data/model';
import type FakeModel from './fake-model';

export default class AsyncModel extends Model {}
export default class AsyncModel extends Model {
@belongsTo('fake-model', { async: false, inverse: 'asyncModel' }) declare fakeModel: FakeModel;
}

declare module 'ember-data/types/registries/model' {
export default interface ModelRegistry {
Expand Down
15 changes: 4 additions & 11 deletions tests/dummy/app/models/fake-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import Model, {
attr,
belongsTo,
hasMany,
type AsyncBelongsTo,
type AsyncHasMany,
type SyncHasMany,
} from '@ember-data/model';
import Model, { attr, belongsTo, hasMany, type AsyncBelongsTo, type AsyncHasMany } from '@ember-data/model';

import { modelValidator, type ValidationsConfig, type ValidatedModel } from 'ember-model-validator';
import type AsyncModel from './async-model';
Expand Down Expand Up @@ -55,9 +48,9 @@ class FakeModel extends Model {
@attr() declare images: any;
@attr('string') declare condType: string;

@hasMany('other-model') declare otherFakes: AsyncHasMany<OtherModel>;
@belongsTo('other-model') declare otherFake: AsyncBelongsTo<OtherModel>;
@belongsTo('async-model', { async: true }) declare asyncModel: SyncHasMany<AsyncModel>;
@hasMany('other-model', { async: true, inverse: 'fakeModel' }) declare otherFakes: AsyncHasMany<OtherModel>;
@belongsTo('other-model', { async: true, inverse: null }) declare otherFake: AsyncBelongsTo<OtherModel>;
@belongsTo('async-model', { async: true, inverse: 'fakeModel' }) declare asyncModel: AsyncModel;

@attr('date', {
defaultValue() {
Expand Down
5 changes: 3 additions & 2 deletions tests/dummy/app/models/other-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Model, { attr } from '@ember-data/model';

import Model, { attr, belongsTo, type AsyncBelongsTo } from '@ember-data/model';
import { modelValidator, type ValidationsConfig, type ValidatedModel } from 'ember-model-validator';

interface OtherModel extends ValidatedModel, Model {}

@modelValidator
class OtherModel extends Model {
@belongsTo('fake-model', { async: true, inverse: 'otherFakes' }) declare fakeModel: AsyncBelongsTo<OtherModel>;

@attr('string') declare name: string;

validations: ValidationsConfig = {
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/models/fake-model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,18 @@ module('Unit | Model | fake-model', function (hooks) {

module('Relations validations', function () {
module('`hasMany` relations', function () {
test('it validates the relations specified on `validations.relations`', function (assert) {
test('it validates the relations specified on `validations.relations`', async function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const model = store.createRecord('fake-model', { email: 'thiisagoo@email.con', name: 'Jose Rene Higuita' });

let otherFakes = model.get('otherFakes');
let otherFakes = await model.otherFakes;
const otherFake = store.createRecord('other-model');
otherFakes.pushObject(otherFake);
if (otherFakes.push) {
otherFakes.push(otherFake);
} else {
otherFakes.pushObject(otherFake);
}

assert.false(model.validate({ only: ['otherFakes'] }));
});
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);
});
});
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,13 @@
dependencies:
ember-cli-babel "^7.26.6"

"@ember/string@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@ember/string/-/string-3.1.1.tgz#0a5ac0d1e4925259e41d5c8d55ef616117d47ff0"
integrity sha512-UbXJ+k3QOrYN4SRPHgXCqYIJ+yWWUg1+vr0H4DhdQPTy8LJfyqwZ2tc5uqpSSnEXE+/1KopHBE5J8GDagAg5cg==
dependencies:
ember-cli-babel "^7.26.6"

"@ember/test-helpers@*", "@ember/test-helpers@^2.8.1":
version "2.9.3"
resolved "https://registry.yarnpkg.com/@ember/test-helpers/-/test-helpers-2.9.3.tgz#c2a9d6ab1c367af92cf1a334f97eb19b8e06e6e1"
Expand Down

0 comments on commit f526fbb

Please sign in to comment.