-
-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validateObjectWithArrayProps as a flexible utility for evaluating…
… secondary options
- Loading branch information
1 parent
ed56990
commit 20ad772
Showing
6 changed files
with
98 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use strict"; | ||
|
||
const validateObjectWithArrayProps = require("../validateObjectWithArrayProps"); | ||
|
||
describe('validateObjectWithArrayProps', () => { | ||
it('should return a function', () => { | ||
expect(validateObjectWithArrayProps(x => x)).toBeInstanceOf(Function) | ||
}) | ||
|
||
describe('returned validator', () => { | ||
const validator = validateObjectWithArrayProps(x => x) | ||
|
||
it('should return false if any of the object properties are not an array', () => { | ||
expect(validator({ | ||
arrayProp: [1, 2], | ||
nonArrayProp: 3 | ||
})).toBeFalsy() | ||
}) | ||
|
||
it('should return false if any of the object properties array values do not pass the test', () => { | ||
expect(validator({ | ||
arrayProp: [1, 2], | ||
nonArrayProp: [0, 3] | ||
})).toBeFalsy() | ||
}) | ||
|
||
it('should return true otherwise', () => { | ||
expect(validator({ | ||
arrayProp: [1, 2], | ||
nonArrayProp: [3, 4] | ||
})).toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('returned validator with array', () => { | ||
const validator = validateObjectWithArrayProps([x => x > 0, x => x < 0]) | ||
|
||
it('should accept an array of validators, any of which can return true', () => { | ||
expect(validator({ | ||
arrayProp: [1, 2], | ||
nonArrayProp: [-1, 3] | ||
})).toBeTruthy() | ||
}) | ||
|
||
it('should be false if none of the validators are true for any value', () => { | ||
expect(validator({ | ||
arrayProp: [1, 2], | ||
nonArrayProp: [-1, 0] | ||
})).toBeFalsy() | ||
}) | ||
}) | ||
}) |
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,39 @@ | ||
/* @flow */ | ||
"use strict"; | ||
|
||
const _ = require("lodash"); | ||
|
||
/** | ||
* Check whether the variable is an object and all it's properties are arrays of string values: | ||
* | ||
* ignoreProperties = { | ||
* value1: ["item11", "item12", "item13"], | ||
* value2: ["item21", "item22", "item23"], | ||
* value3: ["item31", "item32", "item33"], | ||
* } | ||
*/ | ||
|
||
module.exports = ( | ||
validator /*: Function */ | ||
) => ( | ||
value /*: Object*/ | ||
) /*: boolean*/ => { | ||
if (!_.isPlainObject(value)) { | ||
return false; | ||
} | ||
|
||
return Object.keys(value).every(key => { | ||
if (!_.isArray(value[key])) { | ||
return false; | ||
} | ||
|
||
// Make sure the array items are strings | ||
return value[key].every(item => { | ||
if (Array.isArray(validator)) { | ||
return validator.some(v => v(item)) | ||
} | ||
|
||
return validator(item) | ||
}); | ||
}); | ||
}; |
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