-
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.
Merge pull request #20 from atlp-rwanda/fix-userSignup-validation-#18…
…7431242 fix(validation): implement validation for sign up
- Loading branch information
Showing
8 changed files
with
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,14 +44,14 @@ test('should return 409 when registering with an existing email', async () => { | |
.send(userData); | ||
expect(response.status).toBe(409); }, 20000); | ||
|
||
test('should return 500 when registering with an invalid credential', async () => { | ||
test('should return 400 when registering with an invalid credential', async () => { | ||
const userData = { | ||
email: '[email protected]', name: "", username: 'existinguser', }; | ||
const response = await request(app) | ||
.post('/api/v1/users/register') | ||
.send(userData); | ||
|
||
expect(response.status).toBe(500); }, 20000); }); | ||
expect(response.status).toBe(400); }, 20000); }); | ||
|
||
test("should return all users in db --> given '/api/v1/users'", async () => { | ||
const spy = jest.spyOn(User, "findAll"); | ||
|
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,35 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import validator from 'email-validator'; | ||
import { Schema } from 'joi'; | ||
|
||
export const emailValidation = (req: Request, res: Response, next: NextFunction) => { | ||
const { email } = req.body; | ||
|
||
if (!email) { | ||
return res.status(400).json({ | ||
status: 400, | ||
message: "Email is required" | ||
}); | ||
} | ||
|
||
const isValid = validator.validate(email); | ||
if (isValid) { | ||
next(); | ||
} else { | ||
return res.status(400).json({ | ||
status: 400, | ||
message: "Email is not valid." | ||
}); | ||
} | ||
}; | ||
|
||
|
||
export const validateSchema = (schema: Schema) => { | ||
return (req: Request, res: Response, next: NextFunction) => { | ||
const { error } = schema.validate(req.body); | ||
if (error) { | ||
return res.status(400).json({ message: error.details[0].message }); | ||
} | ||
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
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 Joi from "joi"; | ||
|
||
export const signUpSchema = Joi.object({ | ||
name: Joi.string() | ||
.min(5) | ||
.max(40) | ||
.required(), | ||
username: Joi.string() | ||
.min(4) | ||
.required(), | ||
email: Joi.string() | ||
.min(6) | ||
.required() | ||
.email(), | ||
password: Joi.string() | ||
.min(6) | ||
.max(20) | ||
.required() | ||
}).options({ allowUnknown: false }); | ||
|
||
export default signUpSchema |
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
File renamed without changes.