Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a configuration for ZodPipeline #56

Closed
steckhelena opened this issue Apr 24, 2023 · 1 comment
Closed

Add a configuration for ZodPipeline #56

steckhelena opened this issue Apr 24, 2023 · 1 comment

Comments

@steckhelena
Copy link

The parser for the ZodPipeline could be configurable like the one for ZodEffects. Sometimes the pipeline is only an internal transformation we may not want in the generated schema.

Currently, it works as follows:

const dateModel = z.object({
  year: z.number().min(2021).max(2023),
  month: z.number().min(1).max(12),
  day: z.number().min(1).max(31),
})

const stringInputDate = z
  .string()
  .regex(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)
  .transform((date) => {
    const [year, month, day] = date.split('-')

    return {
      year: Number(year),
      month: Number(month),
      day: Number(day),
    }
  }).pipe(dateModel)

const schema = zodToJsonSchema(stringInputDate)

which will generate something like:

{
  "allOf": [
    { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
    {
      "type": "object",
      "properties": {
        "year": { "type": "number", "minimum": 2021, "maximum": 2023 },
        "month": { "type": "number", "minimum": 1, "maximum": 12 },
        "day": { "type": "number", "minimum": 1, "maximum": 31 }
      },
      "required": ["year", "month", "day"],
      "additionalProperties": false
    }
  ],
  "$schema": "http://json-schema.org/draft-07/schema#"
}

I propose the addition of a pipelineStrategy: 'input' | 'all' which would then make the output be:

{
  "type": "string",
  "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
  "$schema": "http://json-schema.org/draft-07/schema#"
}

if the input strategy was selected.

@StefanTerdell
Copy link
Owner

Added in 3.20.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants