Validation

Members

(inner, constant) OPTIONS :Object

Source:
Type:
  • Object

Methods

(inner) validate(argObj, validators, options) → {Array}

Description:
  • Validates each key-value entry in argObj using the validator functions in validators with matching keys.
    For any failures, validate will console.error the reason.

Source:
Example
const elements = {}
   const name = 'michael'
   const address = '12345 E. Street'
   const [ isValid, results ] = validate(
     { elements, name, address },
     { elements: isArr, $default: isStr }
   )
   console.log(isValid) // false
   console.log(results.elements.success) // false
Parameters:
Name Type Description
argObj Object

object, where keys are the name of the argument to validate, and value is its value

validators Object

object, where keys match the argument and values are predicate functions (return true/false and are passed the arg with the same key). - Use the $default key to define a default validator, which will validate any argument that doesn't have a custom validator defined.

options Object

contains logs, throws, and prefix props. When a validation fails, it will throw an error if throws is true. Else it logs error if logs is true. prefix prepends a string to the error messages.

Returns:
  • An entry with two values [ success, results ].
    • success: { Boolean } that is true if all arguments passed their validators, false otherwise
    • results: {Object} that holds the validation results for each argument, keyed by the same keys as in argObj. For each result object, the properties are: { success, key, value, validator, reason }.
Type
Array