server/api/utils/validation

API validation utilities.

You should be familiar with the valdsl validation library.

Source:
See:

Methods

(static) validateRequestBody(req, optionsopt, callbacks) → {Promise.<ValidationContext>}

Source:

Asynchronously validates the body of the specified request with any number of validators. This function returns a promise which is resolved if validation is successful. It is rejected if validation fails.

function validateAuthentication(req) {
  return validate.requestBody(req, function() {
    return this.parallel(
      this.validate(
        this.json('/email'),
        this.required(),
        this.type('string'),
        this.email()
      ),
      this.validate(
        this.json('/password'),
        this.required(),
        this.type('string'),
        this.notBlank()
      )
    );
  })
}
Parameters:
Name Type Attributes Description
req Request

The Express request object to validate.

options object <optional>

Validation options (may be omitted).

Properties
Name Type Attributes Default Description
status number <optional>
422

The HTTP status code that the response should have if validation fails.

types Array.<string> <optional>
["object"]

The list of allowed types for the request body. Only a single object is considered valid by default, but you might want to specify ["array"] for a batch operation (or both, i.e. ["array", "object"]).

callbacks Array.<function()>

Validation callbacks.

Returns:

A promise that will be resolved with an empty valdsl ValidationContext if validation was successful, or rejected with a valdsl ValidationErrorBundle containing the list of errors if validation failed.

Type
Promise.<ValidationContext>

(static) validateValue(value, status, callbacks) → {Promise.<ValidationContext>}

Source:

Asynchronously validates an arbitrary value with any number of validators. This function returns a promise which is resolved if validation is successful. It is rejected if validation fails.

Parameters:
Name Type Description
value *

The value to validate.

status number

The HTTP status code that the response should have if validation fails.

callbacks Array.<function()>

Validation callbacks.

Returns:

A promise that will be resolved with an empty valdsl ValidationContext if validation was successful, or rejected with a valdsl ValidationErrorBundle containing the list of errors if validation failed.

Type
Promise.<ValidationContext>