npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

formatik

v1.2.0

Published

To validate associative arrays

Downloads

14

Readme

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

  • 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

Bitdeli Badge