This Express app uses Joi to check user data sent to /user. If data is wrong, it sends an error message. If data is good, it confirms success.
Express
const express = require('express');
const Joi = require('joi');
const app = express();
app.use(express.json());
const userSchema = Joi.object({
name: Joi.string().min(3).max(30).required(),
email: Joi.string().email().required(),
age: Joi.number().integer().min(0).max(120)
});
app.post('/user', (req, res) => {
const { error, value } = userSchema.validate(req.body);
if (error) {
return res.status(400).send({ message: error.details[0].message });
}
res.send({ message: 'User data is valid', data: value });
});
app.listen(3000, () => console.log('Server running on port 3000'));
OutputSuccess
Important Notes
Joi gives detailed error messages to help fix data problems.
Always validate data before using it to keep your app safe.
You can customize Joi rules to fit your exact needs.
Summary
Joi helps check data easily and clearly.
Use Joi to avoid bugs from bad data.
It works well with Express to keep your app safe and clean.
Practice
(1/5)
1. What is the main purpose of using Joi in an Express application?
easy
A. To serve static files like images and CSS
B. To handle routing between different pages
C. To connect to a database
D. To validate incoming data and ensure it meets expected rules
Solution
Step 1: Understand Joi's role
Joi is a library designed to check if data matches certain rules before using it.
Step 2: Compare with other Express tasks
Routing, database connection, and serving files are handled by other parts of Express or middleware, not Joi.
Final Answer:
To validate incoming data and ensure it meets expected rules -> Option D
Quick Check:
Joi validates data = A [OK]
Hint: Joi is all about checking data rules fast [OK]
Common Mistakes:
Confusing Joi with routing or database tools
Thinking Joi serves files
Assuming Joi manages app navigation
2. Which of the following is the correct way to define a Joi schema for a required string named username?
easy
A. const schema = Joi.string().required();
B. const schema = Joi.required().string();
C. const schema = Joi.string.required();
D. const schema = Joi.string().isRequired();
Solution
Step 1: Recall Joi syntax for strings
Joi.string() creates a string schema, and .required() marks it as mandatory.
Step 2: Check method chaining order
The correct chaining is Joi.string().required(), not Joi.required().string() or others.
Final Answer:
const schema = Joi.string().required(); -> Option A
Quick Check:
Joi.string().required() syntax = B [OK]
Hint: Use Joi.string().required() to make a string mandatory [OK]
Common Mistakes:
Placing .required() before .string()
Using .isRequired() which is not a Joi method
Missing parentheses after string()
3. Given this Joi schema and data:
const schema = Joi.object({ age: Joi.number().min(18).required() });
const data = { age: 16 };
const result = schema.validate(data);
What will result.error contain?
medium
A. Null because Joi does not check numbers
B. Null because age is valid
C. An error object because age is less than 18
D. An error object because age is missing
Solution
Step 1: Understand the schema rules
The schema requires age to be a number, at least 18, and present.
Step 2: Check the data against rules
Data has age = 16, which is less than the minimum 18, so validation fails.
Final Answer:
An error object because age is less than 18 -> Option C
Quick Check:
Age 16 < 18 causes error = D [OK]
Hint: Min 18 means below 18 triggers error [OK]
Common Mistakes:
Thinking 16 passes min(18)
Assuming missing age error instead
Believing Joi ignores number checks
4. You wrote this code to validate an email:
const schema = Joi.object({ email: Joi.string().email() });
const data = { email: 12345 };
const result = schema.validate(data);
What is the main problem here?
medium
A. The email method is missing parentheses
B. The email field is not a string, so validation fails
C. Joi.string() cannot be used with email validation
D. The schema object is missing .required()
Solution
Step 1: Check data type for email
Email expects a string, but data has a number (12345), which is invalid.
Step 2: Confirm Joi email method usage
Joi.string().email() is correct syntax; parentheses are present.
Final Answer:
The email field is not a string, so validation fails -> Option B
Quick Check:
Email must be string, got number = A [OK]
Hint: Email must be string, numbers cause validation error [OK]
Common Mistakes:
Ignoring data type mismatch
Thinking .email() needs no parentheses
Assuming .required() is always needed
5. You want to validate a user object with name (string, required), age (optional number, min 0), and email (required valid email). Which Joi schema correctly enforces these rules?
hard
A. Joi.object({ name: Joi.string().required(), age: Joi.number().min(0), email: Joi.string().email().required() })
B. Joi.object({ name: Joi.string(), age: Joi.number().min(0).required(), email: Joi.string().email() })
C. Joi.object({ name: Joi.string().required(), age: Joi.number().min(0).required(), email: Joi.string().email() })
D. Joi.object({ name: Joi.string(), age: Joi.number(), email: Joi.string().email().required() })
Solution
Step 1: Check required fields
Name and email must be required, so .required() must be on both.
Step 2: Check optional age with min 0
Age is optional, so no .required(), but min(0) applies.
Step 3: Validate each option
Joi.object({ name: Joi.string().required(), age: Joi.number().min(0), email: Joi.string().email().required() }) matches all rules exactly; others miss required or optional correctly.