Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand Joi's role

    Joi is a library designed to check if data matches certain rules before using it.
  2. Step 2: Compare with other Express tasks

    Routing, database connection, and serving files are handled by other parts of Express or middleware, not Joi.
  3. Final Answer:

    To validate incoming data and ensure it meets expected rules -> Option D
  4. 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

  1. Step 1: Recall Joi syntax for strings

    Joi.string() creates a string schema, and .required() marks it as mandatory.
  2. Step 2: Check method chaining order

    The correct chaining is Joi.string().required(), not Joi.required().string() or others.
  3. Final Answer:

    const schema = Joi.string().required(); -> Option A
  4. 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

  1. Step 1: Understand the schema rules

    The schema requires age to be a number, at least 18, and present.
  2. Step 2: Check the data against rules

    Data has age = 16, which is less than the minimum 18, so validation fails.
  3. Final Answer:

    An error object because age is less than 18 -> Option C
  4. 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

  1. Step 1: Check data type for email

    Email expects a string, but data has a number (12345), which is invalid.
  2. Step 2: Confirm Joi email method usage

    Joi.string().email() is correct syntax; parentheses are present.
  3. Final Answer:

    The email field is not a string, so validation fails -> Option B
  4. 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

  1. Step 1: Check required fields

    Name and email must be required, so .required() must be on both.
  2. Step 2: Check optional age with min 0

    Age is optional, so no .required(), but min(0) applies.
  3. 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.
  4. Final Answer:

    Joi.object({ name: Joi.string().required(), age: Joi.number().min(0), email: Joi.string().email().required() }) -> Option A
  5. Quick Check:

    Required name/email, optional age min 0 = C [OK]
Hint: Required fields get .required(), optional just rules [OK]
Common Mistakes:
  • Marking optional age as required
  • Missing .required() on name or email
  • Confusing order of chaining methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard