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

Support for ZodPipeline #115

Closed
jesinmat opened this issue Apr 14, 2023 · 3 comments · Fixed by #118
Closed

Support for ZodPipeline #115

jesinmat opened this issue Apr 14, 2023 · 3 comments · Fixed by #118

Comments

@jesinmat
Copy link

jesinmat commented Apr 14, 2023

Hello,
I'm using Zod Pipelines according to the Zod documentation to safely handle dates. I have the following object:

export const WorkerSchema = z
  .object({
    name: z.string().min(1),
    available: z.array(z.date().or(z.string().min(1).pipe(z.coerce.date()))),
  })
  .strict();

This way, I can either pass date directly or a string of length at least 1 that can be converted to a date.
Using this with zod-to-openapi, the following error is generated:

UnknownZodTypeError {
  message: 'Unknown zod object type, please specify `type` and other OpenAPI props using `ZodSchema.openapi`.',
  data: {
    currentSchema: { in: [ZodString], out: [ZodDate], typeName: 'ZodPipeline' },
    schemaName: undefined
  }
}

Since both ZodString and ZodDate are specified, is it possible to convert this automatically?
What should I do to overcome this issue in the meantime?
And since I use this approach several times in my project, can I specify this conversion method in one place for all of them?

@AGalabov
Copy link
Collaborator

Hi @jesinmat, thanks for using our library and pointing this out. .pipe() is relatively new so we've not looked into it before.

Overall I'd need to check if there is persistent way to make sure that the type is correctly inferred. For example .transform cannot be handled within zod-to-openapi. From what I see in the error I am optimistic.

As for a fix in the meantime - yes there is one. As the message of the error suggests you can just pass .openapi({ type: 'string' ]}) to your schema. And for reusability I guess you can just use regular programming means and do something like:

// In some common file:
export const dateSchema = z.date().or(z.string().min(1).pipe(z.coerce.date()));

// In your current file:
export const WorkerSchema = z
  .object({
    name: z.string().min(1),
    available: z.array(dateSchema),
  })
  .strict();

Let me know if that works for you. And I'll see if we can provide an out of the box support for pipelines.

@AGalabov AGalabov linked a pull request Apr 15, 2023 that will close this issue
@jesinmat
Copy link
Author

Thank you for your quick reply and the pull request!
I ended up using .openapi({ type: "string", format: "date" }) and it works well. This library has helped me a lot, glad to see it's continuing to improve.

AGalabov added a commit that referenced this issue Apr 19, 2023
AGalabov added a commit that referenced this issue Apr 19, 2023
…for-zod-pipelines

#115 added support for ZodPipeline
@AGalabov
Copy link
Collaborator

@jesinmat this should now be available in the v4.6.0 release

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

Successfully merging a pull request may close this issue.

2 participants