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

Using .transform() stops z.discriminatedUnion from working #2315

Closed
thehappycheese opened this issue Apr 10, 2023 · 2 comments
Closed

Using .transform() stops z.discriminatedUnion from working #2315

thehappycheese opened this issue Apr 10, 2023 · 2 comments
Labels
stale No activity in last 60 days

Comments

@thehappycheese
Copy link

Hi there,

May I please have some help I am stuck with an issue which seems to be caused by SomeZodType.transform(fn=>...) when fn returns a type which is incompatible with the original SomeZodType. Whenever I use .transform() the types will no longer work in z.discriminatedUnion.
Is this intended behavior, or am I missing something?
Please excuse the really long example code below;

// === USER CODE ===

class Position extends Component {
    position = new Vector2(0, 0);
    // ...
    static readonly schema = z.object({
        type: z.literal("Position"),
        position:Vector2.schema
    }).strict().transform(data=>new Position(data.position));
}

class Velocity extends Component {
    velocity = new Vector2(0, 0);
    // ...
    static readonly schema = z.object({
        type: z.literal("Position"),
        position:Vector2.schema
    }).strict().transform(data=>new Position(data.position));
}

// === LIBRARY CODE ===
export class Vector2{
    //...
    static readonly schema = z.object({
        type: z.literal("Vector2"),
        x: z.number().default(0),
        y: z.number().default(0),
    }).strict().transform(data=> new Vector2(data.x, data.y));
}

class Entity {
    readonly uid: string;
    label:string;
    private components: Map<string, Component<any>>;
    //...
    static readonly schema = z.object({
        type: z.literal("Entity"),
        uid: z.string().nonempty(),
        label:z.string().nonempty().optional(),
        components: ????????????????????????????
    }).strict().transform(data=>new Entity(data.uid, new Map(data.components.map(???)), data.label ?? data.uid));
}

At this point I am stuck because

  1. Entity is part of a library; user code is supposed to implement arbitrary Component types and give each implementation its own static schema=..., therefore I can't give Entity.components static schema inside the Entity class
  2. I want to do
    • user creates list components = [Velocity.schema, Position.schema]
    • user calls Entity.register_components(components)
    • Entity.schema is updated with z.discriminatedUnion("type", components)
    • User uses the parser as below
Entity.schema.parse({
    type:"Entity",
    uid:"xxxx123",
    label:"the thing",
    components:[
        {
            type:"Position",
            position:{type:"Vector2", x:10, y:10}
        },
        {
            type:"Velocity",
            velocity:{type:"Vector2"}
        }
    ]
})

Whenever I try to make a function that passes a list of component schemas into z.discriminatedUnion I get a massive error which is hard to understand;

Type 'ZodEffects<ZodObject<{ type: ZodLiteral<"Velocity">; velocity: ZodDefault<ZodOptional<ZodEffects<ZodObject<{ type: ZodLiteral<"Vector2">; x: ZodDefault<ZodNumber>; y: ZodDefault<ZodNumber>; }, "strict", ZodTypeAny, { ...; }, { ...; }>, Vector2, { ...; }>>>; }, "strict", ZodTypeAny, { ...; }, { ...; }>, Velocity, { ....' is missing the following properties from type 'ZodObject<{ type: ZodTypeAny; } & ZodRawShape, UnknownKeysParam, ZodTypeAny, { [x: string]: any; type?: any; }, { [x: string]: any; type?: any; }>': _cached, _getCached, shape, strict, and 14 more.ts(2740)
(property) Velocity.schema: z.ZodEffects<z.ZodObject<{
    type: z.ZodLiteral<"Velocity">;
    velocity: z.ZodDefault<z.ZodOptional<z.ZodEffects<z.ZodObject<{
        type: z.ZodLiteral<"Vector2">;
        x: z.ZodDefault<z.ZodNumber>;
        y: z.ZodDefault<z.ZodNumber>;
    }, "strict", z.ZodTypeAny, {
        ...;
    }, {
        ...;
    }>, Vector2, {
        ...;
    }>>>;
}, "strict", z.ZodTypeAny, {
    ...;
}, {
    ...;
}>, Velocity, {
    ...;
}>

I tried my best to find any existing issues or questions... this one looks related? #2146 . There was a suggestion in that thread to try zod@canary, but that doesn't seem to have helped.

@HenryNguyen5
Copy link

Also related to: #1171

@stale
Copy link

stale bot commented Jul 12, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No activity in last 60 days label Jul 12, 2023
@stale stale bot closed this as completed Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale No activity in last 60 days
Projects
None yet
Development

No branches or pull requests

2 participants