colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.95k stars 1.19k forks source link

Strip conditions from a schema #3816

Open t18n opened 2 weeks ago

t18n commented 2 weeks ago

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. :)