Validator

flitter-forms/Validator~ Validator

Validates input against a specific schema and returns errors.

Constructor

new Validator(name, schema)

Source:

Instantiate the class. Store the schema and name used for validation.

Parameters:
Name Type Description
name string

name of this validator

schema Object

schema to validate input against

Methods

extract_validator_arguments(criterion, input) → {Object|Object|Object}

Source:

Break the string-form criterion into its name and argument(s). If the argument has any references to input fields, those values will be interpolated in place of the reference. This is done by calling module:flitter-forms/Validator~Validator#interpolate_fields.

Parameters:
Name Type Description
criterion string

string-form criterion to be parsed

input Object

input to be interpolated

Returns:
Type
Object | Object | Object

handle(req, res, error_route, callback)

Source:

Handle an incoming form submission from the Express request's body. Runs module:flitter-forms/Validator~Validator#validate on the input. If the validation succeeds, pass the masked input to the provided callback. Otherwise, store the error messages in the request session and redirect the user to the specified error route.

Parameters:
Name Type Description
req Express/Request
res Express/Response
error_route string

route the user should be redirected to upon a validation failure

callback module:flitter-forms/Validator~Validator#handle_callback

interpolate_fields(string, inputs) → {string}

Source:

Replace any references to fields with the value of that field from the given input.

Parameters:
Name Type Description
string string

String that may contain references to be interpolated.

inputs Object

collection of input field values to be interpolated

Returns:
Type
string

make_error_chain() → {Object}

Source:

Create an Object for holding the errors for the fields defined in module:flitter-forms/Validator~Validator#schema. Each key in the schema is assigned an empty array.

Returns:
Type
Object

mask_returns(schema, input) → {Object}

Source:

Given an input and a schema, returns only the input fields that are present in the schema.

e.g. provided schema: {test:"JSON"} and
input: {test:"foo", also_test:"bar"}, we have the output:

 output: {test:"foo"}
Parameters:
Name Type Description
schema Object

schema to check fields against

input Object

input to be masked

Returns:
Type
Object

parse_interpolators(string) → {array}

Source:

Given some string that may contain references to field names, create an array of those field names.

Parameters:
Name Type Description
string string

String containing the references. (e.g. "Price: $cost$")

Returns:
Type
array

stringify_argument(argument) → {string}

Source:

Format the provided criterion argument as a human-readable string. For example, if the argument is an Object containing "min" and/or "max" values, make it human readable with "less than...greater than."

Parameters:
Name Type Description
argument string | Object

argument to be converted

Returns:
Type
string

validate(input, callbackopt) → {Error|boolean}

Source:

Validates the given input against the Validator's schema. This method will only provide inputs listed in the schema. All other fields are ignored and are not returned.

If a callback is provided, the boolean fail status and error message object will be passed to it. Otherwise, the function will return the error message object.

Parameters:
Name Type Attributes Default Description
input Object

input to be validated where key => value is the field name => field value

callback module:flitter-forms/Validator~Validator#validate_callback <optional>
false

function called when validation has completed

Returns:
Type
Error | boolean

Type Definitions

handle_callback(req, res, input)

Source:

Called after a form submission passes validation.

Parameters:
Name Type Description
req Express/Request
res Express/Response
input Object

the masked input from the form submission. This input was successfully validated.

validate_callback(failed, maskedopt)

Source:

Some function that is called after the validation completes.

Parameters:
Name Type Attributes Description
failed boolean

if true, then the input has failed validation

masked Object <optional>

if the validation succeeded, this contains the masked input values. If the validation failed, it contains the error message object.