Skip to content

Conversation

@AliQassab
Copy link

@AliQassab AliQassab commented Oct 25, 2025

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Complete Sprint 3 prep exercises - WebSockets and Middleware

Questions

Ready to review!

@AliQassab AliQassab added 📅 Sprint 3 Assigned during Sprint 3 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 25, 2025
Copy link

@LonMcGregor LonMcGregor left a comment

Choose a reason for hiding this comment

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

Looking good, though I have a suggestion of something to think about to help practise writing even more reusable middleware

I've also left a general comment about remembering how to make good PRs

Choose a reason for hiding this comment

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

Remember when committing files to make a PR to only include the relevant ones. As this is about the middleware task, do you need to be committing this one?

Copy link
Author

Choose a reason for hiding this comment

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

No, I don’t need it here. I deleted it.

app.post("/", (req, res) => {
// Validate that body is an array
if (!Array.isArray(req.body)) {
return res.status(400).send("Request body must be a JSON array");

Choose a reason for hiding this comment

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

Because you're using the express.json middleware, does that already check something is a JSON array body? Are you giving the correct response here?

Copy link
Author

Choose a reason for hiding this comment

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

express.json only parses and validates JSON syntax; it does not enforce that the body is an array. We still need separate validation for shape and element types.

Yes, the 400 responses for “must be a JSON array” and “all elements must be strings” are correct for validation failures after parsing

}

// Check if all elements are strings
const allStrings = parsed.every((item) => typeof item === "string");

Choose a reason for hiding this comment

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

What is the goal of this middleware - it seems to do two things.

Here you're combining JSON parsing and checking the input is strings. As middleware aims to allow re-use of code, what change could you make to this that would allow you to re-use these separate checks more easily?

Copy link
Author

Choose a reason for hiding this comment

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

Goal of the middleware: Parse JSON POST bodies and validate an array of strings.

The change I made:

  • Split it into two pieces:
  1. express.json() for parsing JSON.
  2. validateArrayOfStrings for validation only.

@LonMcGregor LonMcGregor added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 28, 2025
Copy link

@LonMcGregor LonMcGregor left a comment

Choose a reason for hiding this comment

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

Great work!

@LonMcGregor LonMcGregor added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. 📅 Sprint 3 Assigned during Sprint 3 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants