glideapps/quicktype

converting a recursive json schema into a zod type breaks

Open

#2,508 创建于 2024年2月16日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)TypeScript (10,867 star) (968 fork)batch import
TS Zodbuggood first issueinput:JSON Schema

描述

example self referential schema:

{
  "id": "http://json-schema.org/geo",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "description": "A geographical coordinate",
  "type": "object",
  "definitions":{
    "Mydef1":{
        "type":"object",
        "properties":{
            "test":{
                "$ref":"#/definitions/Mydef1"
            }
            
        }
    }
  },
  "properties": {
    "latitude": {
       "$ref":"#/definitions/Mydef1"
    }
  }
}

produces:

export const Mydef1Schema = z.object({
    "test": z.union([Mydef1Schema, z.null()]).optional(),
});
export type Mydef1 = z.infer<typeof Mydef1Schema>;

export const CoordinateSchema = z.object({
    "latitude": z.union([Mydef1Schema, z.null()]).optional(),
});
export type Coordinate = z.infer<typeof CoordinateSchema>;

but it should be https://github.com/colinhacks/zod#recursive-types

=> z.lazy(() => Mydef1Schema)

export const Mydef1Schema = z.object({
    "test": z.union([z.lazy(() =>Mydef1Schema), z.null()]).optional(),
});
export type Mydef1 = z.infer<typeof Mydef1Schema>;

export const CoordinateSchema = z.object({
    "latitude": z.union([Mydef1Schema, z.null()]).optional(),
});
export type Coordinate = z.infer<typeof CoordinateSchema>;

贡献者指南