Managing Default Values in Zod Schemas
Q: How do you handle default values in a Zod schema?
- Zod
- Mid level question
Explore all the latest Zod interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Zod interview for FREE!
In a Zod schema, you can handle default values using the `default` method, which allows you to specify a default value that will be used if no value is provided during validation. This method can be chained after defining a schema for the field.
For example, if you have a schema for a user object where you want to set a default role of "user," you can implement it as follows:
```javascript
import { z } from 'zod';
const userSchema = z.object({
username: z.string(),
email: z.string().email(),
role: z.string().default('user'), // Default value set to 'user'
});
// Example without providing a role
const result = userSchema.parse({
username: 'john_doe',
email: '[email protected]',
});
console.log(result);
// Output:
// {
// username: 'john_doe',
// email: '[email protected]',
// role: 'user' // role is assigned the default value
// }
```
If a value for `role` is provided in the input, Zod will use that instead of the default value. For example:
```javascript
const resultWithRole = userSchema.parse({
username: 'jane_doe',
email: '[email protected]',
role: 'admin', // Providing a role
});
console.log(resultWithRole);
// Output:
// {
// username: 'jane_doe',
// email: '[email protected]',
// role: 'admin' // role is assigned the provided value
// }
```
To clarify, the `default` method ensures that a specified value is automatically applied when no value is given for that property in the input. This is particularly useful for maintaining consistent defaults across your data flows and reducing the need for additional logic to check for missing values.
For example, if you have a schema for a user object where you want to set a default role of "user," you can implement it as follows:
```javascript
import { z } from 'zod';
const userSchema = z.object({
username: z.string(),
email: z.string().email(),
role: z.string().default('user'), // Default value set to 'user'
});
// Example without providing a role
const result = userSchema.parse({
username: 'john_doe',
email: '[email protected]',
});
console.log(result);
// Output:
// {
// username: 'john_doe',
// email: '[email protected]',
// role: 'user' // role is assigned the default value
// }
```
If a value for `role` is provided in the input, Zod will use that instead of the default value. For example:
```javascript
const resultWithRole = userSchema.parse({
username: 'jane_doe',
email: '[email protected]',
role: 'admin', // Providing a role
});
console.log(resultWithRole);
// Output:
// {
// username: 'jane_doe',
// email: '[email protected]',
// role: 'admin' // role is assigned the provided value
// }
```
To clarify, the `default` method ensures that a specified value is automatically applied when no value is given for that property in the input. This is particularly useful for maintaining consistent defaults across your data flows and reducing the need for additional logic to check for missing values.


