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

88 changes: 87 additions & 1 deletion 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 @@ -433,6 +433,86 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
get:
tags:
- cohort
summary: Get all cohorts
operationId: getAllCohorts
security:
- bearerAuth: []
responses:
'201':
description: success
content:
application/json:
schema:
type: object
properties:
status:
type: string
data:
properties:
cohort:
$ref: '#/components/schemas/Cohort'
'401':
description: fail
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/cohorts/{id}:
get:
tags:
- cohort
summary: Get a cohort by id
operationId: getCohortById
security:
- bearerAuth: []
parameters:
- name: id
in: path
description: 'The cohort id'
required: true
schema:
type: integer
responses:
'201':
description: success
content:
application/json:
schema:
type: object
properties:
status:
type: string
data:
properties:
cohort:
$ref: '#/components/schemas/Cohort'
'401':
description: Unautorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

components:
securitySchemes:
Expand Down Expand Up @@ -460,12 +540,18 @@ components:
properties:
id:
type: integer
cohortName:
type: string
createdAt:
type: string
format: date-time
updatedAt:
type: string
format: date-time
users:
type: array
items:
$ref: '#/components/schemas/User'

AllUsers:
type: object
Expand Down Expand Up @@ -729,7 +815,7 @@ components:
format: date-time
profileImage:
type: string

Error:
type: object
properties:
Expand Down
19 changes: 19 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,22 @@ export async function validatePostOwnership(req, res, next) {
return sendDataResponse(res, 500, { content: 'Internal server error' })
}
}

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()
}
8 changes: 6 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,15 @@ 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