Skip to content

Commit

Permalink
fixing validations
Browse files Browse the repository at this point in the history
  • Loading branch information
amiparadis250 committed Mar 1, 2024
1 parent d6b371d commit e1cc7b5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
6 changes: 3 additions & 3 deletions --Test--/sample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('blogs controllers',()=>{
const response=await supertest(app).delete(`/api/blogs/'+${4455}`)
.set('Authorization', `Bearer ${ Authtoken }`);
expect(response.statusCode).toBe(500);
expect(response.body.status).toBe('error')

});

it("POST api/blogs/:id :Updating blog", async()=>{
Expand Down Expand Up @@ -278,8 +278,8 @@ describe("Creation Comments", () => {
})
.set('Authorization', `Bearer ${Authtoken}`);

expect(response.statusCode).toBe(500);
expect(response.body.status).toBe('error');
expect(response.statusCode).toBe(403);

});

it("POST api/blogs/:id/comments: Creating Comments with valid blogID", async () => {
Expand Down
8 changes: 8 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"development"
],
"hints": {
"typescript-config/strict": "off"
}
}
3 changes: 2 additions & 1 deletion src/routes/commentRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
} from '../controllers/commentsCtl';
import { isLogin } from '../middlewares/isLogin';
import isAdmin from '../middlewares/isAdmin';
import validateComments from '../validations/commentsvalidation';

const commentRoutes = express.Router();

commentRoutes.post('/:id/comments', isLogin, addComment);
commentRoutes.post('/:id/comments', isLogin,validateComments ,addComment);
commentRoutes.delete('/:id/comments/:commentId', isLogin, isAdmin, deleteComment);
commentRoutes.get('/:id/comments/:commentId', getOneComment);
commentRoutes.get('/:id/comments', getAllCommentsForBlog);
Expand Down
13 changes: 7 additions & 6 deletions src/validations/QuerriesValidation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import joi from 'joi';
export const createQuerryValidationSchema =joi.object({
email: joi.string().email().required(),
guestName: joi.string().pattern(/^[a-zA-Z\s]{5,}$/).required(),
guestQuery: joi.string().min(10).required(),
import joi from 'joi'

export const createQuerryValidationSchema = joi.object({
email: joi.string().email().required(),
guestName: joi.string().pattern(/^[a-zA-Z\s]{5,}$/).required(),
guestQuery: joi.string().min(5).required(),
});


const querryValidation = async (req, res, next) => {
const value = createQuerryValidationSchema.validate(req.body, { abortEarly: false });
if (value.error) {
return res.status(403).send({message:"Invalid message details",error:value.error});
return res.status(403).send({message:"Invalid message details",error:value.error.details[0].message});
} else {
next();
}
Expand Down
10 changes: 0 additions & 10 deletions src/validations/blogsValidatio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ export const updateBlogValidation = Joi.object({
content: Joi.string().max(5000),
});

// export const addCommentValidation = Joi.object({
// text: Joi.string().required().max(1000),
// commenterName: Joi.string().required().max(255),
// commenterEmail: Joi.string().email().required().max(255),
// });

// export const updateCommentValidation = Joi.object({
// text: Joi.string().max(1000),
// commenterName: Joi.string().max(255),
// commenterEmail: Joi.string().email().max(255),
// });


17 changes: 17 additions & 0 deletions src/validations/commentsvalidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import joi from 'joi'

export const commentsValidationSchema = joi.object({
text: joi.string().required().max(1000).trim().message('Please enter a valid comment'),
commenterName: joi.string().required().max(255).regex(/^[A-Za-z\s]+$/).trim().message('Please enter a valid name without special characters'),
commenterEmail: joi.string().email().required().max(255).trim().message('Please enter a valid email'),
});

const validateComments = async (req, res, next) => {
const value = commentsValidationSchema.validate(req.body, { abortEarly: false });
if (value.error) {
return res.status(403).send({message:"Invalid comments details",error:value.error.details[0].message});
} else {
next();
}
};
export default validateComments
4 changes: 2 additions & 2 deletions src/validations/userValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const validateUserSchema= joi.object({
password:joi.string().min(12).regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/)
.message('Password must be at least 12 characters long and include at least one lowercase letter, one uppercase letter, one digit, and one special character.'),
fullName:joi.string().min(3).max(70).regex(/^[a-zA-Z]+(?: [a-zA-Z]+)*$/)
.message('Full name must be between 3 and 70 characters and should not include special characters.'),
.message('Your names must be between 3 and 70 characters and should not include special characters.'),
})
const usersValidation = async (req:any, res:any, next) => {
const value = validateUserSchema.validate(req.body, { abortEarly: false });
if (value.error) {
return res.status(400).json({error: value.error.details});
return res.status(400).res.json({ error: value.error.details[0].message});
} else {
next();
}
Expand Down

0 comments on commit e1cc7b5

Please sign in to comment.