How to transform schema to conform to z.object() shape? #1044
Replies: 2 comments 2 replies
-
Yes, it is possible to write such a function. Valibot schemas store any information in their objects. For example, via |
Beta Was this translation helpful? Give feedback.
-
@fabian-hiller Hi Fabian, thanks for the reply. It appears that Doing simple comparison like this: const zodSchema = z.object({
price: z.number(),
})
console.log(JSON.stringify(zodSchema, null, 2))
const vSchema = v.object({
price: v.number(),
})
console.log(JSON.stringify(vSchema, null, 2)) Prints: // zod
{
"_def": {
"unknownKeys": "strip",
"catchall": {
"_def": {
"typeName": "ZodNever"
},
"~standard": {
"version": 1,
"vendor": "zod"
}
},
"typeName": "ZodObject"
},
"~standard": {
"version": 1,
"vendor": "zod"
},
"_cached": null
}
// valibot
{
"kind": "schema",
"type": "object",
"expects": "Object",
"async": false,
"entries": {
"price": {
"kind": "schema",
"type": "number",
"expects": "number",
"async": false,
"~standard": {
"version": 1,
"vendor": "valibot"
}
}
},
"~standard": {
"version": 1,
"vendor": "valibot"
}
} While stringified Transforming a plain JSON object from one into another would be doable, but this looks like far outside my skill set. Long story short my best bet is to wait for |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am very fond of using valibot in nuxt and have migrated all validator logic to it from
zod
. The recent v3 of nuxt/content has introduced a schema requirement for content like this:I have raised an issue in
nuxt/content
to offer a more generic way to define schema without relying on a specific stack, ideally through https://standardschema.dev.Realistically that’s not going to happen any time soon, so is there any way to work around that and give
nuxt/content
what it needs without maintaining two sets of validator schemas (valibot
application-wide,zod
fornuxt/content
)?Basically a transformer function that maps it to the shape of
ZodObject
.Any help appreciated 🌈
Beta Was this translation helpful? Give feedback.
All reactions