Skip to content

Commit 7f4c35a

Browse files
committed
Support Zod 3.24. Improve type performance.
1 parent c614205 commit 7f4c35a

File tree

5 files changed

+54
-71
lines changed

5 files changed

+54
-71
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
"superstruct": "^2.0.2",
175175
"valibot": "^1.0.0-beta.3",
176176
"yup": "^1.4.0",
177-
"zod": "^3.23.8",
177+
"zod": "^3.24.0",
178178
"zod-to-json-schema": "^3.23.3"
179179
},
180180
"dependencies": {

pnpm-lock.yaml

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/adapters/adapters.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import type {
99
Schema,
1010
ValidationResult,
1111
Infer as InferSchema,
12-
InferIn as InferInSchema
12+
InferIn as InferInSchema,
13+
Registry
1314
} from './typeSchema.js';
1415
import { simpleSchema } from './simple-schema/index.js';
1516

1617
export type { Schema, ValidationIssue, ValidationResult } from './typeSchema.js';
1718

18-
export type Infer<T extends Schema> = NonNullable<InferSchema<T>>;
19-
export type InferIn<T extends Schema> = NonNullable<InferInSchema<T>>;
19+
export type Infer<T extends Schema, K extends keyof Registry = keyof Registry> = NonNullable<InferSchema<T, K>>;
20+
export type InferIn<T extends Schema, K extends keyof Registry = keyof Registry> = NonNullable<InferInSchema<T, K>>;
2021

2122
export type ValidationLibrary =
2223
| 'arktype'

src/lib/adapters/typeSchema.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
InferOutput as Output
1515
} from 'valibot';
1616
import type { Schema as Schema$2, InferType } from 'yup';
17-
import type { ZodSchema, input, output } from 'zod';
17+
import type { ZodType, ZodTypeAny } from 'zod';
1818
import type { SchemaTypes, Infer as VineInfer } from '@vinejs/vine/types';
1919
import type { FromSchema, JSONSchema } from 'json-schema-to-ts';
2020
import type { Struct, Infer as Infer$2 } from 'superstruct';
@@ -128,9 +128,9 @@ interface YupResolver extends Resolver {
128128
}
129129

130130
interface ZodResolver extends Resolver {
131-
base: ZodSchema;
132-
input: this['schema'] extends ZodSchema ? input<this['schema']> : never;
133-
output: this['schema'] extends ZodSchema ? output<this['schema']> : never;
131+
base: ZodTypeAny;
132+
input: this['schema'] extends ZodType<any, any, infer I> ? I : never;
133+
output: this['schema'] extends ZodType<infer O, any,any> ? O : never;
134134
}
135135

136136
interface VineResolver extends Resolver {
@@ -194,8 +194,7 @@ interface RuntypesResolver extends Resolver {
194194
}
195195
196196
*/
197-
198-
type Registry = {
197+
export type Registry = {
199198
arktype: ArkTypeResolver;
200199
classvalidator: ClassValidatorResolver;
201200
custom: CustomResolver;
@@ -217,15 +216,15 @@ type Registry = {
217216
*/
218217
};
219218

220-
type Infer<TSchema extends Schema> = UnknownIfNever<
219+
type Infer<TSchema extends Schema, Keys extends keyof Registry = keyof Registry> = UnknownIfNever<
221220
{
222-
[K in keyof Registry]: IfDefined<InferOutput<Registry[K], TSchema>>;
223-
}[keyof Registry]
221+
[K in Keys]: IfDefined<InferOutput<Registry[K], TSchema>>;
222+
}[Keys]
224223
>;
225-
type InferIn<TSchema extends Schema> = UnknownIfNever<
224+
type InferIn<TSchema extends Schema, Keys extends keyof Registry = keyof Registry> = UnknownIfNever<
226225
{
227-
[K in keyof Registry]: IfDefined<InferInput<Registry[K], TSchema>>;
228-
}[keyof Registry]
226+
[K in Keys]: IfDefined<InferInput<Registry[K], TSchema>>;
227+
}[Keys]
229228
>;
230229

231230
/*

src/lib/adapters/zod.ts

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import type {
2-
AnyZodObject,
3-
ZodDefault,
4-
ZodEffects,
5-
ZodErrorMap,
6-
ZodType,
7-
ZodTypeDef,
8-
ZodUnion
1+
import {
2+
type ZodErrorMap,
3+
type ZodType,
4+
type ZodTypeDef,
95
} from 'zod';
106
import type { JSONSchema7 } from 'json-schema';
117
import {
@@ -32,68 +28,55 @@ export const zodToJSONSchema = (...params: Parameters<typeof zodToJson>) => {
3228
return zodToJson(...params) as JSONSchema7;
3329
};
3430

35-
type ZodObjectUnion<T extends AnyZodObject> = ZodUnion<
36-
[ZodValidation<T>, ZodValidation<T>, ...ZodValidation<T>[]]
37-
>;
38-
39-
export type ZodObjectType = ZodType<Record<string, unknown>, ZodTypeDef, Record<string, unknown>>;
4031

41-
export type ZodObjectTypes = AnyZodObject | ZodObjectUnion<AnyZodObject> | ZodObjectType;
32+
// allows for any object schema
33+
// allows `undefined` in the input type to account for ZodDefault
34+
export type ZodObjectType = ZodType<Record<string, unknown>, ZodTypeDef, Record<string, unknown> | undefined>;
35+
export type ZodObjectTypes = ZodObjectType;
4236

43-
export type ZodValidation<T extends ZodObjectTypes> =
44-
| T
45-
| ZodEffects<T>
46-
| ZodEffects<ZodEffects<T>>
47-
| ZodEffects<ZodEffects<ZodEffects<T>>>
48-
| ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>
49-
| ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>>
50-
| ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>>>
51-
| ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>>>>
52-
| ZodDefault<T>
53-
| ZodDefault<ZodEffects<T>>
54-
| ZodDefault<ZodEffects<ZodEffects<T>>>
55-
| ZodDefault<ZodEffects<ZodEffects<ZodEffects<T>>>>
56-
| ZodDefault<ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>>
57-
| ZodDefault<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>>>
58-
| ZodDefault<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>>>>
59-
| ZodDefault<
60-
ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<T>>>>>>>
61-
>;
37+
// left in for compatibility reasons
38+
export type ZodValidation<T extends ZodObjectTypes = ZodObjectTypes> = T;
39+
// type asdf = ZodValidation['_output']
40+
// type asdf2 = ZodValidation['_input']
6241

63-
async function validate<T extends ZodValidation<ZodObjectTypes>>(
42+
async function validate<T extends ZodValidation>(
6443
schema: T,
6544
data: unknown,
6645
errorMap: ZodErrorMap | undefined
67-
): Promise<ValidationResult<Infer<T>>> {
46+
): Promise<ValidationResult<Infer<T, "zod">>> {
6847
const result = await schema.safeParseAsync(data, { errorMap });
6948
if (result.success) {
7049
return {
71-
data: result.data as Infer<T>,
50+
data: result.data as Infer<T, "zod">,
7251
success: true
7352
};
7453
}
54+
7555
return {
7656
issues: result.error.issues.map(({ message, path }) => ({ message, path })),
7757
success: false
7858
};
7959
}
8060

81-
function _zod<T extends ZodValidation<ZodObjectTypes>>(
61+
function _zod<T extends ZodValidation>(
8262
schema: T,
83-
options?: AdapterOptions<Infer<T>> & { errorMap?: ZodErrorMap; config?: Partial<Options> }
84-
): ValidationAdapter<Infer<T>, InferIn<T>> {
63+
options?: AdapterOptions<Infer<T, "zod">> & { errorMap?: ZodErrorMap; config?: Partial<Options> }
64+
): ValidationAdapter<Infer<T, "zod">, InferIn<T, "zod">> {
8565
return createAdapter({
86-
superFormValidationLibrary: 'zod',
87-
validate: async (data) => validate(schema, data, options?.errorMap),
66+
superFormValidationLibrary: "zod",
67+
async validate (data){
68+
// options?.defaults
69+
return validate(schema, data, options?.errorMap)
70+
},
8871
jsonSchema: options?.jsonSchema ?? zodToJSONSchema(schema, options?.config),
89-
defaults: options?.defaults
72+
defaults: options?.defaults,
9073
});
9174
}
9275

93-
function _zodClient<T extends ZodValidation<ZodObjectTypes>>(
76+
function _zodClient<T extends ZodValidation>(
9477
schema: T,
9578
options?: { errorMap?: ZodErrorMap }
96-
): ClientValidationAdapter<Infer<T>, InferIn<T>> {
79+
): ClientValidationAdapter<Infer<T, "zod">, InferIn<T, "zod">> {
9780
return {
9881
superFormValidationLibrary: 'zod',
9982
validate: async (data) => validate(schema, data, options?.errorMap)

0 commit comments

Comments
 (0)