OpenVeo server API for plugins

API Docs for: 3.0.0
Show:

util

Defined in: lib/util.js:5
Module: util

Summary

Provides functions for common JavaScript operations.

Methods

intersectArray

Defined in lib/util.js:74

Syntax

intersectArray

(
  • [array1]
  • [array2]
)
Array

Summary

Makes intersection of two arrays.

Parameters:

  • [array1] Array optional

    An array

  • [array2] Array optional

    An array

Returns:

Array:

The intersection of the two arrays

isContained

Defined in lib/util.js:101

Syntax

isContained

(
  • expectedValue
)
Boolean

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:

Boolean:

true if the expected value has been found in value

isEmailValid

Defined in lib/util.js:88

Syntax

isEmailValid

(
  • email
)
Boolean

Summary

Checks if an email address is valid or not.

Parameters:

  • email String

    The email address

Returns:

Boolean:

true if the email is valid, false otherwise

joinArray

Defined in lib/util.js:60

Syntax

joinArray

(
  • [array1]
  • [array2]
)
Array

Summary

Makes union of two arrays.

Parameters:

  • [array1] Array optional

    An array

  • [array2] Array optional

    An array

Returns:

Array:

The union of the two arrays

merge

Defined in lib/util.js:18

Syntax

merge

(
  • object1
  • object2
)
Object

Summary

Merges, recursively, all properties of object2 in object1.

This will not create copies of objects.

Parameters:

  • object1 Object

    The JavaScript final object

  • object2 Object

    A second JavaScript object to merge into the first one

Returns:

Object:

object1

shallowValidateObject

Defined in lib/util.js:133

Syntax

shallowValidateObject

(
  • objectToAnalyze
  • validationDescription
)
Object

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:

  • objectToAnalyze Object

    The object to analyze

  • validationDescription Object

    The validation description object

Returns:

Object:

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);