Skip to content

Instantly share code, notes, and snippets.

@jbutko
Created December 5, 2016 10:18
Show Gist options
  • Save jbutko/ce6a109b3feef2a8238cb8216731d3f0 to your computer and use it in GitHub Desktop.
Save jbutko/ce6a109b3feef2a8238cb8216731d3f0 to your computer and use it in GitHub Desktop.
Custom express validators via express-validator
// http://stackoverflow.com/questions/37339479/express-validator-to-validate-paramenter-which-is-an-array#answer-37342458
var expressValidator = require('express-validator');
var validator = require('validator');
app.use(expressValidator({
customValidators: {
isArray: function(value) {
return Array.isArray(value);
},
notEmpty: function(array) {
return array.length > 0;
}
gte: function(param, num) {
return param >= num;
}
}
}));
req.checkBody('category', 'category cannot be empty').isArray().notEmpty();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment