Challenge - 5 Problems
Next.js Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Next.js schema validation?
Given this Next.js schema using Zod, what will be the result of validating { age: 20 }?
NextJS
import { z } from 'zod'; const userSchema = z.object({ name: z.string().min(1), age: z.number().int().min(18), }); const result = userSchema.safeParse({ age: 20 }); console.log(result.success);
Attempts:
2 left
💡 Hint
Check if all required fields are present in the object.
✗ Incorrect
The schema requires both name and age. Since name is missing, validation fails and result.success is false.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Next.js schema for a user with optional email?
Choose the correct Zod schema that defines a user object with a required
username string and an optional email string.Attempts:
2 left
💡 Hint
Optional fields use the .optional() method in Zod.
✗ Incorrect
Option D correctly marks email as optional. Option D makes username optional which is wrong. Option D allows email to be null but not missing. Option D uses .required() which is not a Zod method.
🔧 Debug
advanced2:00remaining
Why does this Next.js schema validation throw an error?
Examine the code and identify why validation throws an error instead of returning a result.
NextJS
import { z } from 'zod'; const productSchema = z.object({ id: z.number(), price: z.number().positive(), }); const data = { id: 1, price: -5 }; const result = productSchema.parse(data); console.log(result);
Attempts:
2 left
💡 Hint
Check the value constraints on the price field.
✗ Incorrect
The price field must be positive. Since -5 is negative, parse throws an error instead of returning a result.
❓ state_output
advanced2:00remaining
What is the value of
isValid after this Next.js schema check?Given this code, what is the value of
isValid after validation?NextJS
import { z } from 'zod'; const loginSchema = z.object({ email: z.string().email(), password: z.string().min(8), }); const input = { email: 'user@example.com', password: '1234567' }; const isValid = loginSchema.safeParse(input).success;
Attempts:
2 left
💡 Hint
Check the minimum length required for password.
✗ Incorrect
The password is only 7 characters but the schema requires at least 8, so validation fails and isValid is false.
🧠 Conceptual
expert2:00remaining
Which statement about Next.js schema definitions with Zod is correct?
Select the only true statement about using Zod schemas in Next.js for data validation.
Attempts:
2 left
💡 Hint
Think about where Next.js code runs and how Zod works.
✗ Incorrect
Zod schemas are JavaScript objects and can be used anywhere JavaScript runs, including both server and client in Next.js, ensuring consistent validation.