backend-validation-express
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -0,0 +0,0 @@ import { NextFunction, Request, Response } from "express"; |
@@ -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", |
122
README.md
# 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!" | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7278
127