Skip to content

Instantly share code, notes, and snippets.

@Radagaisus
Created August 8, 2012 08:59
Show Gist options
  • Select an option

  • Save Radagaisus/3293617 to your computer and use it in GitHub Desktop.

Select an option

Save Radagaisus/3293617 to your computer and use it in GitHub Desktop.
Express Validator Suger
# Implementation
pre = (fn, error_handler, great) ->
fn()
errors = req.validationErrors()
if errors
v(errors) for k,v of error_handler
else
v() for k,v of great
# Result
pre ->
req.assert('postparam', 'Invalid postparam').notEmpty().isInt()
req.assert('getparam', 'Invalid getparam').isInt()
req.assert('urlparam', 'Invalid urlparam').isAlpha()
, error: (errors) ->
res.json errors
, great: ->
# Continue as usual
@Radagaisus
Copy link
Author

// Compare it to this:

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('There have been validation errors: ' + util.inspect(errors), 500);
    return;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment