Skip to content

Default entity values

Andreas Söderlund edited this page Mar 7, 2023 · 3 revisions

Used when returning default values from superValidate for an entity, or when a FormData field is empty.

type value
string ""
number 0
boolean false
object {}
Array.isArray []
bigint BigInt(0)
symbol Symbol()

This whole behavior can be turned off if you pass options.implicitDefaults = false to superValidate, which means that you must add default to all required fields of your schema.

Non-supported defaults

Some Zod types like ZodEnum and ZodUnion can't use the above default values, so an exception will be thrown if you don't set a default value for them yourself, for example:

const schema = z.object({
  fish: z.enum(['Salmon', 'Tuna', 'Trout']).default('Salmon')
});

// If it's nullable/optional/nullish, no need for a default (but can still be set).
const schema = z.object({
  fish: z.enum(['Salmon', 'Tuna', 'Trout']).nullable()
});

Also, see this FAQ question if you need to set a value to something not included in the type.

Clone this wiki locally