Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12 implement create post validation to check for text sizeempty text #43

6 changes: 3 additions & 3 deletions docs/openapi.yml
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this dont need a change

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ openapi: 3.0.3
info:
title: Team Dev Server API
description: |-
version: "1.0"
version: '1.0'

servers:
- url: "http://localhost:4000/"
- url: 'http://localhost:4000/'
tags:
- name: user
- name: post
Expand Down Expand Up @@ -729,7 +729,7 @@ components:
format: date-time
profileImage:
type: string

Error:
type: object
properties:
Expand Down
205 changes: 136 additions & 69 deletions package-lock.json
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this dont need a change

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this dont need a change

Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"eslint-plugin-promise": "^5.1.0",
"nodemon": "^2.0.15",
"prettier": "^2.6.2",
"prisma": "^3.12.0"
"prisma": "^5.21.1"
},
"dependencies": {
"@prisma/client": "^3.12.0",
"@prisma/client": "^5.21.1",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
Expand Down
20 changes: 20 additions & 0 deletions src/middleware/post.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good!

Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ export async function validatePostOwnership(req, res, next) {
return sendDataResponse(res, 500, { content: 'Internal server error' })
}
}
import { sendDataResponse } from '../utils/responses.js'

export async function validatePostContent(req, res, next) {
const { content } = req.body
const maxLength = 200

if (!content || content.trim() === '') {
return sendDataResponse(res, 400, {
content: 'Content cannot be empty or null'
})
}

if (content.length > maxLength) {
return sendDataResponse(res, 400, {
content: `Content cannot exceed ${maxLength} characters`
})
}

next()
}
7 changes: 5 additions & 2 deletions src/routes/post.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good!

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
deleteById
} from '../controllers/post.js'
import { validateAuthentication } from '../middleware/auth.js'
import { validatePostOwnership } from '../middleware/post.js'
import {
validatePostOwnership,
validatePostContent
} from '../middleware/post.js'

const router = Router()

router.post('/', validateAuthentication, create)
router.post('/', validateAuthentication, validatePostContent, create)
router.get('/', validateAuthentication, getAll)
router.get('/:id', validateAuthentication, getById)
router.patch('/:id', validateAuthentication, validatePostOwnership, updateById)
Expand Down