Skip to content

Commit 67d0e7d

Browse files
committed
Add fallbacks
1 parent 0e90257 commit 67d0e7d

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

zod/src/__tests__/__fixtures__/data-v3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Field, InternalFieldName } from 'react-hook-form';
2-
import { z } from 'zod/v3';
2+
import { z } from 'zod';
33

44
export const schema = z
55
.object({

zod/src/__tests__/zod-v3.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ describe('zodResolver', () => {
9292
await expect(promise).rejects.toThrow('custom error');
9393
});
9494

95+
it('should enforce parse params type signature', async () => {
96+
const resolver = zodResolver(schema, {
97+
async: true,
98+
path: ['asdf', 1234],
99+
errorMap(iss, ctx) {
100+
iss.path;
101+
iss.code;
102+
iss.path;
103+
ctx.data;
104+
ctx.defaultError;
105+
return { message: 'asdf' };
106+
},
107+
});
108+
109+
resolver;
110+
});
111+
95112
/**
96113
* Type inference tests
97114
*/

zod/src/__tests__/zod-v4.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ describe('zodResolver', () => {
9292
await expect(promise).rejects.toThrow('custom error');
9393
});
9494

95+
it('should enforce parse params type signature', async () => {
96+
const resolver = zodResolver(schema, {
97+
jitless: true,
98+
reportInput: true,
99+
error(iss) {
100+
iss.path;
101+
iss.code;
102+
iss.path;
103+
return { message: 'asdf' };
104+
},
105+
});
106+
107+
resolver;
108+
});
109+
95110
/**
96111
* Type inference tests
97112
*/

zod/src/zod.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,32 +143,73 @@ type NonRawResolverOptions = {
143143
interface Zod3Type<O = unknown, I = unknown> {
144144
_output: O;
145145
_input: I;
146+
_def: {
147+
typeName: string;
148+
};
146149
}
150+
147151
interface Zod4Type<O = unknown, I = unknown> {
148152
_zod: {
149153
output: O;
150154
input: I;
151155
};
152156
}
153157

158+
// some type magic to make versions pre-3.25.0 still work
159+
type IsUnresolved<T> = PropertyKey extends keyof T ? true : false;
160+
type UnresolvedFallback<T, Fallback> = IsUnresolved<typeof z3> extends true
161+
? Fallback
162+
: T;
163+
type FallbackIssue = {
164+
code: string;
165+
message: string;
166+
path: (string | number)[];
167+
};
168+
type Zod3ParseParams = UnresolvedFallback<
169+
z3.ParseParams,
170+
// fallback if user is on <3.25.0
171+
{
172+
path?: (string | number)[];
173+
errorMap?: (
174+
iss: FallbackIssue,
175+
ctx: {
176+
defaultError: string;
177+
data: any;
178+
},
179+
) => { message: string };
180+
async?: boolean;
181+
}
182+
>;
183+
type Zod4ParseParams = UnresolvedFallback<
184+
z4.ParseContext<z4.$ZodIssue>,
185+
// fallback if user is on <3.25.0
186+
{
187+
readonly error?: (
188+
iss: FallbackIssue,
189+
) => null | undefined | string | { message: string };
190+
readonly reportInput?: boolean;
191+
readonly jitless?: boolean;
192+
}
193+
>;
194+
154195
export function zodResolver<Input extends FieldValues, Context, Output>(
155196
schema: Zod3Type<Output, Input>,
156-
schemaOptions?: Partial<z3.ParseParams>,
197+
schemaOptions?: Zod3ParseParams,
157198
resolverOptions?: NonRawResolverOptions,
158199
): Resolver<Input, Context, Output>;
159200
export function zodResolver<Input extends FieldValues, Context, Output>(
160201
schema: Zod3Type<Output, Input>,
161-
schemaOptions: Partial<z3.ParseParams> | undefined,
202+
schemaOptions: Zod3ParseParams | undefined,
162203
resolverOptions: RawResolverOptions,
163204
): Resolver<Input, Context, Input>;
164205
export function zodResolver<Input extends FieldValues, Context, Output>(
165206
schema: Zod4Type<Output, Input>,
166-
schemaOptions?: z4.ParseContext<z4.$ZodIssue>, // already partial
207+
schemaOptions?: Zod4ParseParams, // already partial
167208
resolverOptions?: NonRawResolverOptions,
168209
): Resolver<Input, Context, Output>;
169210
export function zodResolver<Input extends FieldValues, Context, Output>(
170211
schema: Zod4Type<Output, Input>,
171-
schemaOptions?: z4.ParseContext<z4.$ZodIssue>, // already partial
212+
schemaOptions?: Zod4ParseParams, // already partial
172213
resolverOptions?: RawResolverOptions,
173214
): Resolver<Input, Context, Input>;
174215
/**

0 commit comments

Comments
 (0)