Skip to content

Instantly share code, notes, and snippets.

@sbalay
Created June 5, 2018 15:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sbalay/cccf80145391c31d0c5bbe91058a1175 to your computer and use it in GitHub Desktop.
Express validator example
const { checkSchema } = require('express-validator/check');
exports.validateCalculate = [
checkSchema({
product: {
in: ['query'],
errorMessage: 'product must be an integer',
exists: true,
isInt: { options: { min: 0 } },
toInt: true
},
unit_price: {
in: ['query'],
errorMessage: 'unit_price must be a positive float',
exists: true,
isFloat: { options: { min: 0 } },
toFloat: true
},
total_cost: {
in: ['query'],
errorMessage: 'total_cost must be a positive float',
exists: true,
isFloat: { options: { min: 0 } },
toFloat: true
},
dispatched_at: {
in: ['query'],
errorMessage: 'dispatched_at must be a date time',
exists: true,
custom: {
options: value => {
return !!value && !!Date.parse(value);
}
},
toDate: true
}
})
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment