Strip conditions from a schema
I would like to be able to achieve this
const myZodSchema = z.object({
property: z.integer().min(5).max(10)
property2: z.integer().min(5).max(10)
})
const myZodSchemaOnlyCheckType = z.stripKeywords(myZodSchema, ['min', 'max'])
// Should return
z.object({
property: z.integer()
property2: z.integer()
})
Is there any way to do that?
Some context
I am building an app which uses Drizzle, Zod and Open AI.
On my app, I defined a Drizzle schema and generate a zod schema from it.
import { createInsertSchema } from 'drizzle-zod';
export const profiles = pgTable(
'profiles',
{
headline: varchar('headline', { length: 255 }).notNull(),
},
);
export const insertProfileSchema = createInsertSchema(profiles);
then send the schema over to Open AI to get Structured output.
The generated Zod schema would include the length check. However, OpenAI doesn't support some keyword at the moment, including maxLength. So that's why I am trying to work around this.
I've tried with createSelectSchema from drizzle-zod without luck.
Any other more elegant solutions are welcomed. :)
Hi, @t18n. I'm Dosu, and I'm helping the Zod team manage their backlog. I'm marking this issue as stale.
Issue Summary:
- You are looking for a way to remove specific validation conditions like
minandmaxfrom a Zod schema. - The goal is to maintain compatibility with OpenAI's structured output, which does not support these keywords.
- You are using Drizzle, Zod, and OpenAI in your application.
- You attempted using
createSelectSchemafromdrizzle-zodbut haven't found success. - There have been no comments or developments on the issue yet.
Next Steps:
- Please let me know if this issue is still relevant to the latest version of the Zod repository. You can keep the discussion open by commenting on the issue.
- Otherwise, the issue will be automatically closed in 7 days.
Thank you for your understanding and contribution!
I'm interested in something similar. I am using Zod for form validation (with react-hook-form). I have a schema like the following:
const schema = z.object({
name: z.string().optional().refine((val) => val !== undefined),
});
const FormInput = z.input<typeof schema>; // { name: string | undefined }
const FormOutput = z.output<typeof schema>; // name: string
When the form is submitted, I need to validate the user-entered data against the Zod schema, which I can do with schema.parse(). However, there are instances where I want to parse some initial data against the input Zod schema.
For example, the user might save a partially-filled form. In this case, it's possible that nothing has been filled in yet, and so name is still undefined. The JSON is stored and later retrieved.
const initialDataString = db.query(query);
const initialDataObject = JSON.parse(initialDataString); // type: unknown
const initialFormData = schema.parseInputSchema(initialDataObject); // type: FormInput
Another use-case would be storing the initial form data in the URL search params. I am using TanStack Router which uses Zod to parse the URL search params, but there is no way to parse an input schema.
The hypothetical parseInputSchema would ignore checks, refinements, min/max, transforms, etc. and would result in the z.input<typeof schema> type instead of the output type.
Another potential solution would be something like const inputSchema = z.stripValidation(schema) and then I can just use regular z.parse().
Hi @t18n! I'm Dosu and I’m helping the zod team.
There isn't a built-in .stripKeywords method in Zod to remove specific validation keywords like min, max, or length. However, you can achieve this by recursively traversing your schema and reconstructing it without the unwanted checks. This involves accessing internal properties (like .checks or .def) and filtering out the checks you don't want. Be aware that this approach relies on internal APIs, which may change between Zod versions, so use with caution and test thoroughly if you upgrade Zod in the future.
Here's a relevant community discussion with example code for extracting and manipulating checks in Zod v4: GitHub Discussion #4621. The same thread also suggests an alternative: convert your Zod schema to JSON Schema using zod-to-json-schema, then remove unsupported keywords from the resulting JSON Schema before sending it to OpenAI.
If you want to stick with Zod, you'll need to write a custom utility that walks your schema tree and rebuilds each node, omitting the specified checks. If you want a code example or further guidance on this, let me know!
If this answers your question, please close the issue.
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other
This isn't stale. I still think there's value in this being part of Zod itself. It would be useful to be able to either call schema.parseInput() or getInputSchema(schema).parse() to parse against an input schema that doesn't run refinements or validations.
As the linked GitHub issue mentioned, relying on Zod internals is potentially brittle and could change between versions.