Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server side data validation in Express + node.js

My server is comprised of three major components - node.js + express + mongojs. Right now I am looking for a way to plug in server side validation of the client input. Mongoose offers it through its Schema definitions, however, I would like the validation to be independent of the particular DB layer.

Ideally, I am looking for something supporting declarative validation rules (again, like the mongoose schemas), but without any coupling with mongo.

Any ideas?

P.S.

I am mentioning Express, because my intention is to plug the validation layer as an Express middleware. This should work, shouldn't it?

P.P.S.

An extra bonus, if the same javascript code can be utilized on the client side as well.

like image 277
mark Avatar asked Oct 04 '12 21:10

mark


People also ask

How do you validate data in node JS?

const Validator = require('validatorjs'); const validator = async (body, rules, customMessages, callback) => { const validation = new Validator(body, rules, customMessages); validation. passes(() => callback(null, true)); validation. fails(() => callback(validation. errors, false)); }; module.

How do I validate server-side?

The user input validation that takes place on the server side during a post back session is called server-side validation. The languages such as PHP and ASP.Net use server-side validation. Once the validation process on server side is over, the feedback is sent back to client by generating a new and dynamic web page.

What is express validator in node JS?

According to the official website, Express Validator is a set of Express. js middleware that wraps validator. js , a library that provides validator and sanitizer functions. Simply said, Express Validator is an Express middleware library that you can incorporate in your apps for server-side data validation.

Should I use Joi or express validator?

Joi can be used for creating schemas (just like we use mongoose for creating NoSQL schemas) and you can use it with plain Javascript objects. It's like a plug n play library and is easy to use. On the other hand, express-validator uses validator. js to validate expressjs routes, and it's mainly built for express.


1 Answers

I've had good luck with node-validator: https://github.com/chriso/node-validator

like image 129
PherricOxide Avatar answered Sep 20 '22 15:09

PherricOxide