The Validator Class


exports:  class Validator

class Validator

Handles validation of input and parsing of schema criterion, as well as validation and sanitation of input.


type:  String

The name of this Validator. Should not contain spaces.


type:  Object

Collection of key-value pairs specifying the fields this Validator covers, and their input requirements. Each key should be the name of an input field as it would appear in the request body. Each value should be an Array of validator criterion and options to be applied to the field.

constructor(String name, Object schema)

returns:  this

Initializes the class. Stores the name in and the schema in this.schema.

validate(Object input, function callback = false)

returns:  Error|Boolean

Iterates over the this.schema fields and applies each field's validation criteria to the corresponding input field. If any of these criteria fail, their error messages are collected. If the validation succeeds, then the input fields are masked, which is to say that all input fields NOT in this.schema are removed. Then, the callback is called and passed a Boolean value which is true if the validation fails. If the validation has failed, an Object of key-value pairs where the keys are the name of the field, and the values are each an Array of error messages is passed to the callback as well. If the validation succeeds, then the Boolean value is false and the callback is passed the masked input. If at any point this validation encounters a programmatic error, this function will return an instance of Error which should be handled accordingly.

If no callback is provided, this function will return a Boolean value that is true if the validation has failed. However, this is not the recommended implementation.

mask_returns(Object schema, Object input)

returns:  Object

Masks the input such that any fields not present in the schema are removed.


returns:  Object

Creates a template to store error messages by field. That is, an Object of key-value pairs where the keys are the names of the fields in this.schema and the values are each an empty Array to hold error messages.

extract_validator_arguments(String criterion, Object input)

returns:  Object

Field criteria can be specified with one or more arguments. A criterion argument can be specified after the : character in the criterion String. The argument should be valid JSON. Here's an example:

const sample_form_definitions = {
  	// field name		criteria
  	age				:	[ 'isInt:{"min":13}', 'required' ],
  	parent_age		:	[ 'isInt:{"min":$age$}' ],
  	secret_password :   [ 'equals:thisisapassword' ],

Here, the age field is specified to be required, and must be an integer greater than 13. Note that the argument on isInt is valid JSON. The parent_age field must be an integer greater than the value of the age field. Field interpolation can be specified by surrounding the name of the field with $ characters. The secret_password field must be equal to 'thisisapassword'. Unformatted strings are each parsed as a String.

parse_interpolators(String string)

returns:  Array

Given a string, it finds names of fields to be interpolated (that is, the name of a field surrounded by $ characters) and returns an Array of the field names.

interpolate_fields(String string, Object inputs)

returns:  String

Given a string that may contain $-surrounded field names to be interpolated, this function replaces them with the corresponding values from the specified input, then it returns the string.

stringify_argument(String argument = '')

returns:  String

Turns a criterion argument string into human readable format. This is specifically useful for arguments which specify a min and/or max attributes. This is usually passed to the message generator to make error messages more explicit.

For example:

'{"min":8, "max":10}'    becomes    'greater than 8 and less than 10'

handle(express.request req, express.response res, String error_route, function callback)

Handles a request by calling this.validate on the req.body. If the validation fails, the error Object is written to req.session.forms[].errors, and the user is redirected to error_route. If the validation succeeds, the callback function is called and is passed the req, res, and input.

No Comments
Back to top