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

.pick doesn't typecheck keys and throws Cannot read property '_parse' of undefined #1128

Closed
krivachy opened this issue May 11, 2022 · 3 comments

Comments

@krivachy
Copy link
Contributor

I've run into this issue when using .pick that the keys are not typechecked that they're actual keys on the source object.

Here's a small example to reproduce this using 3.15.1:

import { z } from 'zod';

const schema = z.object({
  a: z.string(),
  b: z.number(),
});

const pickedSchema = schema.pick({
  a: true,
  doesntExist: true,
});

pickedSchema.parse({
  a: 'value',
});

Results in:

/node_modules/.pnpm/[email protected]/node_modules/zod/lib/types.js:1060
                value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
                                    ^
TypeError: Cannot read property '_parse' of undefined
    at ZodObject._parse (/node_modules/.pnpm/[email protected]/node_modules/zod/lib/types.js:1060:37)
    at ZodObject._parseSync (/node_modules/.pnpm/[email protected]/node_modules/zod/lib/types.js:109:29)
    at ZodObject.safeParse (/node_modules/.pnpm/[email protected]/node_modules/zod/lib/types.js:139:29)
    at ZodObject.parse (/node_modules/.pnpm/[email protected]/node_modules/zod/lib/types.js:120:29)
    at Object.<anonymous> (/zodTest.ts:13:14)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module.m._compile (/node_modules/.pnpm/[email protected]_3yjwidxeabsavpmncg7zl6hyk4/node_modules/ts-node/src/index.ts:1455:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Object.require.extensions.<computed> [as .ts] (/node_modules/.pnpm/[email protected]_3yjwidxeabsavpmncg7zl6hyk4/node_modules/ts-node/src/index.ts:1458:12)
    at Module.load (internal/modules/cjs/loader.js:950:32)
@krivachy
Copy link
Contributor Author

I see it's a duplicate of #895. Is this an issue worth fixing?

@sizzlorox
Copy link

I see it's a duplicate of #895. Is this an issue worth fixing?

I've just encountered this exact issue, I believe this is just broken and pick is unusable, Is there any news for a fix? It cant be possible that nobody uses this feature or am I just misunderstand the use of .pick?

@colinhacks
Copy link
Owner

Good catch, fixed in 3.17.2

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

3 participants