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

Rename property of another Zod object with Transform method #3908

Open
reisblucas opened this issue Dec 12, 2024 · 0 comments
Open

Rename property of another Zod object with Transform method #3908

reisblucas opened this issue Dec 12, 2024 · 0 comments

Comments

@reisblucas
Copy link

Possible problem:

When I have some defined schema and I want to change only the property value from it to be validated on PARSE, it not work as expected.

Inside application, the type infers correctly, like my object.ids, can access the possible object with the new property name, but on PARSE, seems to not recognize the renamed property it expects to the last one, e.g., disabled_group.

I have another schema with the same shape, just need to change the property name validation, so I tried with transform, maybe I missunderstood the usage of it.

Make a new schema with ids name, works fine! So I'm following this way at the time

e.g.:

// zod helper
import { ZodEffects, ZodType } from 'zod';

export namespace ZodHelpers {
  export function nonemptyObjects<T extends ZodType>(zodBaseObject: T): ZodEffects<T> {
    return zodBaseObject.refine((schema) => Object.keys(schema).length > 0, 'JSON cannot be empty');
  }
}

// some dto schema
export const BulkDisableQuerySchema = ZodHelpers.nonemptyObjects(
    z
      .object({
        disabled_group: z.array(z.number()).nonempty().optional(),
      })
      .strict(),
);

// ❌ NOT WORK -> keep expecting for disable_group
// transformed schema with new property name, but shape and everything same
export const BulkDeleteQuerySchema = BulkDisableQuerySchema.transform((property) => ({
  ids: property.disabled_group
})

// ✅ WORK as expected, but it's the same shape
export const BulkDisableQuerySchema = ZodHelpers.nonemptyObjects(
    z
      .object({
        ids: z.array(z.number()).nonempty().optional(),
      })
      .strict(),
);

Error

{
    "issues": [
        {
            "code": "unrecognized_keys",
            "keys": [
                "ids"
            ],
            "path": [],
            "message": "Unrecognized key(s) in object: 'ids'"
        },
        {
            "code": "custom",
            "message": "JSON cannot be empty",
            "path": []
        }
    ],
    "name": "ZodError"
}

Expected

  • Working: type inside project already able to access using ids.
  • Not working: On parse, it's not identifying the NEW property name.

Version:

  • Nestjs: ^10.0.0
  • Zod: ^3.23.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant