Formatik
Yet another module to validate associative arrays with a schema, but unlike the others implementations (see below) there some particulars features :
- can only validate associative arrays, (Browser form or Browser query, Mysql datasets, etc.)
- can transform type of the variables
- the messages can be specific for each error
- i18n supports for labels and messages
- origins data are never modified
- The result of the validation tries to be the most practical :
- all declared variables exists
- original value & cast value are side by side
- extremely easy to use in a template see by yourself
Contributors
Installation
With npm do:
$ npm install formatik
Tests
Use mocha to run the tests.
$ npm install mocha
$ mocha test
Usage
Basic example
var form = ; if form console;
Complete example
see the example directory for a complete example with expressjs.
API
parse(Object data, Object schema, String language)
Return Output Object.
Parse and validate ''data'' with ''schema''. Labels and Messages are choosed with ''language''.
create(Object schema, String language)
Return Output Object.
Create on empty Output Object with ''schema''. Labels are choosed with ''language''.
Schema
Example
Description
type
The Javascript type for cast the variable. Values can be :
- string | text
- number
- date
- boolean
required
To indicate if the variable are required or optional. Values can be :
- true
- false (default)
pattern
To validate the variable with a mask (or pattern). Values depended of the type of the variable.
- a REGEX for text
- a date format for date
default
To set the variable with default value.
maxlength
Not yet implemeted. Contribs are welcome
label
The label of the variable. Values can be multiform :
- array of object like this :
{ 'lang' : 'XX', '$t' : 'The label' }
- object like this :
{ 'en' : 'Hello', 'fr' : 'Bonjour' }
- string
error
The list of errors messages depending or not of the control test. Values can be multiform :
- array of object like this :
{ 'lang' : 'XX', '$t' : 'The error message', 'for' : type|required|pattern|maxlength }
- object like this :
{ 'en' : 'Hello', 'fr' : 'Bonjour' }
- object like this :
{ type|required|pattern|maxlength : { 'en' : 'Hello', 'fr' : 'Bonjour' }}
- string
values
List of predefined values. Values are an array.
alias
List of alternative name of the variable.
Output Object
The validator product an new object contains for each variable 5 fields. Also, the object provide 2 methods.
Fields
valid
boolean indicate if the variable is valid.
value
the variable casted with the corresponding type.
error
if the variable is not valid, the error message (depending of the selected language).
label
the label of the variable (depending of the selected language)
Methods
mset(String name, Object value)
Return None.
Set one field of all the variable with the same value. Example : form.mset('valid', null)
mget(String name)
Return Object.
Get an new object with all the variable with only the value of one field. Example : form.mget('value')
Example
Also
- https://github.com/garycourt/JSV
- https://github.com/chriso/node-validator
- https://github.com/freewil/express-form
- https://github.com/eivindfjeldstad/validate
- https://github.com/kriszyp/json-schema
- https://github.com/Baggz/Amanda
- https://github.com/bradleyg/acceptance