formatik

To validate associative arrays

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

Installation

With npm do:

$ npm install formatik

Tests

Use mocha to run the tests.

$ npm install mocha
$ mocha test

Usage

 
var form = require('formatik').parse(req.body, require('./schema.json'), 'fr');
 
if (form.isValid()) {
   console.log(form.mget('value'));
}

see the example directory for a complete example with expressjs.

API

Return Output Object.

Parse and validate ''data'' with ''schema''. Labels and Messages are choosed with ''language''.

Return Output Object.

Create on empty Output Object with ''schema''. Labels are choosed with ''language''.

Schema

{
   "familyName" : {
      "type" : "text", 
      "required" : true,
      "label" : {
          "fr" : "Nom de famille",
          "en" : "Family Name"
      },
      "error" : {
         "required" : {
            "fr" : "Le nom de famille est obligatoire",
            "en" : "familly Name are required"
         }
      } 
   }
}

The Javascript type for cast the variable. Values can be :

  • string | text
  • number
  • date
  • boolean

To indicate if the variable are required or optional. Values can be :

  • true
  • false (default)

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

To set the variable with default value.

Not yet implemeted. Contribs are welcome

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

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

List of predefined values. Values are an array.

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.

boolean indicate if the variable is valid.

the variable casted with the corresponding type.

if the variable is not valid, the error message (depending of the selected language).

the label of the variable (depending of the selected language)

Return None.

Set one field of all the variable with the same value. Example : form.mset('valid', null)

Return Object.

Get an new object with all the variable with only the value of one field. Example : form.mget('value')

{
   "familyName" : {
        "valid" : true,
        "value" : "Thouvenin",
        "input" : "Thouvenin",
        "error" : null,
        "label" : "Nom de famille"
    },
   "givenName" : {
        "valid" : false,
        "value" : "",
        "input" : "",
        "error" : "Le prénom est obligatoire",
        "label" : "Prénom"
    },
   "age" : {
        "valid" : true,
        "value" : 99,
        "input" : "99",
        "error" : null,
        "label" : "Age"
    },
   "available" : {
        "valid" : true,
        "value" : true,
        "input" : "on",
        "error" : null,
        "label" : "Disponible"
    }
}

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

License

MIT/X11