adopted-ember-addons / ember-cp-validations

Ember computed property based validations

Home Page:https://adopted-ember-addons.github.io/ember-cp-validations/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ds-error validation is not reset for belongsTo relationship

azhiv opened this issue · comments

Environment

ember-cli: 3.12
ember: 3.12
ember-cp-validations: 4.0.0-beta.9

Steps to Reproduce

When an attribute is validated against ds-error this validation is reset when the value of the attribute is changed. However, for a belongsTo relationship this is not true. Consider a situation when the app receives 2 errors from the backend. The user sees that, for example, a string and a dropdown value (which is bound to a relationship) are invalid. He alters the text field and the dropdown selected value, but only the text field becomes valid which is not consistent.
Here is a simple text for the described behaviour:

import { module, test } from 'qunit';
import { buildValidations, validator } from 'ember-cp-validations';
import Model, { attr, belongsTo } from '@ember-data/model';
import { setupTest } from 'ember-qunit';

module('Unit | Utility | ds-error issue', function(hooks) {
  setupTest(hooks);

  test('ds-error', async function(assert) {
    const validations = buildValidations({
      text: validator('ds-error'),
      author: validator('ds-error'),
    });
    const userModel = Model.extend({
      name: attr('string'),
    });
    const commentModel = Model.extend(validations, {
      text: attr('string'),
      author: belongsTo('test-user-model'),
    });

    this.owner.register('model:test-user-model', userModel);
    this.owner.register('model:test-comment-model', commentModel);

    const store = this.owner.lookup('service:store');
    const user = store.createRecord('test-user-model', { id: 1, name: 'Charlie' });
    const comment = store.createRecord('test-comment-model', { id: 1, text: 'note', author: user });

    assert.ok(comment.validations.isValid, 'comment is valid');

    comment.errors.add('text', 'text error');
    comment.errors.add('author', 'author error');

    assert.notOk(comment.validations.attrs.text.isValid, 'comment text is not valid');
    assert.notOk(comment.validations.attrs.author.isValid, 'comment author is not valid');

    comment.set('text', 'new text');
    assert.ok(comment.validations.attrs.text.isValid, 'comment text is valid');

    comment.set('author', store.createRecord('test-user-model', { id: 2, name: 'Bob' }));
    // the below assertion is not satisfied
    assert.ok(comment.validations.attrs.author.isValid, 'comment author valid');
  });
});

So please tell me wether this is not a bug and how to reset the state of the validation to make the model valid.