Skip to content

Commit

Permalink
fix: Update account data with undefined from team-info (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Herdmann authored Sep 18, 2017
1 parent 8c012ad commit d37680e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
6 changes: 3 additions & 3 deletions electron/renderer/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ 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',
'name': 'String',
'accentID': 'Number',
});

if (isValidAccountData) {
dispatch(updateAccount(id, data));
if (validatedAccountData) {
dispatch(updateAccount(id, validatedAccountData));
} else {
console.warn(`Got invalid account data ${JSON.stringify(data)}`);
}
Expand Down
21 changes: 15 additions & 6 deletions electron/renderer/src/lib/__tests__/verifyObjectProperties.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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', () => {
Expand All @@ -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 = {
Expand All @@ -79,4 +88,4 @@ describe('verifyObjectProperties', () => {
expect(verifyObjectProperties(obj, config)).toBeFalsy();
});

});
});
17 changes: 12 additions & 5 deletions electron/renderer/src/lib/verifyObjectProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit d37680e

Please sign in to comment.