zod icon indicating copy to clipboard operation
zod copied to clipboard

deepPartial doesn't work with superRefine

Open dstoyanoff opened this issue 1 year ago • 8 comments

Given a schema with an object child which has superRefine, the deepPartial on the parent schema does not make the child's entries optional

dstoyanoff avatar Jan 02 '24 07:01 dstoyanoff

Hi @dstoyanoff, You mean:

import { z } from 'zod';

const user = z.object({
  username: z.string().superRefine((value) => value.length >= 1),
  location: z.object({
    latitude: z.number(),
    longitude: z.number(),
  }),
  strings: z.array(z.object({ value: z.string() })),
});

const deepPartialUser = user.deepPartial();

type AAA = z.infer<typeof deepPartialUser>;

chanthinh avatar Jan 02 '24 08:01 chanthinh

Hello @chanthinh, its more like this:

import { z } from 'zod';

const user = z.object({
  username: z.string(),
  location: z.object({
    latitude: z.number(),
    longitude: z.number(),
  }).superRefine(() => false),
  strings: z.array(z.object({ value: z.string() })),
});

const deepPartialUser = user.deepPartial();

type AAA = z.infer<typeof deepPartialUser>;

here is a codesandbox - https://codesandbox.io/p/devbox/polished-rain-738nnl?file=%2Fsrc%2Findex.ts%3A23%2C1

dstoyanoff avatar Jan 02 '24 08:01 dstoyanoff

@dstoyanoff, You can check this one: https://stackblitz.com/edit/typescript-zgzrzk?file=schemas%2FschemaWithDefaultType.ts

and change strict to false

Hope this help you

{
  "include": ["./src/*"],
  "compilerOptions": {
    "esModuleInterop": true,
    "strict": false
  }
}

chanthinh avatar Jan 02 '24 08:01 chanthinh

Thanks @chanthinh, but Zod itself recommends that strict is enabled, so this change is rather controversial.

dstoyanoff avatar Jan 02 '24 11:01 dstoyanoff

@dstoyanoff, yah I know, glad to help you

chanthinh avatar Jan 02 '24 13:01 chanthinh

and change strict to false

I highly recommend that you don't do this. This will cause other problems and so strict should always be used for Zod.


This does seem to be a bug, however, I think that deepPartial is in process of being deprecated. So I'm not sure this will be fixed, but I could be wrong.

JacobWeisenburger avatar Jan 02 '24 16:01 JacobWeisenburger

and change strict to false

I highly recommend that you don't do this. This will cause other problems and so strict should always be used for Zod.


This does seem to be a bug, however, I think that deepPartial is in process of being deprecated. So I'm not sure this will be fixed, but I could be wrong.

I see that it's getting deprecated, but at the moment there is no suggested alternative in the docs. Happy for an alternative solution to deepPartial.

dstoyanoff avatar Jan 02 '24 17:01 dstoyanoff

Hi @dstoyanoff, You mean:

import { z } from 'zod';

const user = z.object({
  username: z.string().superRefine((value) => value.length >= 1),
  location: z.object({
    latitude: z.number(),
    longitude: z.number(),
  }),
  strings: z.array(z.object({ value: z.string() })),
});

const deepPartialUser = user.deepPartial();

type AAA = z.infer<typeof deepPartialUser>;

@chanthinh "refine" method suffices for custom validation, just want to know why superRefine is used in this case, which is better for customizing error codes.

tsdineshjai avatar May 03 '24 09:05 tsdineshjai