Verify the validity and accuracy of data as it enters your system.
npm install accurized --save
Accurized, in its simplest form, is a fluent interface wrapper around the validator module. In addition it includes connect/express middleware builders for validating url-encoded forms and JSON data submissions.
accurized = require 'accurized'validate = accurizedfield'must be a valid email address'trimemailas 'string'validate 'firstname.lastname@example.org'assert value is 'email@example.com'assert falsevalidate ' firstname.lastname@example.org 'assert value is 'email@example.com'assert falsevalidate 'monkeys'assert falseassert error is 'must be a valid email address'
express = require 'express'accurized = require 'accurized'body = require 'body-parser'app = expressappuse bodyjsonapppost '/events'accurizeddatajsonfield'name''A name between 3 and 33 characters long is required.'trimlength333jsonfield'state'"The state must be one of 'visible', 'hidden' or 'deleted'."trimin'visible''hidden''deleted'start = jsonfield'time.start''The start time, if present, must be an iso8601 formatted datetime.'trimoptionaldateas 'date'jsonfield'time.end''The end time, if present, must be an iso8601 formatted datetime occurring after the start time.'trimoptionalafterstartvalueas 'date'# if any of the above fail the built middleware will render a response# with a status code of 422 and a human and machine readable JSON body# describing the invalid field and their issues# operate on the body knowing that all of the above transformations,# validations and type coercions have been performed successfully
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.