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.
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.
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.
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.
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.
I've had good luck with node-validator: https://github.com/chriso/node-validator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With