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

Properly manage error keys #402

Merged
merged 3 commits into from
Jan 11, 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
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