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

Fix false values #417

Merged
merged 3 commits into from
Jan 20, 2020
Merged
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
8 changes: 4 additions & 4 deletions addon/-private/validated-changeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ export class BufferedChangeset implements IChangeset {
let c: Change = changes[baseKey];
let result = this.getDeep(normalizeObject(c), remaining.join('.'));
// just b/c top level key exists doesn't mean it has the nested key we are looking for
if (result) {
if (typeof result !== 'undefined') {
return result;
}
}
Expand Down Expand Up @@ -822,7 +822,7 @@ export class BufferedChangeset implements IChangeset {
// find leaf and delete from map
while (isObject(currentNode) && currentKey) {
let curr: { [key: string]: unknown } = currentNode
if (curr.value || curr.validation) {
if (typeof curr.value !== 'undefined' || curr.validation) {
delete previousNode[currentKey];
}

Expand All @@ -849,14 +849,14 @@ export class BufferedChangeset implements IChangeset {
if (remaining.length > 0) {
let c = changes[baseKey];
result = this.getDeep(normalizeObject(c), remaining.join('.'));
if (result) {
if (typeof result !== 'undefined') {
return result;
}
} else {
result = changes[baseKey];
}

if (result && isObject(result)) {
if ((result !== undefined && result !== null) && isObject(result)) {
result = normalizeObject(result);
let content: Content = this[CONTENT];

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ let dummyValidations = {
return isPresent(value);
},
org: {
isCompliant(value) {
return !!value;
},
usa: {
ny(value) {
return isPresent(value) || "must be present";
Expand Down Expand Up @@ -682,6 +685,32 @@ module('Unit | Utility | changeset', function(hooks) {
assert.notOk(get(dummyChangeset, 'isInvalid'), 'should be valid');
});

test('it clears errors when setting to original value when nested', async function(assert) {
set(dummyModel, 'org', {
usa: { ny: 'vaca' }
});
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('org.usa.ny', '');

assert.ok(get(dummyChangeset, 'isInvalid'), 'should be invalid');
dummyChangeset.set('org.usa.ny', 'vaca');
assert.ok(get(dummyChangeset, 'isValid'), 'should be valid');
assert.notOk(get(dummyChangeset, 'isInvalid'), 'should be valid');
});

test('it clears errors when setting to original value when nested Booleans', async function(assert) {
set(dummyModel, 'org', {
isCompliant: true
});
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('org.isCompliant', false);

assert.ok(get(dummyChangeset, 'isInvalid'), 'should be invalid');
dummyChangeset.set('org.isCompliant', true);
assert.ok(get(dummyChangeset, 'isValid'), 'should be valid');
assert.notOk(get(dummyChangeset, 'isInvalid'), 'should be valid');
});

test('#set should delete nested changes when equal', async function(assert) {
set(dummyModel, 'org', {
usa: { ny: 'i need a vacation' }
Expand Down