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

[BUG]: typescript-zod can't handle top-level arrays (either no code generated at all, or only the inner element type for arrays of objects) #2680

Open
hi2u opened this issue Jan 29, 2025 · 1 comment
Labels

Comments

@hi2u
Copy link

hi2u commented Jan 29, 2025

When the input sample data is an array, e.g a single sample input of:

[{"key":"one"},{"key":"two"}]

The generated code for most languages like Go/Rust/Haskell/C# etc will logically be a Vec/List/array etc.

But when using output language typescript-zod, it does NOT generate a Zod schema/type that is an array, only a schema/type for the internal array element type.

This is inconsistent with other languages, and makes it very hard to use the lib in a consistent way when you're expecting the generated types to match the sample data you gave it.

Simple arrays of scalars produce nothing at all

Additionally... if you give an input that is a simple array of scalar values, e.g:

[1,2,3]

or

["a","b","c"]

...then absolutely no code is generated at all, aside from the import at the top: import * as z from "zod";

Context (Environment, Version, Language)

Input Format: JSON
Output Language: typescript-zod

I'm seeing this:

Expected Behaviour / Output

  • Match the input sample data, including when it's an array. As it does for other languages.

Current Behaviour / Output

  • For arrays of objects: No array schema/type is generated. Therefore the generated type does not always match the sample data given.
  • For simple arrays of scalars: no code is generated at all

Steps to Reproduce

  1. Go to https://app.quicktype.io/
  2. In source type "JSON", paste one of either: [1,2,3] -or- [{"key":"one"},{"key":"two"}]
  3. Pick output language: TypeScript Zod
  4. Note that either no code in generated, or for object arrays, there's no array type.
  5. Switch to other languages like Go/Haskell/Rust/C#, and you'll see that most of them give you a Vec/List/array type at the top-level
@hi2u hi2u added the bug label Jan 29, 2025
@hi2u
Copy link
Author

hi2u commented Jan 29, 2025

Using JSON Schema as an input also fails, e.g input of:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "array",
    "items": {
        "type": "integer"
    },
    "definitions": {}
}

Generates no Zod code at all (aside from import line). But works for most other output languages.

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

No branches or pull requests

1 participant