From 4271abdf1b2d842847a7b93e3ee400bf51d3b930 Mon Sep 17 00:00:00 2001 From: Scott Newcomer Date: Sat, 11 Jan 2020 14:21:20 -0800 Subject: [PATCH] Properly manage error keys (#402) --- addon/utils/get-key-values.ts | 23 +++++++++++++---------- tests/unit/changeset-test.js | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/addon/utils/get-key-values.ts b/addon/utils/get-key-values.ts index d085cc1b..3b19cd5e 100644 --- a/addon/utils/get-key-values.ts +++ b/addon/utils/get-key-values.ts @@ -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>(obj: T, keysUpToValue: string[] = []): object[] { +export function getKeyValues>(obj: T): object[] { let map = []; for (let key in obj) { @@ -19,39 +21,40 @@ export function getKeyValues>(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>( - obj: T, - keysUpToValue: string[] = [] -): object[] { +export function getKeyErrorValues>(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; } diff --git a/tests/unit/changeset-test.js b/tests/unit/changeset-test.js index 646dda16..699a1167 100644 --- a/tests/unit/changeset-test.js +++ b/tests/unit/changeset-test.js @@ -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' + ); }); /**