Skip to content
An express.js middleware for node-validator.
JavaScript
Find file
Pull request Compare This branch is 8 commits ahead, 245 commits behind ctavan:master.
Latest commit 4e7f6eb May 15, 2013 @cjroebuck bump version
Failed to load latest commit information.
lib passing value into the filter.sanitize function May 13, 2013
test tests pass Nov 30, 2012
.gitignore tests pass Nov 30, 2012
.travis.yml Remove node 0.4 from travis and add 0.8 Aug 3, 2012
Makefile Add Makefile Apr 6, 2012
README.md tests pass Nov 30, 2012
index.js tests pass Nov 30, 2012
package.json bump version May 15, 2013

README.md

restify-validator

A restify.js middleware for node-validator.

This is basically a copy of a gist by node-validator author chriso.

Installation

npm install restify-validator

Usage

var util = require('util'),
    restify = require('restify'),
    restifyValidator = require('restify-validator'),
    app = restify.createServer();

app.use(restify.bodyParser());
app.use(restify.queryParser());
app.use(restifyValidator);

app.post('/:urlparam', function(req, res) {

  req.assert('postparam', 'Invalid postparam').notEmpty().isInt();
  req.assert('getparam', 'Invalid getparam').isInt();
  req.assert('urlparam', 'Invalid urlparam').isAlpha();

  req.sanitize('postparam').toBoolean();

  var errors = req.validationErrors();
  if (errors) {
    res.send(500 ,'There have been validation errors: ' + util.inspect(errors));
    return;
  }
  res.json({
    urlparam: req.params['urlparam'],
    getparam: req.params['getparam'],
    postparam: req.params['postparam']
  });
});

app.listen(8888);

Which will result in:

$ curl -d 'postparam=1' http://localhost:8888/test?getparam=1
{"urlparam":"test","getparam":"1","postparam":true}

$ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1
There have been validation errors: [
  { param: 'urlparam', msg: 'Invalid urlparam', value: 't1est' } ]

$ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1ab
There have been validation errors: [
  { param: 'getparam', msg: 'Invalid getparam', value: '1ab' },
  { param: 'urlparam', msg: 'Invalid urlparam', value: 't1est' } ]
Something went wrong with that request. Please try again.