util
Summary
Provides functions for common JavaScript operations.
// Load module "util"
var util = require('@openveo/api').util;
Item Index
Methods
- areSameArrays static
- evaluateDeepObjectProperties static
- getPropertyFromArray static
- intersectArray static
- isContained static
- isEmailValid static
- joinArray static
- merge static
- shallowValidateObject static
- validateFiles static
Methods
areSameArrays
Syntax
Summary
Compares two arrays.
Shallow validates that two arrays contains the same elements, no more no less.
Returns:
true if arrays are the same, false otherwise
evaluateDeepObjectProperties
Syntax
evaluateDeepObjectProperties
-
propertyPath -
objectToAnalyze
Summary
Evaluates a path of properties on an object.
It does not use the JavaScript eval function.
Parameters:
Returns:
The value of the property
Example:
// Get util
var util = require('@openveo/api').util;
// Get property 'my.deep.property' of the object
var value = util.evaluateDeepObjectProperties('my.deep.property', {
my {
deep {
property: 'My deep property value'
}
}
});
// "My deep property value"
console.log(value);
getPropertyFromArray
Syntax
Summary
Gets a specific property from an Array of Objects.
Parameters:
Returns:
The list of values for the given property
Example:
// Get util
var util = require('@openveo/api').util;
// Get property 'id' of each objects of the array
var params = util.getPropertyFromArray('id', [
{id: 0},
{id: 1},
{id: 2, items: [{id: 3}]}
], 'items');
// [0, 1, 2, 3]
console.log(params);
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<string>
- required Boolean to indicate if the value is required (an empty array is not an error)
- in Specify an array of values to validate that each value of the array is inside this array
- array<number>
- required Boolean to indicate if the value is required (an empty array is not an error)
- in Specify an array of values to validate that each value of the array is inside this array
- array<object>
- 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)
- boolean
- default Specify a default value
- required Boolean to indicate if the value is required (if default is specified, value will always be set)
- file
- required Boolean to indicate if the value is required
- in Specify an array of types to validate that the file's type is inside this array
Parameters:
Returns:
A new object with the list of properties as expected
Example:
// Get util
var util = require('@openveo/api').util;
var fileSystem = require('@openveo/api').fileSystem;
// Validate parameters
var params = util.shallowValidateObject({
myStringProperty: 'my value',
myNumberProperty: 25,
myArrayStringProperty: ['value1', 'value2'],
myArrayNumberProperty: [10, 5],
myArrayObjectProperty: [{}, {}],
myDateProperty: '02/25/2016',
myObjectProperty: {firstKey: 'firstValue'},
myBooleanProperty: true,
myFileProperty: 88 13 70 17 // At least the first 300 bytes of the file
}, {
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, in: ['value1', 'value2']},
myArrayNumberProperty: {type: 'array<number>', required: true, in: [42, 43]},
myArrayObjectProperty: {type: 'array<object>', required: true},
myDateProperty: {type: 'date', required: true, gte: '02/20/2016', lte: '03/30/2016'},
myObjectProperty: {type: 'object', required: true},
myBooleanProperty: {type: 'boolean', required: true},
myFileProperty: {type: 'file', required: true, in: [
fileSystem.FILE_TYPES.JPG,
fileSystem.FILE_TYPES.PNG,
fileSystem.FILE_TYPES.GIF,
fileSystem.FILE_TYPES.MP4,
fileSystem.FILE_TYPES.TAR
]}
});
console.log(params);
validateFiles
Syntax
validateFiles
-
filesToAnalyze -
validationDescription -
callback
Summary
Validates that files are in the expected type.
Available features for validation object:
- in Specify an array of types to validate that the file type is inside this array
Parameters:
-
filesToAnalyzeObjectFiles to validate with keys as files identifiers and values as files absolute paths
-
validationDescriptionObjectThe validation description object with keys as files identifiers and values as validation objects
-
callbackFunctionThe function to call when done
- Error The error if an error occurred, null otherwise
- Object Files with keys as the files identifiers and values as Objects containing validation information: isValid and type (from util.FILE_TYPES)
Example:
// Get util
var util = require('@openveo/api').util;
var fileSystem = require('@openveo/api').fileSystem;
// Validate parameters
var params = util.validateFiles({
myFirstFile: '/tmp/myFirstFile.mp4',
mySecondFile: '/tmp/mySecondFile.tar'
}, {
myFirstFile: {in: [fileSystem.FILE_TYPES.MP4]},
mySecondFile: {in: [fileSystem.FILE_TYPES.TAR]}
}, function(error, files) {
if (error) {
console.log('An error occurred during validation with message: ' + error.message);
}
console.log('Is file valid ? ' + files.myFirstFile.isValid);
console.log('File type: ' + files.myFirstFile.type);
});
console.log(params);