validately
A simple form validation for NodeJS.
Installation
npm install validately
Getting started
Given a form:
Submit
Posting this form to the server may result in post data which looks something like this:
var formData = 'sample': 'This is some sample text!';
If we want to validate that the "sample" field contains a value, we can create a form-validator object with a 'required' validator.
var validately = ; var sampleFormValidator = ; var validation = sampleValidator; if validation // Happiness :) else // Sadness :( validation;
If the validation fails, the console for this example should print:
Sample is required
Default field validators
Name | Settings | Description |
---|---|---|
required | true | Ensures that a field contains a value. |
matches | "target field name" | Ensures that a field's value matches a target field's value. |
length | { min: Number, max: Number } | Ensures that the length of a field falls within a bound. |
schema | { fieldName: { validatorName: config } } | Ensures that a field's contents match a schema. |
Custom field validators
The form validation library supports custom field validators. Add them to the form validator:
var sampleFormValidator = ; { if formDatafieldName !== config'key' return ; ;}
This sample validator checks if a field matches the value stored in the rule's settings. If it doesn't, it will print out a message telling the user that the field does not match.
Validators pass results into the "next" callback parameter. If a validator calls "next" with no parameters, then the containing field will not store any errors for that rule. If a validator passes a message into "next," then the containing field will add the message into a list of errors accessible on the final validation object.
Arrays
Configure validately to handle arrays by wrapping a field's configuration in an array:
var formData = names: 'steve' 'dave'; var validator = ; var validation = validator; // ...
Nested Objects
Configure validately to handle nested, structured objects by using the "schema" field validator.
var formData = 'name': 'first': 'john' 'last': 'smith' ; var validator = ; // ...
Using custom validators in a nested schema
The "schema" validator uses the "validately" factory by default in order to validate nested objects. Adding custom validators requires overriding this default behavior by either deriving a custom factory from FormValidatorFactory, or by manually re-adding the 'schema' validator in your custom factory.
Method 1: Overriding the default factory
var util = ;var FormValidatorFactory = factory; { FormValidatorFactory;} util; CustomFormValidatorFactoryprototype { var validator = FormValidatorFactoryprototypecreate; // validator.addFieldValidator('custom', ...); return validator;}; var sampleValidator = ; sampleValidator;
Method 2: Re-adding the 'schema' validator
var validately = ;var buildSchemaValidator = validatorsbuildSchemaValidator; { return ;} var sampleValidator = ; sampleValidator;
Suggestions
Add custom validators using a factory method
If you make custom validators, you may want to decorate the "validator" factory method with your own.
// myValidator.jsvar validately = ; { return ;}
// sampleRoutes.jsvar myValidator = ; var sampleFormValidator = ;
Split validators into reusable modules
// userFormValidator.jsvar validately = ; var userFormValidator = ; moduleexports = userFormValidator;
// routes.jsvar userFormValidator = ; // ... etc ...
Documentation
Full documentation for all public classes and methods exposed by this project can be found in the "dist/docs" folder.