util
Summary
Provides functions for common JavaScript operations.
Item Index
Methods
intersectArray
Syntax
Summary
Makes intersection of two arrays.
Returns:
The intersection of the two arrays
isContained
Syntax
Summary
Checks if a value is isContained into another comparing primitive types.
All values in expectedValue must be found in value to pass the test.
Parameters:
Returns:
true if the expected value has been found in value
isEmailValid
Syntax
Summary
Checks if an email address is valid or not.
Parameters:
-
emailStringThe email address
Returns:
true if the email is valid, false otherwise
merge
Syntax
Summary
Merges, recursively, all properties of object2 in object1.
This will not create copies of objects.
Parameters:
Returns:
object1
shallowValidateObject
Syntax
Summary
Validates first level object properties using the given validation description object.
It helps validating that an object, coming from a request query string parameters correspond to the expected type, if it has to be required, if it must be contained into a list of values etc.
Available features by types :
- string
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
- in Specify an array of strings to validate that the value is inside this array
- number
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
- in Specify an array of numbers to validate that the value is inside this array
- gt Specify a number to validate that the value is greater than this number
- lt Specify a number to validate that the value is lesser than this number
- gte Specify a number to validate that the value is greater or equal to this number
- lte Specify a number to validate that the value is lesser or equal to this number
- array
- required Boolean to indicate if the value is required (an empty array is not an error)
- array
- required Boolean to indicate if the value is required (an empty array is not an error)
- date
- required Boolean to indicate if the value is required
- gt Specify a date to validate that the value is greater than this date
- lt Specify a date to validate that the value is lesser than this date
- gte Specify a date to validate that the value is greater or equal to this date
- lte Specify a date to validate that the value is lesser or equal to this date
- object
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
Parameters:
Returns:
A new object with the list of properties as expected
Example:
// Get util
var util = require('@openveo/api').util;
// Validate parameters
var params = util.shallowValidateObject({
myStringProperty: 'my value',
myNumberProperty: 25,
myArrayStringProperty: ['value1', 'value2']
myArrayNumberProperty: [10, 5]
myDateProperty: '02/25/2016'
myObjectProperty: {firstKey: 'firstValue'}
}, {
myStringProperty: {type: 'string', required: true, default: 'default', in: ['my value', 'value']},
myNumberProperty: {type: 'number', required: true, default: 0, in: [0, 5, 10], gte: 0, lte: 5},
myArrayStringProperty: {type: 'array<string>', required: true},
myArrayNumberProperty: {type: 'array<number>', required: true},
myDateProperty: {type: 'date', required: true, gte: '02/20/2016', lte: '03/30/2016'}
myObjectProperty: {type: 'object', required: true}
});
console.log(params);