📖 Scenario: You are building a simple NestJS API to register users. You want to make sure the data sent by clients is correct before saving it.
🎯 Goal: Create a Joi validation schema to check user data, then apply it in a NestJS DTO to validate incoming requests.
📋 What You'll Learn
Create a Joi schema for user data with exact fields
Add a configuration variable for minimum password length
Use the Joi schema in a NestJS DTO class
Apply the validation pipe in the controller method
💡 Why This Matters
🌍 Real World
Validating user input is essential in APIs to prevent bad data and security issues. Joi is a popular library for this in Node.js and NestJS.
💼 Career
Backend developers often use validation libraries like Joi to ensure data integrity and improve API reliability.
Progress0 / 4 steps
1
Create Joi schema for user data
Create a constant called userSchema using Joi.object() with these exact fields: username as a required string, email as a required string with email format, and password as a required string.
NestJS
Hint
Use Joi.object() to define the schema and Joi.string().required() for each field. For email, add .email().
2
Add minimum password length configuration
Create a constant called MIN_PASSWORD_LENGTH and set it to 8. Then update the password field in userSchema to require a minimum length using .min(MIN_PASSWORD_LENGTH).
NestJS
Hint
Define MIN_PASSWORD_LENGTH as 8, then use .min(MIN_PASSWORD_LENGTH) on the password field.
3
Use Joi schema in NestJS DTO
Create a class called CreateUserDto. Add a static property called schema and assign it the userSchema constant.
NestJS
Hint
Create a class and add a static property schema that points to userSchema.
4
Apply validation pipe in controller
In a NestJS controller method called createUser, use the @UsePipes(new JoiValidationPipe(CreateUserDto.schema)) decorator above the method. The method should accept a parameter createUserDto of type CreateUserDto.
NestJS
Hint
Use @UsePipes(new JoiValidationPipe(CreateUserDto.schema)) above the createUser method and accept createUserDto as a parameter with @Body().
Practice
(1/5)
1. What is the main purpose of using Joi in a NestJS application?
easy
A. To style the user interface
B. To manage database connections
C. To handle HTTP routing
D. To validate and enforce rules on incoming data
Solution
Step 1: Understand Joi's role
Joi is a library used to define schemas that describe how data should look and to check if data matches those rules.
Step 2: Identify the correct purpose in NestJS
In NestJS, Joi is used to validate incoming data to ensure it meets expected formats and constraints before processing.
Final Answer:
To validate and enforce rules on incoming data -> Option D
Quick Check:
Joi = Data validation [OK]
Hint: Joi is for checking data shapes and rules [OK]
Common Mistakes:
Confusing Joi with database or routing tools
Thinking Joi styles UI elements
Assuming Joi manages server configuration
2. Which of the following is the correct way to define a Joi schema for a required string field named username?
easy
A. Joi.string().required()
B. Joi.number().required()
C. Joi.boolean().optional()
D. Joi.array().required()
Solution
Step 1: Identify the data type for username
A username is typically a string, so the schema should start with Joi.string().
Step 2: Make the field required
Adding .required() ensures the field must be present and not empty.
Final Answer:
Joi.string().required() -> Option A
Quick Check:
String + required = Joi.string().required() [OK]
Hint: Use Joi.string().required() for mandatory text fields [OK]
Common Mistakes:
Using wrong Joi types like number or boolean for strings
Forgetting to add .required() for mandatory fields
A. email should be Joi.email() instead of Joi.string().email
B. password should not be required
C. Missing parentheses after email() method
D. min(8) is invalid for strings
Solution
Step 1: Review the email field syntax
The email method in Joi is a function and must be called with parentheses: email().
Step 2: Identify the missing parentheses
The code uses Joi.string().email without parentheses, which is incorrect and will cause an error.
Final Answer:
Missing parentheses after email() method -> Option C
Quick Check:
Joi methods require () to execute [OK]
Hint: Always add () after Joi methods like email() [OK]
Common Mistakes:
Omitting parentheses on Joi method calls
Misunderstanding required() necessity
Thinking min() is invalid for strings
5. You want to validate a user object with name (required string), age (optional number, minimum 0), and email (required valid email). Which Joi schema correctly applies 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(1).optional(), email: Joi.string().email().required() })
D. Joi.object({ name: Joi.string().required(), age: Joi.number().min(0).required(), email: Joi.string().email().required() })
Solution
Step 1: Check each field's requirements
Name must be a required string, age is optional number with min 0, email is required valid email string.
Step 2: Match schema to requirements
Joi.object({ name: Joi.string().required(), age: Joi.number().min(0), email: Joi.string().email().required() }) matches all: name is required string, age is number with min 0 (optional by default), email is required email string.
Step 3: Verify other options
Joi.object({ name: Joi.string(), age: Joi.number().min(0).required(), email: Joi.string().email() }) makes age required and email optional, Joi.object({ name: Joi.string().required(), age: Joi.number().min(1).optional(), email: Joi.string().email().required() }) uses min(1) for age (wrong min), Joi.object({ name: Joi.string().required(), age: Joi.number().min(0).required(), email: Joi.string().email().required() }) makes age required (wrong).