Middleware to validade all form entries express js, node js - node.js

Is there some middleware function to validate all entries in forms for node js and express js?
I want to check for special characteres and i don't want to validade each form field at each time.
Thanks!

The express-validator could be a good fit. It is built upon validator.js, a popular validation library.
The express-validator is also frequently updated.
1. Set up and init
expressValidator = require('express-validator');
app.use(expressValidator([options]));
see available options
2. Configure the validation
req.checkBody('postparam', 'Invalid postparam').notEmpty().isInt();
req.checkParams('urlparam', 'Invalid urlparam').isAlpha();
req.checkQuery('getparam', 'Invalid getparam').isInt();

Related

Node.js express passport joi - First validated body/joy or check passport req?

I have a Node.js express passport joi restful api. When trying to access the routes ... is it better to first validated the inputs (body or query) or check passport requirements?
So this:
router.route('/signup')
.post(validateBody(schemas.signupSchema), passportSignup, controllerSignup.signup);
router.route('/login')
.post(validateBody(schemas.loginSchema), passportLogin, controllerLogin.login);
router.route('/search')
.get(validateQuery(schemas.searchSchema), passportJWT, controllerSearch.search);
... or this?
router.route('/signup')
.post(passportSignup, validateBody(schemas.signupSchema), controllerSignup.signup);
router.route('/login')
.post(passportLogin, validateBody(schemas.loginSchema), controllerLogin.login);
router.route('/search')
.get(passportJWT, validateQuery(schemas.searchSchema), controllerSearch.search);
Which version is preferred?
It doesn't really matter as, both of them are middlewares, doesn't matter which one fails first. But, doing passport authentication first is a better idea cause, why even process the data if the user isn't correctly authenticated.

Sanitization and Validations for Sails JS

I'm using sails js 0.11. Are the inputs using req.body or req.params.all() sanitized? If not, what should be done to sanitize them?
Secondly, for validations - where can the validations be done? Eg: req.params.all().id should be an integer type.
For sanitazing you can write hook or service
Validation rules should be described in your models

Generating Joi validation a Sequelize model

I have developed an API using expressjs and Sequelize is the ORM I have used. I want to integrate express-validation to my API to validate the request body and params. The express-validation framework uses the Joi validation rules. But as I have already defined the validation rules in my Sequalize model, I' don't like to redefine validation rules using Joi for request body validations.
I'm just wondering if there's any method or library to generate Joi validation rules based on validations defined in Sequelize model. Else, what would be the best approach to handle this?
Have you checked out joi-sequelize ?
This question is too old however this answer maybe helpful for those newbies who are learning the language or maybe started working with sequelize
Sequelize provides validation by default. You can look into the docs
Sequelize Validation docs
However if you want to provide your own custom validators with custom "pretty" messages. You can always use the
#hapi/joi
package the "normal" way. joi-sequelize or sequelize-joi are not required just to provide custom error message.(period)
function validateData(datas) {
const schema = Joi.object({
user_name: Joi.string().min(3).required(),
user_address: Joi.string().required()
});
return schema.validate(datas);
}
and then validate the data using
const { error } = validateData(req.body);
which catches if any properties fails the validation.

How to pass multiple parameters to the express js server api in a mean.js application?

The default serverside router for a mean.js application for a single parameter request is like this :
app.route('/articles/:articleId')
.get(articles.read)
.put(users.requiresLogin, articles.hasAuthorization, articles.update)
.delete(users.requiresLogin, articles.hasAuthorization, articles.delete);
// Finish by binding the article middleware
app.param('articleId', articles.articleByID);
How do I parse multiple parameters in the middleware for the request say :
app.route('/articles/:articleId/:lineId')
How do I define the middleware for this ?
I have tried doing :
app.param(['articleId','pageId'],articles.articleByID);
but it fails. Even the express.js API says that an array of arguments can be passed but it's vague on it's implementation.
I even tried accessing req.param in the middleware definition but it returned undefined.
Somebody please guide as to how to implement it?

How to Handle a Coinbase Callback in NodeJS?

How to Handle a Coinbase Callback in NodeJS to Recieve Instant Bit Coin Payment Notifications ?
Please I need example.
Note : I'm using SailsJS MVC Framework.
OK, based on your comment, I will give it a go.
I am assuming you have (or will have) an ExpressJs app.
UPDATE Sorry, I just noticed you're using sailsjs. The below should still be valid but you'll need to adapt it to work with the sails routing engine.
In your app, you need to define the post route:
// the app variable is the express js server
// name the route better than this...
app.post('/coinbase', function(req, res){
var data = req.body;
var orderId = data.order.id;
// etc...
});

Resources