Skip to content

toJSONSchema problem with object and enum #5817

@Garthi

Description

@Garthi

i found a bug: convertingan element containing an enum JSON schema works when it contains only the enum, but it fails when the enumis nested inside an object.

const test = z.enum(['h1', 'h2', 'h3', 'h4']).default('h2');

console.log(z.toJSONSchema(test));

/*
{
  '$schema': 'https://json-schema.org/draft/2020-12/schema',
  default: 'h2',
  type: 'string',
  enum: [ 'h1', 'h2', 'h3', 'h4' ]
}
 */

const test2 = z.object({
    test: test,
});

console.log(z.toJSONSchema(test2));
/*
{
  '$schema': 'https://json-schema.org/draft/2020-12/schema',
  type: 'object',
  properties: { type: { default: 'h2', type: 'string', enum: [Array] } },
  required: [ 'type' ],
  additionalProperties: false
}
 */

the enum data is missing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions