[BUG]: Using --lang typescript-zod, the generated code ignores regex fields
Opened this issue · 0 comments
Issue Type
Context (Environment, Version, Language)
Input Format: JSON Schema written in YAML
Output Language: typescript-zod
CLI, npm, or app.quicktype.io: "npx quicktype" using Node.js v20.12
Version: 23.0.115
Description
I've generated some JSON Schema's, some of which must use regular expressions to enforce string formats. But, the generated Zod code does not include the regex patterns.
Input Data
This file is saved as regex.yaml
$schema: "https://json-schema.org/draft/2020-12/schema"
$id: "https://example.com/project.schema.json"
title: "Project"
description: "A product in the catalog"
type: "object"
properties:
duration1:
description: |
A string conforming with ISO8601 duration strings, enforced
by a regular expression pattern.
type: string
format: regex
pattern: '^(-?)P(?=\d|T\d)(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)([DW]))?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$'
duration2:
description: |
A string conforming with ISO8601 duration strings, enforced
by a regular expression pattern. This time without the regex format.
type: string
pattern: '^(-?)P(?=\d|T\d)(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)([DW]))?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$'
year:
description: |
String of four digits meant to represent a year
type: string
pattern: '^[0-9][0-9][0-9][0-9]$'
The pattern
attribute is documented here: https://www.learnjsonschema.com/2020-12/validation/pattern/
Expected Behaviour / Output
The generated Zod code should include:
z.string().regex(regex);
This is documented at: https://zod.dev/?id=strings
Current Behaviour / Output
The generated code is:
import * as z from "zod";
export const RegexSchema = z.object({
"duration1": z.string().optional(),
"duration2": z.string().optional(),
"year": z.string().optional(),
});
export type Regex = z.infer<typeof RegexSchema>;
Notice that the regex
calls are missing.
Steps to Reproduce
Using the above YAML, run:
npx quicktype -s schema ./regex.yaml --lang typescript-zod --out regex.ts
Possible Solution
ADDENDUM - RELATED ISSUE
This is almost certainly the same issue. Two properties with type: number
with minimum
and maximum
attributes, there is nothing in the Zod code:
station_lon:
description: |
Station center WSG84-encoded longitude in decimal degrees to at least 4 decimal places.
Valid longitudes are between -180 and 180
type: number # float(>3) default=no part 680=yes
minimum: -180
maximum: 180
station_lat:
description: |
Station center WSG84 encoded latitude in decimal degrees to at least 4 decimal places.
Valid latitudes are between -90 and 90.
type: number # float(>3) default=no part 680=yes
minimum: -90
maximum: 90
The corresponding part of the generated Zod schema is:
"station_lat": z.number(),
"station_lon": z.number(),
ADDENDUM 2 -- typescript-effect
Using the above example with typescript-effect-schema gives a similar result:
npx quicktype -s schema ./regex.yaml --lang typescript-effect-schema --out regex-effect.ts
Output:
export class RegexEffect extends S.Class<RegexEffect>("RegexEffect")({
"duration1": S.optional(S.union(S.null, S.string)),
"duration2": S.optional(S.union(S.null, S.string)),
"year": S.optional(S.union(S.null, S.string)),
}) {}