0
0
NextJSframework~20 mins

Schema definition in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
AThrows a runtime error
Btrue
Cfalse
Dundefined
Attempts:
2 left
💡 Hint
Check if all required fields are present in the object.
📝 Syntax
intermediate
2: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.
Az.object({ username: z.string().optional(), email: z.string() })
Bz.object({ username: z.string().required(), email: z.string() })
Cz.object({ username: z.string(), email: z.string().nullable() })
Dz.object({ username: z.string(), email: z.string().optional() })
Attempts:
2 left
💡 Hint
Optional fields use the .optional() method in Zod.
🔧 Debug
advanced
2: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);
ABecause price is negative and parse throws on invalid data
BBecause id is not a string
CBecause parse returns undefined on failure
DBecause the schema is missing a required field
Attempts:
2 left
💡 Hint
Check the value constraints on the price field.
state_output
advanced
2: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;
Atrue
Bfalse
CThrows a TypeError
Dundefined
Attempts:
2 left
💡 Hint
Check the minimum length required for password.
🧠 Conceptual
expert
2: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.
AZod schemas can be used both server-side and client-side for consistent validation.
BZod schemas can only validate client-side data, not server-side.
CZod automatically converts all input data types to strings during validation.
DZod schemas require manual error handling for every validation failure.
Attempts:
2 left
💡 Hint
Think about where Next.js code runs and how Zod works.