Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backend-validation-express

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backend-validation-express - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

0

lib/index.d.ts

@@ -0,0 +0,0 @@ import { NextFunction, Request, Response } from "express";

4

lib/index.js

@@ -14,3 +14,3 @@ "use strict";

try {
const errorMessages = {};
let errorMessages = {};
let errorExist = false;

@@ -47,3 +47,3 @@ (_a = Object.keys(validationSchema)) === null || _a === void 0 ? void 0 : _a.map((key) => {

errorExist = true;
const messages = {};
let messages = {};
(_a = error.details) === null || _a === void 0 ? void 0 : _a.map((err) => {

@@ -50,0 +50,0 @@ messages[err.path[0]] = err.message;

{
"name": "backend-validation-express",
"version": "1.0.3",
"version": "1.0.4",
"description": "Request data validation middleware using Joi for express in backend",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

# backend-validation-express
#### Request data validation middleware using Joi for express in backend
##### Request data validation middleware using Joi for express in backend. This middleware allows you to validate req.body, req.query, req.params, req.headers. Also return the error messages to the better format.

@@ -8,4 +8,120 @@ ## Installation

## Homepage
## Example
- [https://github.com/MuhammadRifat/backend-validation-express#readme](https://github.com/MuhammadRifat/backend-validation-express#readme)
##### validation.js
```js
const { validator, Joi } = require("backend-validation-express");
const bodySchema = Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).required()
});
const querySchema = Joi.object({
_id: Joi.string().required(),
name: Joi.string().required(),
});
const paramsSchema = Joi.string().required();
const bodyValidator = validator({ body: bodySchema });
const queryValidator = validator({ query: querySchema });
const paramsValidator = validator({ params: paramsSchema });
module.exports = {
bodyValidator,
queryValidator,
paramsValidator
};
```
##### index.js
```js
const express = require("express");
const { paramsValidator, queryValidator, bodyValidator } = require("./validation");
const app = express();
app.use(express.json());
app.post("/user", bodyValidator, (req, res, next) => {
// Here, req.body has been validated.
return res.status(200).json(req.body);
});
app.get("/user", queryValidator, (req, res, next) => {
// Here, req.query has been validated.
return res.status(200).json(req.query);
});
app.listen(3000, () => {
console.log("Server listening on the port 3000");
});
```
### You can add multiple schema in single validator. Example:
##### validation.js
```js
const { validator, Joi } = require("backend-validation-express");
const bodySchema = Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).required(),
role: Joi.string()
});
const querySchema = Joi.object({
_id: Joi.string().required(),
role: Joi.string().required(),
});
const commonValidator = validator({ body: bodySchema, query: querySchema });
module.exports = { commonValidator };
```
### Error Messages Format
##### Error message for validating only req.body in single validator
```sh
{
"success": false,
"errors": {
"body": {
"name": "name is required",
"email": "email is required",
"password": "password is required"
}
},
"message": "Validation Error!"
}
```
##### Error message for validating req.body & req.query in single validator
```sh
{
"success": false,
"errors": {
"body": {
"name": "name is required",
"email": "email is required",
"password": "password is required"
},
"query": {
"_id": "_id is required",
"role": "role is required"
}
},
"message": "Validation Error!"
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc