Skip to content

Commit

Permalink
Fix false values (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored Jan 20, 2020
1 parent 657b8b1 commit dafac44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
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

0 comments on commit dafac44

Please sign in to comment.