const {
check,
body,
cookie,
header,
param,
query,
checkSchema,
oneOf,
matchedData,
validationResult
} = require('express-validator');
//
// This is a mock of the express request.
// Feel free to modify it to match whatever the request you're sending
// to your HTTP server.
//
const req = {
body: {
count1: 1,
count2: {},
count3: "",
count4: true,
count5: [],
count6: [1],
},
cookies: {},
headers: {},
query: {},
params: {},
query: {},
};
//
// This should be the validation chain you're using.
// Feel free to add all the validators and sanitizers that you might need!
// For the documentation, please see:
// - https://express-validator.github.io/docs/validation-chain-api.html
// - https://express-validator.github.io/docs/sanitization-chain-api.html
//
const chain = await check(['count1','count2','count3','count4','count5','count6'])
.isInt()
.toInt();
// Runs the validation chain.
// The returned value is the same as calling validationResult(req).
// See docs at https://express-validator.github.io/docs/validation-chain-api.html#runreq-options
const result = await chain.run(req);
// Grabs the data that was matched, validated and sanitized by express-validator in the req object.
// Feel free to modify it to match what you're doing in your app!
// See docs at https://express-validator.github.io/docs/matched-data-api.html
const data = matchedData(req);
// Finally, print the result of the validation and what its matched data is!
console.log({ result, data });