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

Optional arrays are not marked optional in Zod schema #18

Closed
LaCuneta opened this issue Apr 27, 2021 · 1 comment
Closed

Optional arrays are not marked optional in Zod schema #18

LaCuneta opened this issue Apr 27, 2021 · 1 comment

Comments

@LaCuneta
Copy link
Contributor

LaCuneta commented Apr 27, 2021

Bug description

If I have a type with an optional array of items, the result in the Zod schema is not marked optional(). A validation error is generated:

✖ Validating generated types
 ›   Error: 'A' is not compatible with 'aSchema':
 ›   Argument of type 'A' is not assignable to parameter of type '{ values: string[]; }'.
 ›     Types of property 'values' are incompatible.
 ›       Type 'string[] | undefined' is not assignable to type 'string[]'.
 ›         Type 'undefined' is not assignable to type 'string[]'.
error Command failed with exit code 2.

Input

// typescript type or interface causing the output
export type A = {
  values?: Array<string>
}

Expected output

// Generated by ts-to-zod
import { z } from "zod";

export const aSchema = z.object({
  values: z.array(z.string()).optional(),
});

Actual output

// Actual Zod schemas
// Generated by ts-to-zod
import { z } from "zod";

export const aSchema = z.object({
  values: z.array(z.string()),
});

Versions

  • ts-to-zod: v1.0.1
  • Typescript: v4.2.3
  • Zod: v3.0.0-alpha.39
fabien0102 added a commit that referenced this issue May 3, 2021
@fabien0102
Copy link
Owner

Good catch! Thanks for the report!

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