Skip to content

Default entity values

Andreas Söderlund edited this page Feb 26, 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') // or nullable/optional/nullish
});

Let me know if you find something that could or should be supported regarding this.

Clone this wiki locally