Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support for literal types #16

@boredland

Description

@boredland

Hi there!

When using toZod on this interface:

export type Position = [number, number];

export interface Polygon {
    type: "Polygon";
    coordinates: [Position[],Position[]];
    bbox?: [number, number, number, number] | [number, number, number, number, number, number]
}

I stumble across the missing support for unions (#4), because I neither can use a tuple .or another, nor an array.

And I can't set a literal for "type", because toZod expects "never" instead of a literal.

I'd expect this to be a strict match for it:

export const polygonValidation = z.object({
  type: z.literal('Polygon'),
  coordinates: z
    .array(z.array(z.tuple([z.number(), z.number()])).min(4))
    .min(1)
    .max(2),
  bbox: z
    .union([
      z.tuple([z.number(), z.number(), z.number(), z.number()]),
      z.tuple([
        z.number(),
        z.number(),
        z.number(),
        z.number(),
        z.number(),
        z.number(),
      ]),
    ])
    .optional(),
});

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