-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(validation):joi validation for login
-login data from request.body are validated by joi [Fixes #187419169]
- Loading branch information
1 parent
b9bd13e
commit 304e8e1
Showing
6 changed files
with
43 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Request,Response,NextFunction } from "express"; | ||
export const dataValidation = async(req:Request,res:Response,next:NextFunction,data:any) => { | ||
const {error} = data.validate(req.body); | ||
if(error){ | ||
return res.status(406) | ||
.json({ | ||
status:406, | ||
mesage:`Error in User Data : ${error.message}` | ||
}) | ||
} | ||
const allowedFields = Object.keys(data.describe().keys); | ||
const unknownFields = Object.keys(req.body).filter(field => !allowedFields.includes(field)); | ||
if (unknownFields.length > 0) { | ||
return res.status(406).json({ | ||
status: 406, | ||
message: `Unknown fields: ${unknownFields.join(", ")}` | ||
}); | ||
}else{ | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Joi from "joi"; | ||
import { Request,Response,NextFunction } from "express"; | ||
import { dataValidation } from "../helpers/validation"; | ||
const loginValidation:any = Joi.object({ | ||
email:Joi.string().email().trim(true).required(), | ||
password:Joi.string().min(8).trim(true).required(), | ||
}).options({ abortEarly: false }); | ||
|
||
export const loginDataValidation = async(req:Request,res:Response,next:NextFunction) =>{ | ||
await dataValidation(req,res,next,loginValidation); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters