Skip to content

Commit

Permalink
Properly manage error keys (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored Jan 11, 2020
1 parent 31b931b commit 4271abd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
23 changes: 13 additions & 10 deletions addon/utils/get-key-values.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import isObject from './is-object';
import Err from '../-private/err';

let keysUpToValue: string[] = [];

/**
* traverse through target and return leaf nodes with `value` property and key as 'person.name'
*
* @method getKeyValues
* @return {Array} [{ 'person.name': value }]
*/
export function getKeyValues<T extends Record<string, any>>(obj: T, keysUpToValue: string[] = []): object[] {
export function getKeyValues<T extends Record<string, any>>(obj: T): object[] {
let map = [];

for (let key in obj) {
Expand All @@ -19,39 +21,40 @@ export function getKeyValues<T extends Record<string, any>>(obj: T, keysUpToValu
// stop collecting keys
keysUpToValue = [];
} else if (key !== 'value') {
map.push(...getKeyValues(obj[key], keysUpToValue));
map.push(...getKeyValues(obj[key]));
}
}
}

keysUpToValue = [];
return map;
}

let errorKeysUpToValue: string[] = [];

/**
* traverse through target and return leaf nodes with `value` property and key as 'person.name'
*
* @method getKeyErrorValues
* @return {Array} [{ key: 'person.name', validation: '', value: '' }]
*/
export function getKeyErrorValues<T extends Record<string, any>>(
obj: T,
keysUpToValue: string[] = []
): object[] {
export function getKeyErrorValues<T extends Record<string, any>>(obj: T): object[] {
let map = [];

for (let key in obj) {
keysUpToValue.push(key);
errorKeysUpToValue.push(key);

if (obj[key] && isObject(obj[key])) {
if (Object.prototype.hasOwnProperty.call(obj[key], 'value') && obj[key] as any instanceof Err) {
map.push({ key: keysUpToValue.join('.'), validation: obj[key].validation, value: obj[key].value });
map.push({ key: errorKeysUpToValue.join('.'), validation: obj[key].validation, value: obj[key].value });
// stop collecting keys
keysUpToValue = [];
errorKeysUpToValue = [];
} else if (key !== 'value') {
map.push(...getKeyErrorValues(obj[key], keysUpToValue));
map.push(...getKeyErrorValues(obj[key]));
}
}
}

errorKeysUpToValue = [];
return map;
}
14 changes: 12 additions & 2 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,22 @@ module('Unit | Utility | changeset', function(hooks) {
assert.deepEqual(dummyChangeset.get('change').name, 'a', 'should return nested change');
});


test('can get nested values in the errors object', function(assert) {
let dummyChangeset = new Changeset(dummyModel, dummyValidator);
dummyChangeset.set('unknown', 'wat');
dummyChangeset.set('org.usa.ny', '');
dummyChangeset.set('name', '');

let expectedErrors = [{ key: 'org.usa.ny', validation: 'must be present', value: '' }];
assert.deepEqual(dummyChangeset.get('errors'), expectedErrors, 'should return errors object for `org.usa.ny` key');
let expectedErrors = [
{ key: 'org.usa.ny', validation: 'must be present', value: '' },
{ key: 'name', validation: 'too short', value: '' }
];
assert.deepEqual(
dummyChangeset.get('errors'),
expectedErrors,
'should return errors object for `org.usa.ny` key and `name` key'
);
});

/**
Expand Down

0 comments on commit 4271abd

Please sign in to comment.