Skip to content

Commit

Permalink
feat: zod transform inference support
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Nov 16, 2023
1 parent e86dc5b commit 40d1404
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/conform-dom/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export type FormOptions<Schema> = {
form: HTMLFormElement;
submitter: HTMLInputElement | HTMLButtonElement | null;
formData: FormData;
}) => Submission<Schema>;
}) => Submission<Schema, any>;
};

export type SubscriptionSubject = {
Expand Down
4 changes: 2 additions & 2 deletions packages/conform-dom/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export type SubmissionContext<Value> = {
state: SubmissionState;
};

export type Submission<Output> =
export type Submission<Schema, Value = Schema> =
| {
type: 'submit';
payload: Record<string, unknown>;
value: Output | null;
value: Value | null;
error: Record<string, string[]>;
reject(options?: RejectOptions): SubmissionResult;
accept(options?: AcceptOptions): SubmissionResult;
Expand Down
11 changes: 7 additions & 4 deletions packages/conform-zod/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {
import {
type IssueData,
type SafeParseReturnType,
type output,
type RefinementCtx,
type ZodTypeAny,
type ZodError,
type ZodErrorMap,
type input,
type output,
ZodIssueCode,
} from 'zod';
import { enableTypeCoercion } from './coercion';
Expand All @@ -37,23 +38,25 @@ export function parse<Schema extends ZodTypeAny>(
async?: false;
errorMap?: ZodErrorMap;
},
): Submission<output<Schema>>;
): Submission<input<Schema>, output<Schema>>;
export function parse<Schema extends ZodTypeAny>(
payload: FormData | URLSearchParams,
options: {
schema: Schema | ((intent: string) => Schema);
async: true;
errorMap?: ZodErrorMap;
},
): Promise<Submission<output<Schema>>>;
): Promise<Submission<input<Schema>, output<Schema>>>;
export function parse<Schema extends ZodTypeAny>(
payload: FormData | URLSearchParams,
options: {
schema: Schema | ((intent: string) => Schema);
async?: boolean;
errorMap?: ZodErrorMap;
},
): Submission<output<Schema>> | Promise<Submission<output<Schema>>> {
):
| Submission<input<Schema>, output<Schema>>
| Promise<Submission<input<Schema>, output<Schema>>> {
return baseParse(payload, {
resolve(payload, intent) {
const errorMap = options.errorMap;
Expand Down

0 comments on commit 40d1404

Please sign in to comment.