-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(identify): make identify API accept deep object as value (#90)
* feat(identify): make identify API accept deep object as value * move isValidateProperties to utils and add unit test * make type recursively tightened * validateProperties fixes * add warn when type is invalid
- Loading branch information
1 parent
005c309
commit 044230f
Showing
5 changed files
with
89 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { logger } from './logger'; | ||
const MAX_PROPERTY_KEYS = 1000; | ||
|
||
const _isValidObject = (properties: { [key: string]: any }): boolean => { | ||
if (Object.keys(properties).length > MAX_PROPERTY_KEYS) { | ||
logger.warn('too many properties. Skipping operation'); | ||
return false; | ||
} | ||
for (const key in properties) { | ||
if (typeof key !== 'string') { | ||
logger.warn('invalid properties format. Skipping operation'); | ||
return false; | ||
} | ||
let value = properties[key]; | ||
if (!isValidProperties(key, value)) return false; | ||
} | ||
return true; | ||
}; | ||
|
||
const isValidProperties = (property: string, value: any): boolean => { | ||
if (typeof property != 'string') return false; | ||
if (Array.isArray(value)) { | ||
for (const valueElement of value) { | ||
if (Array.isArray(valueElement)) { | ||
logger.warn('invalid array element type ', typeof valueElement); | ||
return false; | ||
} else if (typeof valueElement === 'object') { | ||
return _isValidObject(value); | ||
} else if (!(typeof valueElement === 'number' || typeof valueElement === 'string')) { | ||
logger.warn('invalid array element type ', typeof valueElement); | ||
return false; | ||
} | ||
} | ||
} else if (typeof value === 'object') { | ||
return _isValidObject(value); | ||
} else if (!(typeof value === 'number' || typeof value === 'string')) { | ||
logger.warn('invalid value type ', typeof value); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
export { isValidProperties }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { isValidProperties } from '../src/validateProperties'; | ||
describe('isValidateProperties', () => { | ||
it('should pass on valid properties', () => { | ||
const validProperties = { | ||
keyForString: 'stringValue', | ||
keyForNumber: 123, | ||
keyForArray: ['test', 456, { arrayObjKey1: 'arrayObjValue1' }], | ||
keyForObj: { | ||
objKey1: 'objValue1', | ||
objKey2: 'objValue2', | ||
}, | ||
}; | ||
expect(isValidProperties('property', validProperties)).toBe(true); | ||
}); | ||
|
||
it('should fail on invalid properties with function as value', () => { | ||
const testFunc = () => { | ||
return 'test'; | ||
}; | ||
const inValidProperties = { | ||
keyForFunct: testFunc, | ||
}; | ||
expect(isValidProperties('property', inValidProperties)).toBe(false); | ||
}); | ||
|
||
it('should fail on invalid properties with array nested in array', () => { | ||
const inValidProperties = ['item1', 123, ['subItem1', 'subItem2']]; | ||
expect(isValidProperties('property', inValidProperties)).toBe(false); | ||
}); | ||
|
||
it('should fail when any key is not string', () => { | ||
const validProperties = { | ||
keyForString: 'stringValue', | ||
keyForNumber: 123, | ||
keyForArray: ['test', 456], | ||
keyForObj: { | ||
objKey1: 'objValue1', | ||
objKey2: 'objValue2', | ||
}, | ||
}; | ||
expect(isValidProperties(1 as any, validProperties)).toBe(false); | ||
}); | ||
}); |