StefanTerdell / zod-to-json-schema

Converts Zod schemas to Json schemas
ISC License
886 stars 71 forks source link

Types with default not added to required? #96

Closed ciscoheat closed 7 months ago

ciscoheat commented 10 months ago

Hello, thank you for a great library! It will be a part of Superforms v2 for sure!

I have a question about default and require. When I add a default to a property in an object schema, the property isn't added to the object's required property. Which conflicts with the inferred type:

const schema = z.object({
  agree: z.literal(true).default(true)
})

type T = z.infer<typeof schema>
// {
//   agree: true // Not optional!
// }

const jsonSchema = zodToJsonSchema(schema)
jsonSchema.required?.includes('agree') // falsy

Is there a setting for making this conform to the inferred schema?

StefanTerdell commented 10 months ago

This one's a little trickier than you'd initially expect. Whether or not a property with a default value should be required or not basically comes down to which JSON Schema validator you use. The only single source of truth that I know of comes from the other side of the equation - Zod itself.

import { z } from "zod";

const schema = z.object({ requiredProperty: z.string().default("some value") });

type Input = z.input<typeof schema> // { requiredProperty?: string | undefined }
type Output = z.output<typeof schema> // { requiredProperty: string }

const data = schema.parse({}) // This is fine

console.log(data); // { requiredProperty: "some value" }

Due to design decisions made for the Zod API we can't target the output type (aka infer), only the input type.

Unless there is a part of the JSON Schema specification (which I may well have missed) that defines this behavior conclusively I'm leaning towards keeping it as is.

ciscoheat commented 10 months ago

Ok, and with Superforms targeting the output type is the primary thing, or at least has been for version 1. But I'll take this into consideration for sure, thank you. Would it still be possible to add it as an option, even though you're focusing on the input type? If it's too much work or goes against your design, no problem.

StefanTerdell commented 10 months ago

Well, Zod allows chaining transformational functions into the schemas that affect the output type, and those obviously can't be serialized. So it's not really up to me unfortunately :/

However, if you wanted to really target this specific use-case, you could rebuild the way optionality is checked in the object parser and add an option to ignore default values there. I'd be open to a PR for it. I might even take a stab at it myself at some point but I'm not making any promises.