Skip to content

Commit 537eee0

Browse files
committed
Merge commit 'refs/pull/528/merge' of github.com:ciscoheat/sveltekit-superforms
2 parents 9b1b006 + 092e7b7 commit 537eee0

File tree

4 files changed

+42
-59
lines changed

4 files changed

+42
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
"joi": "^17.13.3",
168168
"json-schema-to-ts": "^3.1.1",
169169
"superstruct": "^2.0.2",
170-
"valibot": "1.0.0-beta.9",
170+
"valibot": "^1.0.0-beta.9",
171171
"yup": "^1.5.0",
172172
"zod": "^3.24.1",
173173
"zod-to-json-schema": "^3.24.1"

src/lib/adapters/adapters.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ 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<
20+
InferSchema<T, K>
21+
>;
22+
export type InferIn<T extends Schema, K extends keyof Registry = keyof Registry> = NonNullable<
23+
InferInSchema<T, K>
24+
>;
2025

2126
export type ValidationLibrary =
2227
| '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 { ZodTypeAny, input, output } 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 ZodTypeAny ? input<this['schema']> : never;
133+
output: this['schema'] extends ZodTypeAny ? output<this['schema']> : never;
134134
}
135135

136136
interface VineResolver extends Resolver {
@@ -198,8 +198,7 @@ interface RuntypesResolver extends Resolver {
198198
}
199199
200200
*/
201-
202-
type Registry = {
201+
export type Registry = {
203202
arktype: ArkTypeResolver;
204203
classvalidator: ClassValidatorResolver;
205204
custom: CustomResolver;
@@ -221,15 +220,15 @@ type Registry = {
221220
*/
222221
};
223222

224-
type Infer<TSchema extends Schema> = UnknownIfNever<
223+
type Infer<TSchema extends Schema, Keys extends keyof Registry = keyof Registry> = UnknownIfNever<
225224
{
226-
[K in keyof Registry]: IfDefined<InferOutput<Registry[K], TSchema>>;
227-
}[keyof Registry]
225+
[K in Keys]: IfDefined<InferOutput<Registry[K], TSchema>>;
226+
}[Keys]
228227
>;
229-
type InferIn<TSchema extends Schema> = UnknownIfNever<
228+
type InferIn<TSchema extends Schema, Keys extends keyof Registry = keyof Registry> = UnknownIfNever<
230229
{
231-
[K in keyof Registry]: IfDefined<InferInput<Registry[K], TSchema>>;
232-
}[keyof Registry]
230+
[K in Keys]: IfDefined<InferInput<Registry[K], TSchema>>;
231+
}[Keys]
233232
>;
234233

235234
/*

src/lib/adapters/zod.ts

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import type {
2-
AnyZodObject,
3-
ZodDefault,
4-
ZodEffects,
5-
ZodErrorMap,
6-
ZodType,
7-
ZodTypeDef,
8-
ZodUnion
9-
} from 'zod';
1+
import { type ZodErrorMap, type ZodType, type ZodTypeDef } from 'zod';
102
import type { JSONSchema7 } from 'json-schema';
113
import {
124
type AdapterOptions,
@@ -32,68 +24,55 @@ export const zodToJSONSchema = (...params: Parameters<typeof zodToJson>) => {
3224
return zodToJson(...params) as JSONSchema7;
3325
};
3426

35-
type ZodObjectUnion<T extends AnyZodObject> = ZodUnion<
36-
[ZodValidation<T>, ZodValidation<T>, ...ZodValidation<T>[]]
27+
// allows for any object schema
28+
// allows `undefined` in the input type to account for ZodDefault
29+
export type ZodObjectType = ZodType<
30+
Record<string, unknown>,
31+
ZodTypeDef,
32+
Record<string, unknown> | undefined
3733
>;
34+
export type ZodObjectTypes = ZodObjectType;
3835

39-
export type ZodObjectType = ZodType<Record<string, unknown>, ZodTypeDef, Record<string, unknown>>;
40-
41-
export type ZodObjectTypes = AnyZodObject | ZodObjectUnion<AnyZodObject> | ZodObjectType;
36+
// left in for compatibility reasons
37+
export type ZodValidation<T extends ZodObjectTypes = ZodObjectTypes> = T;
4238

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-
>;
62-
63-
async function validate<T extends ZodValidation<ZodObjectTypes>>(
39+
async function validate<T extends ZodValidation>(
6440
schema: T,
6541
data: unknown,
6642
errorMap: ZodErrorMap | undefined
67-
): Promise<ValidationResult<Infer<T>>> {
43+
): Promise<ValidationResult<Infer<T, 'zod'>>> {
6844
const result = await schema.safeParseAsync(data, { errorMap });
6945
if (result.success) {
7046
return {
71-
data: result.data as Infer<T>,
47+
data: result.data as Infer<T, 'zod'>,
7248
success: true
7349
};
7450
}
51+
7552
return {
7653
issues: result.error.issues.map(({ message, path }) => ({ message, path })),
7754
success: false
7855
};
7956
}
8057

81-
function _zod<T extends ZodValidation<ZodObjectTypes>>(
58+
function _zod<T extends ZodValidation>(
8259
schema: T,
83-
options?: AdapterOptions<Infer<T>> & { errorMap?: ZodErrorMap; config?: Partial<Options> }
84-
): ValidationAdapter<Infer<T>, InferIn<T>> {
60+
options?: AdapterOptions<Infer<T, 'zod'>> & { errorMap?: ZodErrorMap; config?: Partial<Options> }
61+
): ValidationAdapter<Infer<T, 'zod'>, InferIn<T, 'zod'>> {
8562
return createAdapter({
8663
superFormValidationLibrary: 'zod',
87-
validate: async (data) => validate(schema, data, options?.errorMap),
64+
validate: async (data) => {
65+
return validate(schema, data, options?.errorMap);
66+
},
8867
jsonSchema: options?.jsonSchema ?? zodToJSONSchema(schema, options?.config),
8968
defaults: options?.defaults
9069
});
9170
}
9271

93-
function _zodClient<T extends ZodValidation<ZodObjectTypes>>(
72+
function _zodClient<T extends ZodValidation>(
9473
schema: T,
9574
options?: { errorMap?: ZodErrorMap }
96-
): ClientValidationAdapter<Infer<T>, InferIn<T>> {
75+
): ClientValidationAdapter<Infer<T, 'zod'>, InferIn<T, 'zod'>> {
9776
return {
9877
superFormValidationLibrary: 'zod',
9978
validate: async (data) => validate(schema, data, options?.errorMap)

0 commit comments

Comments
 (0)