Skip to content

orioro/node-validate

Repository files navigation

node-validate

npm install @orioro/validate

Use cases

API

ValidationErrorSpec
  • validationErrorSpec {Object}
    • code {string}
    • messsage {string}
ValidationCase
ValidationError
  • validationError {Error}
ValidateOptions

Options for calling validate

  • options {Object}
    • interpreters {Object}
validate(validationExpression, value, options)

Executes a validation expression against the given value. Returns either an Array of ValidationErrorSpec objects or null (which indicates there were no errors found and the value is valid).

The expression may return one of these values:

  • a string: is interpreted as an error code and will be converted to a ValidationErrorSpec (e.g. 'SOME_VALIDATION_ERROR' becomes { code: 'SOME_VALIDATION_ERROR' })

  • an object: is interpreted as a ValidationErrorSpec and should have the properties code and message

  • an Array: is interpreted as an array of ValidationErrorSpec objects

  • null: indicates that no errors were found

  • validationExpression {Expression}

  • value {*}

  • options {ValidateOptions}

  • Returns: {null | ValidationErrorSpec[]}

validateThrow(validationExpression, value, options)

Performs same validation process as validate but if an error is encountered throws a ValidationError.

sequentialCases(cases)

Takes an array of ValidationCases and returns a ValidationExpression to be used with either validate or validateThrow.

The returned expression evaluates each case sequentially and if any of the cases returns a non-null value, the validation is interrupted and remaining cases are not evaluated.

parallelCases(cases)

Takes an array of ValidationCases and returns a ValidationExpression to be used with either validate or validateThrow.

The returned expression evaluates all cases in parallel and returns an array of all errors found.

allowValues(allowedValues, validation)

Wraps the given validationExpression so that it returns null (no error) if the value equals any of the specified allowedValues.

  • allowedValues {*[]}
  • validation {ValidationExpression}
  • Returns: {ValidationExpression}