Validation schema withtout default value keys
Is there a way to use a validation schema with missing default values keys :
Something like this :
const userSchema = z.object({
age: z.number().gte(13, 'You must be 13 to make an account'),
})
function App() {
const form = useForm({
defaultValues: {
age: 0,
name: 'toto',
},
validators: {
onChange: userSchema,
},
})
return (
<div>
<form.Field
name="age"
children={(field) => {
return <>{/* ... */}</>
}}
/>
</div>
)
}
If i do this, i have on error on the onChange validator
5 Replies
eastern-cyan•3mo ago
Zod is strict with extra properties. Does
userSchema.passthrough()
fix your issue?
https://v3.zod.dev/?id=passthroughGitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
rare-sapphireOP•3mo ago
i'm using v4, passthrough is deprecated, loose() in v4 dont do the trick
fascinating-indigo•3mo ago
Isn't it
z.looseObject
?
https://zod.dev/v4/changelog?id=deprecates-strict-and-passthrough
Why do you need to pass keys that aren't in your schema?rare-sapphireOP•3mo ago
it's the same,
/** @deprecated Use
z.looseObject()
or .loose()
instead. */
passthrough(): ZodObject<Shape, core.$loose>;
And i need keys in my form that dont require validation
To be more precise, i have lots of complex objects in defaultValues, and dont want to type every attributes of my objects in my schemas, it's not necessary. And i want to use my existing dto/interfaceseastern-cyan•3mo ago
it's not possible to do it on form level yet. Your best option is to use a field-level schema for the ones that have them