0
0
Expressframework~20 mins

Joi as validation alternative in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Joi Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when Joi validation fails?
Consider an Express route using Joi to validate a request body. If the validation fails, what will be the response sent to the client?
Express
const Joi = require('joi');
const schema = Joi.object({ name: Joi.string().min(3).required() });

app.post('/user', (req, res) => {
  const { error } = schema.validate(req.body);
  if (error) {
    return res.status(400).send(error.details[0].message);
  }
  res.send('User created');
});
AA 400 status with a message describing the first validation error
BA 200 status with 'User created' message regardless of input
CA 500 status with a server error message
DNo response is sent, request hangs
Attempts:
2 left
💡 Hint
Think about what happens when Joi finds a problem in the input.
📝 Syntax
intermediate
2:00remaining
Which Joi schema correctly validates an email and age?
You want to validate a request body with two fields: email (required, valid email) and age (optional, integer, minimum 18). Which Joi schema is correct?
AJoi.object({ email: Joi.email().string().required(), age: Joi.integer().number().min(18) })
BJoi.object({ email: Joi.string().required(), age: Joi.number().integer().max(18) })
CJoi.object({ email: Joi.string().required().email(), age: Joi.number().min(18).integer() })
DJoi.object({ email: Joi.string().email().required(), age: Joi.number().integer().min(18) })
Attempts:
2 left
💡 Hint
Check the order and method names for Joi validation.
🔧 Debug
advanced
2:00remaining
Why does this Joi validation always pass?
This code is supposed to reject empty strings for the username, but it always passes validation. Why?
Express
const schema = Joi.object({ username: Joi.string().min(1) });
const result = schema.validate({ username: '' });
console.log(result.error);
ABecause Joi.string() allows empty strings unless .required() or .empty() is used to forbid them
BBecause .min(1) applies to numbers, not strings, so it does not enforce string length
CBecause the validate method is asynchronous and result.error is always undefined
DBecause Joi.string() allows empty strings by default; .min(1) checks length but empty string length is 0, so it should fail but it doesn't because .min(1) is ignored
Attempts:
2 left
💡 Hint
Think about how Joi treats empty strings by default.
state_output
advanced
2:00remaining
What is the value of validatedData after validation?
Given this code, what will be the value of validatedData after validation?
Express
const schema = Joi.object({ name: Joi.string().trim().required(), age: Joi.number().default(30) });
const input = { name: '  Alice  ' };
const { value: validatedData } = schema.validate(input);
console.log(validatedData);
A{ name: 'Alice' }
B{ name: ' Alice ', age: undefined }
C{ name: 'Alice', age: 30 }
D{ name: ' Alice ', age: 30 }
Attempts:
2 left
💡 Hint
Look at how Joi's trim and default work on the input.
🧠 Conceptual
expert
2:00remaining
Why choose Joi over manual validation in Express?
Which of the following is the strongest reason to use Joi as a validation alternative in Express apps?
AJoi replaces the need for any error handling in Express routes
BJoi provides a declarative, reusable schema to validate and sanitize input, reducing bugs and improving maintainability
CJoi automatically fixes all invalid inputs without developer intervention
DJoi is faster than native JavaScript validation functions in all cases
Attempts:
2 left
💡 Hint
Think about what Joi offers beyond simple checks.