0.4.6 • Published 7 years ago
accurized v0.4.6
Accurized
Verify the validity and accuracy of data as it enters your system.
Getting Started
Install Accurized:
npm install accurized --save
Overview
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.
Examples
Field Validation:
accurized = require 'accurized'
validate = accurized.field('must be a valid email address').trim().email().as 'string'
validate 'user@domain.com',
(value) ->
assert value is 'user@domain.com'
(error) ->
assert false
validate ' user@domain.com ',
(value) ->
assert value is 'user@domain.com'
(error) ->
assert false
validate 'monkeys',
(value) ->
assert false
(error) ->
assert error is 'must be a valid email address'
JSON Validation:
express = require 'express'
accurized = require 'accurized'
body = require 'body-parser'
app = express()
app.use body.json()
app.post '/events',
accurized.data (json, req, res, next) ->
json.field('name', 'A name between 3 and 33 characters long is required.')
.trim().length(3, 33)
json.field('state', "The state must be one of 'visible', 'hidden' or 'deleted'.")
.trim().in(['visible', 'hidden', 'deleted'])
start = json.field('time.start', 'The start time, if present, must be an iso8601 formatted datetime.')
.trim().optional().date().as 'date'
json.field('time.end', 'The end time, if present, must be an iso8601 formatted datetime occurring after the start time.')
.trim().optional().after(start.value()).as '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
(res, res, next) ->
# operate on the body knowing that all of the above transformations,
# validations and type coercions have been performed successfully
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
Release History
(Nothing yet)