diff --git a/electron/renderer/src/actions/index.js b/electron/renderer/src/actions/index.js index e041f1be433..ddf0c5f4fc9 100644 --- a/electron/renderer/src/actions/index.js +++ b/electron/renderer/src/actions/index.js @@ -62,7 +62,7 @@ export const abortAccountCreation = (id) => { export const updateAccountData = (id, data) => { return (dispatch, getState) => { - const isValidAccountData = verifyObjectProperties(data, { + const validatedAccountData = verifyObjectProperties(data, { 'teamID': 'String', 'userID': 'String', 'picture': 'String', @@ -70,8 +70,8 @@ export const updateAccountData = (id, data) => { 'accentID': 'Number', }); - if (isValidAccountData) { - dispatch(updateAccount(id, data)); + if (validatedAccountData) { + dispatch(updateAccount(id, validatedAccountData)); } else { console.warn(`Got invalid account data ${JSON.stringify(data)}`); } diff --git a/electron/renderer/src/lib/__tests__/verifyObjectProperties.spec.js b/electron/renderer/src/lib/__tests__/verifyObjectProperties.spec.js index c69bb318827..af737d13a51 100644 --- a/electron/renderer/src/lib/__tests__/verifyObjectProperties.spec.js +++ b/electron/renderer/src/lib/__tests__/verifyObjectProperties.spec.js @@ -21,7 +21,7 @@ import verifyObjectProperties from '../verifyObjectProperties'; describe('verifyObjectProperties', () => { - it('should return true if object contains all properties specified in the config', () => { + it('should return the object if object contains all properties specified in the config', () => { const obj = { foo: 'test', bla: 2, @@ -30,18 +30,27 @@ describe('verifyObjectProperties', () => { foo: 'String', bla: 'Number', }; - expect(verifyObjectProperties(obj, config)).toBeTruthy(); + + const validatedObject = verifyObjectProperties(obj, config); + expect(validatedObject).toBeTruthy(); + expect(Object.keys(validatedObject).length).toBe(2); }); - it('should return true if object contains only a subset of properties specified in the config', () => { + it('should return the object with defaults if object contains only a subset of properties specified in the config', () => { const obj = { bla: 2, }; const config = { foo: 'String', bla: 'Number', + some: 'Number', }; - expect(verifyObjectProperties(obj, config)).toBeTruthy(); + + const validatedObject = verifyObjectProperties(obj, config); + expect(validatedObject).toBeTruthy(); + expect(Object.keys(validatedObject).length).toBe(3); + expect(validatedObject['foo']).toBe(''); + expect(validatedObject['some']).toBeUndefined(); }); it('should return false if object contains a property with a wrong type', () => { @@ -58,7 +67,7 @@ describe('verifyObjectProperties', () => { it('should return false if object contains a property with undefined but config excepted a type', () => { const obj = { - foo: 1, + foo: undefined, bla: 2, }; const config = { @@ -79,4 +88,4 @@ describe('verifyObjectProperties', () => { expect(verifyObjectProperties(obj, config)).toBeFalsy(); }); -}); \ No newline at end of file +}); diff --git a/electron/renderer/src/lib/verifyObjectProperties.js b/electron/renderer/src/lib/verifyObjectProperties.js index ceb4a2fff44..6adb50bb8de 100644 --- a/electron/renderer/src/lib/verifyObjectProperties.js +++ b/electron/renderer/src/lib/verifyObjectProperties.js @@ -18,19 +18,26 @@ */ function is(type, obj) { - var clas = Object.prototype.toString.call(obj).slice(8, -1); - return obj != null && clas === type; + const getType = Object.prototype.toString.call(obj).slice(8, -1); + return obj != null && getType === type; } export default function(data, config) { const dataKeys = Object.keys(data); const configKeys = Object.keys(config); - if (dataKeys.length > configKeys) { + if (dataKeys.length > configKeys.length) { return false; } - return dataKeys.every((key) => { - return config.hasOwnProperty(key) && is(config[key], data[key]); + const isValidObject = configKeys.every((key) => { + if (!data.hasOwnProperty(key)) { + data[key] = config[key] === 'String' ? '' : undefined; + return true; + } + + return is(config[key], data[key]); }); + + return isValidObject ? data : false; }