Validator

flitter-forms/Validator~ Validator

Validates input against a specific schema and returns errors.


Constructor

new Validator(name, schema)

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

Extends

Members

(static) services

Defines the services required by this class.

(private) _di_allow_defer :boolean

If true, the injector will defer the class if the class requests any services that the container is missing. These services are filled in later and added to the prototype and any instances. True by default.

Type:
  • boolean

(private) _di_deferred_instances :Array.<module:flitter-di/src/Injectable~Injectable>

Collection of instances of this class that need to have the deferred service instances injected into them when the deferred services are finally provided.

(private) _di_deferred_services :Array.<string>

List of services that were deferred and not provided at the time of injection.

Type:
  • Array.<string>

Methods

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

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)

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}

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}

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}

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}

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}

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}

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)

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)

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.