Skip to content

Commit 783aefa

Browse files
committed
preprocessed option now has typesafe keys.
1 parent 3fe8b2f commit 783aefa

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

src/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,18 +803,18 @@ test('Passthrough validation', async () => {
803803

804804
test.only('Preprocessed fields', async () => {
805805
const schema = z.object({
806-
accept: z.preprocess(
807-
(val) => (val === undefined ? undefined : Boolean(val)),
806+
tristate: z.preprocess(
807+
(value) => (value === undefined ? undefined : Boolean(value)),
808808
z.boolean().optional()
809809
)
810810
});
811811

812812
const formData = new FormData();
813813

814814
const form = await superValidate(formData, schema, {
815-
preprocessed: ['accept']
815+
preprocessed: ['tristate']
816816
});
817817

818818
assert(form.valid);
819-
expect(form.data.accept).toBeUndefined();
819+
expect(form.data.tristate).toBeUndefined();
820820
});

src/lib/schemaEntity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function _entityHash<T extends ZodTypeAny>(type: T) {
174174

175175
export function entityData<T extends AnyZodObject>(
176176
schema: T,
177-
warnings?: SuperValidateOptions['warnings']
177+
warnings?: SuperValidateOptions<T>['warnings']
178178
) {
179179
const cached = getCached(schema);
180180
if (cached) return cached;
@@ -288,7 +288,7 @@ export function defaultValues<T extends ZodValidation<AnyZodObject>>(
288288

289289
function constraints<T extends AnyZodObject>(
290290
schema: T,
291-
warnings: SuperValidateOptions['warnings']
291+
warnings: SuperValidateOptions<T>['warnings']
292292
): InputConstraints<T> {
293293
function constraint(
294294
key: string,

src/lib/superValidate.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export function setError<T extends ZodValidation<AnyZodObject>>(
154154
function formDataToValidation<T extends AnyZodObject>(
155155
data: FormData,
156156
schemaData: SchemaData<T>,
157-
preprocessed?: string[]
157+
preprocessed?: (keyof z.infer<T>)[]
158158
) {
159159
const output: Record<string, unknown> = {};
160160
const { schemaKeys, entityInfo } = schemaData;
@@ -283,7 +283,7 @@ type SchemaData<T extends AnyZodObject> = {
283283
hasEffects: boolean;
284284
entityInfo: Entity<T>;
285285
schemaKeys: string[];
286-
opts: SuperValidateOptions;
286+
opts: SuperValidateOptions<T>;
287287
};
288288

289289
type ParsedData = {
@@ -313,7 +313,7 @@ function dataToValidate<T extends AnyZodObject>(
313313
function parseFormData<T extends AnyZodObject>(
314314
formData: FormData,
315315
schemaData: SchemaData<T>,
316-
options?: SuperValidateOptions
316+
options?: SuperValidateOptions<T>
317317
): ParsedData {
318318
function tryParseSuperJson() {
319319
if (formData.has('__superform_json')) {
@@ -350,7 +350,7 @@ function parseFormData<T extends AnyZodObject>(
350350
function parseSearchParams<T extends AnyZodObject>(
351351
data: URL | URLSearchParams,
352352
schemaData: SchemaData<T>,
353-
options?: SuperValidateOptions
353+
options?: SuperValidateOptions<T>
354354
): ParsedData {
355355
if (data instanceof URL) data = data.searchParams;
356356

@@ -464,7 +464,7 @@ function validateResult<T extends AnyZodObject, M>(
464464

465465
function getSchemaData<T extends AnyZodObject>(
466466
schema: ZodValidation<T>,
467-
options: SuperValidateOptions | undefined
467+
options: SuperValidateOptions<T> | undefined
468468
): SchemaData<T> {
469469
const originalSchema = schema as T;
470470

@@ -500,23 +500,24 @@ function getSchemaData<T extends AnyZodObject>(
500500

501501
/////////////////////////////////////////////////////////////////////
502502

503-
export type SuperValidateOptions = Partial<{
504-
errors: boolean;
505-
id: string;
506-
warnings: {
507-
multipleRegexps?: boolean;
508-
multipleSteps?: boolean;
509-
};
510-
preprocessed: string[];
511-
}>;
503+
export type SuperValidateOptions<T extends AnyZodObject = AnyZodObject> =
504+
Partial<{
505+
errors: boolean;
506+
id: string;
507+
warnings: {
508+
multipleRegexps?: boolean;
509+
multipleSteps?: boolean;
510+
};
511+
preprocessed: (keyof z.infer<T>)[];
512+
}>;
512513

513514
export async function superValidate<
514515
T extends ZodValidation<AnyZodObject>,
515516
// eslint-disable-next-line @typescript-eslint/no-explicit-any
516517
M = any
517518
>(
518519
schema: T,
519-
options?: SuperValidateOptions
520+
options?: SuperValidateOptions<UnwrapEffects<T>>
520521
): Promise<SuperValidated<UnwrapEffects<T>, M>>;
521522

522523
export async function superValidate<
@@ -534,7 +535,7 @@ export async function superValidate<
534535
| null
535536
| undefined,
536537
schema: T,
537-
options?: SuperValidateOptions
538+
options?: SuperValidateOptions<UnwrapEffects<T>>
538539
): Promise<SuperValidated<UnwrapEffects<T>, M>>;
539540

540541
/**
@@ -548,11 +549,11 @@ export async function superValidate<
548549
M = any
549550
>(
550551
data: unknown,
551-
schema?: T | SuperValidateOptions,
552-
options?: SuperValidateOptions
552+
schema?: T | SuperValidateOptions<UnwrapEffects<T>>,
553+
options?: SuperValidateOptions<UnwrapEffects<T>>
553554
): Promise<SuperValidated<UnwrapEffects<T>, M>> {
554555
if (data && typeof data === 'object' && 'safeParseAsync' in data) {
555-
options = schema as SuperValidateOptions | undefined;
556+
options = schema as SuperValidateOptions<UnwrapEffects<T>> | undefined;
556557
schema = data as T;
557558
data = null;
558559
}
@@ -625,7 +626,7 @@ export function superValidateSync<
625626
M = any
626627
>(
627628
schema: T,
628-
options?: SuperValidateOptions
629+
options?: SuperValidateOptions<UnwrapEffects<T>>
629630
): SuperValidated<UnwrapEffects<T>, M>;
630631

631632
export function superValidateSync<
@@ -641,7 +642,7 @@ export function superValidateSync<
641642
| null
642643
| undefined,
643644
schema: T,
644-
options?: SuperValidateOptions
645+
options?: SuperValidateOptions<UnwrapEffects<T>>
645646
): SuperValidated<UnwrapEffects<T>, M>;
646647

647648
/**
@@ -655,11 +656,11 @@ export function superValidateSync<
655656
M = any
656657
>(
657658
data: unknown,
658-
schema?: T | SuperValidateOptions,
659-
options?: SuperValidateOptions
659+
schema?: T | SuperValidateOptions<UnwrapEffects<T>>,
660+
options?: SuperValidateOptions<UnwrapEffects<T>>
660661
): SuperValidated<UnwrapEffects<T>, M> {
661662
if (data && typeof data === 'object' && 'safeParse' in data) {
662-
options = schema as SuperValidateOptions | undefined;
663+
options = schema as SuperValidateOptions<UnwrapEffects<T>> | undefined;
663664
schema = data as T;
664665
data = null;
665666
}

0 commit comments

Comments
 (0)