formatik

To validate associative arrays

npm install formatik
1 downloads in the last week
24 downloads in the last month

Formatik

Build Status

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 = require('formatik').parse(req.body, require('./schema.json'), 'fr');

if (form.isValid()) {
   console.log(form.mget('value'));
}

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

{
   "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"
         }
      } 
   }
}

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

{
   "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

License

MIT/X11

Bitdeli Badge

npm loves you