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

.passthrough() pass through excess nested properties as well #2655

Open
MrDrewShep opened this issue Aug 14, 2023 · 4 comments
Open

.passthrough() pass through excess nested properties as well #2655

MrDrewShep opened this issue Aug 14, 2023 · 4 comments

Comments

@MrDrewShep
Copy link

Appears .passthrough() only operates on root properties.

import { z } from 'zod';

const schema = z.object({
  first: z.string(),
  details: z.object({
    hair: z.string(),
  }),
});

const myObj = {
  first: 'me',
  last: 'mine',
  details: {
    hair: 'red',
    eyes: 'blue',
  },
};

const res = schema.passthrough().safeParse(myObj);
console.log(res);

Actual results

{
  success: true,
  data: { first: 'me', details: { hair: 'red' }, last: 'mine' }
}

Expected results

{
  success: true,
  data: { first: 'me', details: { hair: 'red', eyes: 'blue' }, last: 'mine' }
}

Additional functionality which would be very handy, as it relates to nested properties:

  1. .pick() allow picking and choosing for nested props
@carmelneta
Copy link

carmelneta commented Aug 17, 2023

+1

@colinhacks
Copy link
Owner

.passthrough() is intended to only work shallowly.

For Zod 4 I'm exploring a way to set the object strictness setting globally.

@robogeek
Copy link

robogeek commented Dec 6, 2023

I have the same issue.

I'm working on data types generated from an OpenAPI-based API that has many nested objects. The API specification explicitly allows implementors to add additional properties on mutual agreement. That means data validation must do what passthrough does, which is to check the things defined in the API spec but pass everything else through.

But, as noted above, for nested objects passthrough does not apply. Additional fields are not passed through.

With Joi, I get the desired effect this way:

joiIsEvent.validate(data, {
        allowUnknown: true
    })

Where joiIsEvent was defined using Joi declarations.

Hence, if zodSchema.parse() took an optional options argument with this value - as you say, a strictness setting that's handled "globally" ...

@paul-uz
Copy link

paul-uz commented Apr 4, 2024

How can I get around this current limitation? I need to have a nested object with one specific rule, and then allow other fields, without knowing what they are beforehand.

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

5 participants