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

add #removeError and #removeErrors to Changeset interface #668

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ export default class ValidatedForm extends Component {
- [`validate`](#validate)
- [`addError`](#adderror)
- [`pushErrors`](#pusherrors)
- [`removeError`](#removeerror)
- [`removeErrors`](#removeerrors)
- [`snapshot`](#snapshot)
- [`restore`](#restore)
- [`cast`](#cast)
Expand Down Expand Up @@ -823,6 +825,28 @@ This is compatible with `ember-changeset-validations`, and allows you to either

**[⬆️ back to top](#api)**

#### `removeError`

Manually remove an error from the changeset.

```js
changeset.removeError('email');
```
Removes an error without having to rollback the property.

**[⬆️ back to top](#api)**

#### `removeErrors`

Manually remove an error from the changeset.

```js
changeset.removeErrors()
```
Removes all the errors without having to rollback properties.

**[⬆️ back to top](#api)**

#### `snapshot`

Creates a snapshot of the changeset's errors and changes. This can be used to `restore` the changeset at a later time.
Expand Down
23 changes: 23 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,29 @@ export class EmberChangeset extends BufferedChangeset {
return error;
}

/**
* Manually remove an error from the changeset.
*
* @method removeError
*/
removeError(key) {
super.removeError(key);

notifyPropertyChange(this, key);
// Return passed-in `error`.
return this;
}

/**
* Manually clears the errors from the changeset
*
* @method removeError
*/
removeErrors() {
super.removeErrors();
return this;
}

/**
* Manually push multiple errors to the changeset as an array.
*
Expand Down
88 changes: 88 additions & 0 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,94 @@ module('Unit | Utility | changeset', function (hooks) {
assert.ok(get(dummyChangeset, 'isValid'), 'should be valid');
});

/**
* #removeError
*/

test('#removeError removes an error from the changeset', async function (assert) {
let dummyChangeset = Changeset(dummyModel);
dummyChangeset.addError('email', {
value: '[email protected]',
validation: 'Email already taken',
});

assert.true(dummyChangeset.isInvalid);
assert.strictEqual(get(dummyChangeset, 'error.email.validation'), 'Email already taken');

dummyChangeset.removeError('email');
assert.true(dummyChangeset.isValid);

assert.strictEqual(get(dummyChangeset, 'error.email.validation'), undefined);
});

test('#removeError using an invalid key does not throw an error', async function (assert) {
let dummyChangeset = Changeset(dummyModel);
dummyChangeset.addError('email', {
value: '[email protected]',
validation: 'Email already taken',
});

assert.true(dummyChangeset.isInvalid);
assert.strictEqual(get(dummyChangeset, 'error.email.validation'), 'Email already taken');

dummyChangeset.removeError('email');
dummyChangeset.removeError('foo');
assert.true(dummyChangeset.isValid);

assert.strictEqual(get(dummyChangeset, 'error.email.validation'), undefined);
});

test('#removeError removing one error leaves the other', async function (assert) {
let dummyChangeset = Changeset(dummyModel);
dummyChangeset.addError('email', {
value: '[email protected]',
validation: 'Email already taken',
});
dummyChangeset.addError('age', {
value: '0',
validation: 'Age is too low',
});

assert.true(dummyChangeset.isInvalid);
assert.strictEqual(get(dummyChangeset, 'error.email.validation'), 'Email already taken');
assert.strictEqual(get(dummyChangeset, 'error.age.validation'), 'Age is too low');

dummyChangeset.removeError('email');
assert.false(dummyChangeset.isValid);

assert.strictEqual(get(dummyChangeset, 'error.email.validation'), undefined);
assert.strictEqual(get(dummyChangeset, 'error.age.validation'), 'Age is too low');
});

/**
* #removeErrors
*/

test('#removeErrors removes all errors', async function (assert) {
let dummyChangeset = Changeset(dummyModel);
dummyChangeset.addError('email', {
value: '[email protected]',
validation: 'Email already taken',
});

assert.true(dummyChangeset.isInvalid);
assert.strictEqual(get(dummyChangeset, 'error.email.validation'), 'Email already taken');

dummyChangeset.removeErrors();
assert.true(dummyChangeset.isValid);

assert.strictEqual(get(dummyChangeset, 'error.email.validation'), undefined);
assert.propEqual(get(dummyChangeset, 'errors'), []);
});

test('#removeErrors succeeds even when there are no errors', async function (assert) {
let dummyChangeset = Changeset(dummyModel);
assert.false(dummyChangeset.isInvalid);
dummyChangeset.removeErrors();
assert.true(dummyChangeset.isValid);
assert.propEqual(get(dummyChangeset, 'errors'), []);
});

/**
* #pushErrors
*/
Expand Down