Skip to content

Validation Schema Documentation

Bob D'Errico edited this page Jul 30, 2016 · 3 revisions

The API controllers utilize a validation schema object to verify that all request body fields are valid, as well as to sanitize request body data.

##Example

var schema = {
  phoneNumber: {
    required: true,
    rules: [{
      validator: 'isNumeric',
      message: 'must be all numbers'
    }, {
      validator: 'isLength',
      options: 10,
      message: 'must be 10 digits'
    }],
    sanitizers: ['trim']
  }
}

##Explanation:

  • schema - The validation schema object
  • schema.fieldName - One or more fieldName objects
  • schema.fieldName.required - A boolean value indicating whether the field is required. A request body that does is missing required values will not pass validation.
  • schema.fieldName.rules - An array of rules objects for the field
  • schema.fieldName.rules.validator - A string representing a validator to run for this rule(see below for available validators).
  • schema.fieldName.rules.options - A value to be passed into the validator
  • schema.fieldName.rules.message - A message to be return in the error object if validation fails.
  • schema.fieldName.sanitizers - An array of strings, representing sanitizers to run for this field

##Available Validators The validators module depends on the (validator)[https://www.npmjs.com/package/validator] package. You will find a list of validators there. In addition, custom validators can be added. Custom validators for this project are found in /lib/validators.js.

Clone this wiki locally