Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ds-error validation is not reset for belongsTo relationship #670

Open
azhiv opened this issue Jan 14, 2020 · 0 comments
Open

ds-error validation is not reset for belongsTo relationship #670

azhiv opened this issue Jan 14, 2020 · 0 comments
Labels

Comments

@azhiv
Copy link

azhiv commented Jan 14, 2020

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.

@fsmanuel fsmanuel added the bug label Sep 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants