Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

express-validator-errors

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-validator-errors

Error serializer for validation errors found in express-validator

latest
Source
npmnpm
Version
2.1.0
Version published
Weekly downloads
49
25.64%
Maintainers
1
Weekly downloads
 
Created
Source

express-validator-errors

Error json serializer for consistent errors when using express-validator

Combines errors from different sources:

  • errors in serializer function argument
  • errors in req.body.errors
  • errors in req.validationErrors()

Usage Examples

Data Validation

errors.addToReq - add your own errors to to express-validator's _validationErrors without mixing in new validators errors.serialize - outputs all known validation errors

var errors = require('express-validator-errors')

var isValid = function (req) {
  if (!req.body.objects)
    errors.addToReq(req, 'objects', 'Root objects array is required', req.body)

  req.checkBody(['objects', 0, 'title'], 'Title is required').notEmpty()

  return req.validationErrors().length == 0
}

var expressReqHandler = function (req, res) {
  if (isValid(req)) {
    // ... happiness
  } else {
    res.json(400, errors.serialize(req))
  }
}

// example input =>
// { objects: [{}] }

// outputs =>
// { 
//   "errors": [
//     {
//       "param": "objects.0.title",
//       "msg": "Title is required"
//     }
//   ]
// }

Error Handling

errors.serialize - outputs all known validation errors and the specific errors you ask it to output. Options are:

  • Null (in which case, errors on req are serialized)
  • Single error
  • Array of errors
  • Single Error object
var errors = require('express-validator-errors')

var expressReqHandler = function (req, res) {
  doSomethingThatSometimesErrors(function (err, data) {
    if (err) 
      return res.json(500, errors.serialize(new Error('An error occurred'), req(
    
    // ... happiness
  }
}

// example output =>
// { 
//   "errors": [
//     { 
//       "exception": {
//         "message": "An error occurred",
//         "stack": "... file stack trace ..."
//       }
//     }
//   ]
// }

Keywords

node-validator

FAQs

Package last updated on 02 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts