0
0
NestJSframework~20 mins

Validation with Joi in NestJS - 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 happens when a NestJS controller uses Joi validation and receives invalid data?

Consider a NestJS controller method that uses Joi to validate incoming request data. If the data does not match the Joi schema, what will be the behavior?

NestJS
import * as Joi from 'joi';

const schema = Joi.object({
  username: Joi.string().min(3).required(),
  age: Joi.number().integer().min(18).required()
});

// Assume this schema is used in a validation pipe in a controller method
AThe controller method runs normally but with undefined or default values for invalid fields.
BThe invalid fields are automatically corrected to default valid values and the method runs.
CThe request is rejected and a 400 Bad Request error is sent with validation error details.
DThe server crashes with an unhandled exception due to Joi validation failure.
Attempts:
2 left
💡 Hint

Think about how NestJS validation pipes handle schema validation failures.

📝 Syntax
intermediate
2:00remaining
Which Joi schema correctly validates an object with an optional email and a required password of at least 8 characters?

Choose the Joi schema that correctly matches the requirements:

  • email: optional, must be a valid email if present
  • password: required, string, minimum length 8
AJoi.object({ email: Joi.string().email().required(), password: Joi.string().min(8) })
BJoi.object({ email: Joi.string().email(), password: Joi.string().min(8).required() })
CJoi.object({ email: Joi.string().optional(), password: Joi.string().min(8).required() })
DJoi.object({ email: Joi.email().optional(), password: Joi.string().min(8).required() })
Attempts:
2 left
💡 Hint

Remember that by default fields are optional unless marked required.

🔧 Debug
advanced
2:00remaining
Why does this Joi validation schema cause a runtime error?

Examine the following Joi schema and identify the cause of the runtime error:

NestJS
const schema = Joi.object({
  name: Joi.string().min(3),
  age: Joi.number().integer().min(0),
  email: Joi.string().email().required(),
});
AThe schema object must be wrapped in Joi.object().keys() instead of Joi.object().
BThe min(3) on name is invalid because min only works on numbers.
CJoi.number().integer() is invalid syntax and causes the error.
DThe .required method is missing parentheses, so email is not marked required correctly.
Attempts:
2 left
💡 Hint

Check the syntax of method calls on Joi validators.

state_output
advanced
2:00remaining
What is the output of this Joi validation result?

Given the schema and input below, what is the value of result.error after validation?

NestJS
const schema = Joi.object({
  username: Joi.string().alphanum().min(3).max(10).required(),
  password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')).required()
});

const input = { username: 'ab', password: '123' };

const result = schema.validate(input);
AAn error object indicating username is too short
BAn error object indicating both username and password are invalid
CAn error object indicating password pattern mismatch
Dnull (no error, validation passed)
Attempts:
2 left
💡 Hint

Check the username length and password pattern against the input.

🧠 Conceptual
expert
3:00remaining
How does Joi validation integrate with NestJS pipes for request validation?

Which statement best describes how Joi schemas are used within NestJS validation pipes to validate incoming requests?

AJoi schemas are passed to a custom validation pipe that runs validation and throws exceptions on failure, integrating with NestJS exception filters.
BNestJS automatically converts Joi schemas to class-validator decorators without extra code.
CJoi validation is only used on the client side and does not integrate with NestJS pipes.
DNestJS uses Joi schemas internally by default without needing to define validation pipes.
Attempts:
2 left
💡 Hint

Think about how validation pipes work and how Joi can be used in them.