Schema
/ GuidesSchema
/ GuidesConverting Schemas
A common use case is storing your JSON schemas in a database and then converting them to Zod schemas when you need to use them.
Let's imagine we have the following Zod schema:
import { z } from 'zod'
const schema = z.object({
name: z.string(),
age: z.number(),
items: z.array(z.string()),
})
Using it to validate data is straightforward:
const data = {
name: 'John Doe',
age: 30,
items: ['foo', 'bar'],
}
const result = schema.safeParse(data)
if (!result.success) {
console.error(result.error)
}
But what if the schema is dynamic and needs to be stored in a database?
This is where we can use fromZod to convert the Zod schema to a JSON schema and store it in a database:
import { fromZod } from '@weser/schema'
// pseudo-code for the database
import db from './db'
const json = fromZod(schema)
db.save(json)
Now, whenever we need to validate data, we can convert the JSON schema back to a Zod schema using the toZod
function:
import { toZod } from '@weser/schema'
// pseudo-code for the database
import db from './db'
const json = db.load()
const zodSchema = toZod(json)
const result = zodSchema.safeParse(data)
if (!result.success) {
console.error(result.error)
}
© 2024-present Robin Weser. All Rights Reserved.