This Express server listens for POST requests to /register. It checks the request body against the userSchema. If the data is wrong, it sends back an error message. If correct, it confirms the data is valid.
B. It validates only the email string, not the whole object
C. It uses res.send instead of res.json
D. It should use Joi.number() for email
Solution
Step 1: Check what is validated
The schema expects an object with an email property, but code validates req.body.email (a string).
Step 2: Understand Joi object validation
To validate the whole object, pass req.body to schema.validate, not just one property.
Final Answer:
It validates only the email string, not the whole object -> Option B
Quick Check:
Validate whole object, not single property [OK]
Hint: Validate req.body object, not a single field string [OK]
Common Mistakes:
Validating only a property instead of full object
Confusing res.send and res.json (both work)
Forgetting to call next() is not required here
Using wrong Joi type for email
5. You want to validate a user object with optional phone that must be a string of 10 digits if present, and a required name string. Which Joi schema correctly enforces this?
hard
A. Joi.object({ name: Joi.string().required(), phone: Joi.string().pattern(/\d+/).required() })
B. Joi.object({ name: Joi.string(), phone: Joi.number().length(10).optional() })
C. Joi.object({ name: Joi.string().required(), phone: Joi.string().pattern(/^\d{10}$/).optional() })
D. Joi.object({ name: Joi.string().required(), phone: Joi.string().length(10).required() })
Solution
Step 1: Identify required and optional fields
Name is required string; phone is optional string matching exactly 10 digits.
Step 2: Check regex pattern and optional usage
Pattern /^\d{10}$/ matches exactly 10 digits; phone is optional, so use .optional().
Step 3: Eliminate incorrect options
Other options make phone required, use wrong types like Joi.number(), apply invalid methods like .length(10) on numbers, or use loose patterns like /\d+/.
Final Answer:
Joi.object({ name: Joi.string().required(), phone: Joi.string().pattern(/^\d{10}$/).optional() }) -> Option C