Skip to content

Commit 0a47f90

Browse files
committed
feat: [esbanarangoGH-356] allow-to-use-if-with-realtions
1 parent b3cdf52 commit 0a47f90

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

addon/decorators/core-validator.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,17 @@ function coreValidator(constructor) {
470470
}
471471
}
472472
_validateRelations(property, validation) {
473-
if (validation.relations.indexOf('hasMany') !== -1) {
473+
const relationType = Array.isArray(validation.relations) ? validation.relations : validation.relations.value;
474+
475+
if (relationType.indexOf('hasMany') !== -1) {
474476
if (get(this, `${property}.content`)) {
475477
get(this, `${property}.content`).forEach((objRelation) => {
476478
if (!objRelation.validate()) {
477479
set(this, 'isValidNow', false);
478480
}
479481
});
480482
}
481-
} else if (validation.relations.indexOf('belongsTo') !== -1) {
483+
} else if (relationType.indexOf('belongsTo') !== -1) {
482484
if (get(this, `${property}.content`) && !get(this, `${property}.content`).validate()) {
483485
set(this, 'isValidNow', false);
484486
}

tests/dummy/app/models/fake-model.ts

+19
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class FakeModel extends Model {
5252
@belongsTo('other-model', { async: true, inverse: null }) declare otherFake: AsyncBelongsTo<OtherModel>;
5353
@belongsTo('async-model', { async: true, inverse: 'fakeModel' }) declare asyncModel: AsyncModel;
5454

55+
@belongsTo('other-model', { async: true, inverse: null }) declare ifOtherFake: AsyncBelongsTo<OtherModel>;
56+
@hasMany('other-model', { async: true, inverse: null }) declare ifOtherFakes: AsyncHasMany<OtherModel>;
57+
5558
@attr('date', {
5659
defaultValue() {
5760
return new Date();
@@ -235,6 +238,22 @@ class FakeModel extends Model {
235238
presence: true,
236239
relations: ['belongsTo'],
237240
},
241+
ifOtherFake: {
242+
relations: {
243+
if: function (key: string, value: any, _this: FakeModel) {
244+
return 'gallery' === _this.get('condType');
245+
},
246+
value: ['belongsTo'],
247+
},
248+
},
249+
ifOtherFakes: {
250+
relations: {
251+
if: function (key: string, value: any, _this: FakeModel) {
252+
return 'gallery' === _this.get('condType');
253+
},
254+
value: ['hasMany'],
255+
},
256+
},
238257
otherCustomValidation: {
239258
custom: {
240259
validation: function (key: string, value: any) {

tests/unit/models/fake-model-test.js

+64
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,44 @@ module('Unit | Model | fake-model', function (hooks) {
619619

620620
assert.false(model.validate({ only: ['otherFakes'] }));
621621
});
622+
623+
module('conditional option', function () {
624+
test('it validates only if `if function` returns true', async function (assert) {
625+
assert.expect(1);
626+
627+
const store = this.owner.lookup('service:store');
628+
const model = store.createRecord('fake-model', { condType: 'gallery' });
629+
630+
let ifOtherFakes = await model.ifOtherFakes;
631+
const otherFake = store.createRecord('other-model');
632+
if (ifOtherFakes.push) {
633+
ifOtherFakes.push(otherFake);
634+
} else {
635+
ifOtherFakes.pushObject(otherFake);
636+
}
637+
638+
assert.false(model.validate({ only: ['ifOtherFakes'] }));
639+
});
640+
641+
test('skips validation if `if function` returns false', async function (assert) {
642+
assert.expect(1);
643+
644+
const store = this.owner.lookup('service:store');
645+
const model = store.createRecord('fake-model', { condType: 'chancuncha' });
646+
647+
let ifOtherFakes = await model.ifOtherFakes;
648+
const otherFake = store.createRecord('other-model');
649+
if (ifOtherFakes.push) {
650+
ifOtherFakes.push(otherFake);
651+
} else {
652+
ifOtherFakes.pushObject(otherFake);
653+
}
654+
655+
model.validate();
656+
657+
assert.strictEqual(model.get('errors').errorsFor('ifOtherFakes').length, 0);
658+
});
659+
});
622660
});
623661

624662
module('`belongsTo` relations', function () {
@@ -635,6 +673,32 @@ module('Unit | Model | fake-model', function (hooks) {
635673
Messages.presenceMessage
636674
);
637675
});
676+
677+
module('conditional option', function () {
678+
test('it validates only if `if function` returns true', async function (assert) {
679+
assert.expect(1);
680+
681+
const store = this.owner.lookup('service:store');
682+
const model = store.createRecord('fake-model', { condType: 'gallery' });
683+
684+
model.set('ifOtherFake', store.createRecord('other-model'));
685+
686+
assert.false(model.validate({ only: ['ifOtherFake'] }));
687+
});
688+
689+
test('skips validation if `if function` returns false', async function (assert) {
690+
assert.expect(1);
691+
692+
const store = this.owner.lookup('service:store');
693+
const model = store.createRecord('fake-model', { condType: 'chancuncha' });
694+
695+
model.set('ifOtherFake', store.createRecord('other-model'));
696+
697+
model.validate();
698+
699+
assert.strictEqual(model.get('errors').errorsFor('ifOtherFake').length, 0);
700+
});
701+
});
638702
});
639703
});
640704

0 commit comments

Comments
 (0)