0
0
Expressframework~10 mins

Schema validation in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Schema validation
Receive HTTP Request
Extract Data from Request
Validate Data Against Schema
Proceed
Next Middleware
The server receives a request, extracts data, checks it against rules (schema), then either continues or sends an error.
Execution Sample
Express
const schema = Joi.object({ name: Joi.string().required() });
app.post('/user', (req, res) => {
  const { error } = schema.validate(req.body);
  if (error) return res.status(400).send(error.message);
  res.send('User valid');
});
This code checks if the request body has a 'name' string; if not, it sends an error, else confirms validity.
Execution Table
StepActionInput DataValidation ResultResponse Sent
1Receive POST /user{ name: 'Alice' }ValidNo
2Validate data{ name: 'Alice' }No errorNo
3Send success response'User valid'
4Receive POST /user{ name: 123 }InvalidNo
5Validate data{ name: 123 }Error: 'name' must be a stringNo
6Send error response400 Bad Request with error message
💡 Stops when data is invalid or after sending success response
Variable Tracker
VariableStartAfter Step 2 (Valid)After Step 5 (Invalid)
req.body{}{ name: 'Alice' }{ name: 123 }
errorundefinedundefined"'name' must be a string"
response status200 (default)200400
response message'''User valid'"'name' must be a string"
Key Moments - 2 Insights
Why does the server send a 400 error when the name is a number?
Because the schema requires 'name' to be a string. The validation fails at step 5 in the execution_table, triggering the error response.
What happens if the request body is missing the 'name' field?
The validation will fail since 'name' is required. This is similar to step 5 where an error is returned and the server sends a 400 response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the validation result at step 2?
AMissing required field
BError: 'name' must be a string
CNo error
DValidation not performed
💡 Hint
Check the 'Validation Result' column at step 2 in the execution_table
At which step does the server send a 400 Bad Request response?
AStep 6
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Response Sent' column for the 400 error in the execution_table
If the schema required an additional 'age' number field, how would the validation result change for input { name: 'Alice' }?
AValidation passes as before
BValidation fails due to missing 'age'
CValidation error on 'name'
DServer crashes
💡 Hint
Consider the schema rules and how missing required fields affect validation in the execution_table
Concept Snapshot
Schema validation in Express:
- Define a schema with rules (e.g., using Joi)
- On request, validate input data against schema
- If valid, proceed to next step
- If invalid, send error response (e.g., 400 Bad Request)
- Helps ensure data correctness before processing
Full Transcript
Schema validation in Express means checking incoming request data against a set of rules called a schema. When the server gets a request, it extracts the data and compares it to the schema. If the data matches the rules, the server continues processing. If not, it sends back an error message and stops. For example, if the schema says 'name' must be a string, and the request sends a number instead, the server will respond with a 400 error. This process helps keep the app safe and working correctly by catching bad data early.