Validating req.params with express validator
express-validator custom validator
express-validator/check query params
express-validator/check if exists
express-validator required field
express-validator isint
express-validator schema
express-validator npm
I want the user to be able to write a specific account number in the endpoint, been trying to validate the endpoint param if it exists in my database. I couldn't get it to work, please what am I doing wrong?
My validation
const validateReq: [ param('accountNumber').exists().custom(acctNo => accountNumberExist(acctNo)),]
My accountNumberExist function
accountNumberExist(inputAcct) { const isfound = accounts.find(account => account.accountNumber === inputAcct); if (isfound === undefined) throw new Error('Account Number not found'); }
My accounts file
const accounts = [ { id: 1, accountNumber: 1234567890, createdOn: new Date(), owner: 1, type: 'current', balance: 23444.43, status: 'active', }, { id: 2, accountNumber: 1234167890, createdOn: new Date(), owner: 1, type: 'savings', balance: 2233444.43, status: 'active', }, { id: 3, accountNumber: 9987654321, createdOn: new Date(), owner: 2, type: 'saving', balance: 73444.43, status: 'active', }, ];
But this is always throwing the 'Account Number not found' error even though, the req.param exists in my accounts database.
Params are parsed as string
by express middleware. Say I make a req to path defined below like /some/1000
app.get('/some/:path', (req, res, next) => { console.log(typeof req.param.path) // outputs string })
So you need to parse the incoming parameter to integer (Number) since you've stored accountNumber
as integer. So adding toInt
to chain like below should solve it:
const validateReq: [ param('accountNumber').exists().toInt().custom(acctNo => accountNumberExist(acctNo)), ]
Validating req.params with express validator, Params are parsed as string by express middleware. Say I make a req to path defined below like /some/1000 app.get('/some/:path', (req, res,� Validating req.params with express validator. Ask Question Asked 1 year, 2 months ago. Active 9 months ago. Viewed 4k times 1. 1. I want the user to be able to write
accountNumber inside accounts array is a number whereas req.params.accountNumber is a string. You need to convert the data type. You can do it as
accountNumberExist(inputAcct) { const isfound = accounts.find(account => account.accountNumber.toString() === inputAcct); if (isfound === undefined) throw new Error('Account Number not found'); }
Validating input in Express using express-validator, Learn how to validate any data coming in as input in your Express Say you have a POST endpoint that accepts the name, email and age parameters: const app = express() app.use(express.json()) app.post('/form', (req,� Same as check([fields, message]), but only checking req.params. query([fields, message]) Same as check([fields, message]), but only checking req.query. checkSchema(schema) schema: the schema to validate. Must comply with the format described in Schema Validation. Returns: an array of validation chains. oneOf(validationChains[, message])
I think that the problem is your query. find
method runs in an asynchronous way, that's why isfound
property does not contain the data you expect. Here is a simple approach using promises
which works pretty well for me.
// Here is your function. accountNumberExist(inputAcct) { return accounts.find({accountNumber: inputAcct}) .then(result => { if (result.length == 0) { console.log("Account Number not found"); return Promise.reject('Account Number not found'); } return Promise.resolve(); }); }
How to validated req.params new api � Issue #442 � express , Sorry, I did not hit how to validate export const rules = [ check('id').exists().custom ((value, { req }) => { const { id } = req.body.params return� This changed when the API for validator.js switched to accepting only string inputs. In order to not introduce breaking changes to express-validator, those same guidelines from validator.js were introduced to express-validator so express-validator would comply with the new API for validator.js.
How to make input validation simple and clean in your Express.js app, js framework, validation plays a crucial role in any web app which requires you to validate the request body param query . Writing your own� express-validator. An express.js middleware for validator. Installation; Documentation; Changelog; License; Installation npm install express-validator Also make sure that you have Node.js 8 or newer in order to use it. Documentation. Please refer to the documentation website on https://express-validator.github.io. Changelog. Check the GitHub
Validate your request parameters using validation middleware in , a request parameter validation middleware in Node.js and reduce the amount of code that you repeat. Tagged with javascript, node, express,� In this tutorial, you’ll learn how to validate input in an Express.js app using an open source and popular module called express-validator. Introduction to express-validator. The definition on Github says: express-validator is a set of express.js middlewares that wraps validator.js validator and sanitizer functions.
How to validate your request parameters easily using middleware in , You can choose whichever you want to use. Edit 1: Using Express-Validation for � In this tutorial you’ll learn how to validate input in express.js app using an open source and popular module express-validator. Introduction to express-validator. Definition on github says: express-validator is a set of express.js middlewares that wraps validator.js validator and sanitizer functions. Module implements 5 important API’s
Comments
- As the express-validator maintainer: this would be the preferred way of solving this ;)
- The OP is using plain arrays. No async stuff involved.
- @gustavohenke where is the problem in this code? It is also recommended from the official docs express-validator.github.io/docs/…
- As said above, the OP is using plain arrays, the
.find()
mentioned above isArray.prototype.find
-- not some kind of DB query or API call.