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

Prev Previous commit
Next Next commit
simplified function
eyvmal committed Oct 30, 2024

Unverified

This user has not yet uploaded their public signing key.
commit f9c842fefd9b76cc3a81e91a55490dcdf6a28acc
22 changes: 10 additions & 12 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
@@ -1,21 +1,19 @@
import { sendDataResponse } from '../utils/responses.js'

export async function validatePostContent(req, res, next) {
const validateContent = (content) => {
const maxLength = 200 // Set maximum length
const { content } = req.body
const maxLength = 200

if (!content || content.trim() === '') {
return 'Content cannot be empty or null'
}
if (content.length > maxLength) {
return `Content cannot exceed ${maxLength} characters`
}
return null
if (!content || content.trim() === '') {
return sendDataResponse(res, 400, {
content: 'Content cannot be empty or null'
})
}

const contentError = validateContent(req.body.content)
if (contentError) {
return sendDataResponse(res, 400, { content: contentError })
if (content.length > maxLength) {
return sendDataResponse(res, 400, {
content: `Content cannot exceed ${maxLength} characters`
})
}

next()