You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RFC/Feature Request: Add a type-safe convert/input output mapper method for compile time input validation
Summary
Introduce a new method, ie. convert, for Zod schemas that provides type-safe input validation and transformation. This method would enforce that the provided input matches the schema’s defined input type (T["_input"]) at compile time, ensuring a stricter contract compared to parse.
Motivation
Zod’s parse and safeParse methods currently accept unknown as input. While this is intentional for unstructured or untrusted data validation, there are common use cases where input data is already structured (e.g., from a typed API response, database query, or other controlled sources). For such cases, allowing type-safe input validation without relying on runtime type checking enhances both safety and developer experience.
The proposed method aligns with Zod’s TypeScript-first philosophy by:
Enforcing input types at compile time, reducing reliance on runtime checks.
Providing a clear distinction between structured and unstructured input handling.
Supporting scenarios where transformations (via .transform()) are applied, while maintaining type safety.
Proposed API
Add a method convert to Zod schemas with the following behavior:
schema.convert(input: T["_input"]): T["_output"];
Input Type (T["_input"]): Enforces that the input matches the schema's input type at compile time.
Output Type (T["_output"]): Returns the validated and transformed data, adhering to the schema's transformation logic.
Use case: General-purpose validation of unstructured data.
convert (Proposed):
Enforces that the input matches the schema’s input type at compile time.
Use case:
Validating already-structured data with guaranteed input types, while benefiting from transformations and Zod’s validation.
Using zod as typesafe converter ie. between well defined input and output types.
Potential Concerns
Confusion with Existing Methods:
I am uncertain about how to name the method in order to clearly differentiate itself from parse, safeParse, transform,... to avoid ambiguity. Suggested name: convert, inputToOutput, parseConvert?
oberbeck
changed the title
RFC/Feature Request: Add a type-safe convert/input output mapper method for compile type input validation
RFC/Feature Request: Add a type-safe convert/input output mapper method for compile time input validation
Nov 19, 2024
Another option instead of adding a new function would be to add a param to ParseParams that would enforce input type-checking. Something like:
exportdeclaretypeParseParams={path: (string|number)[];errorMap: ZodErrorMap;async: boolean;typedInput: boolean;// this would be new}classZodType{// ...// add definition overridesparse(data: unknown,params?: Partial<ParseParams>): Outputparse(data: Input,params: Partial<Omit<ParseParams,"typedInput">>&{typedInput: true}): Output// current implementation wouldn't need to changeparse(data: unknown,params?: Partial<ParseParams>): Output{constresult=this.safeParse(data,params);if(result.success)returnresult.data;throwresult.error;}}
Then you could call it with:
z.object({name: z.string()}).parse({});// runtime errorz.object({name: z.string()}).parse({},{typedInput: true});// compile-time and runtime error
Not exactly sold on the name typedInput, however... maybe strict: true?
I do kinda think I like the idea of a separate function better though. What about the name strictParse, or would that get confused with .strict()? Maybe parseKnown?
RFC/Feature Request: Add a type-safe
convert
/input output mapper
method for compile time input validationSummary
Introduce a new method, ie.
convert
, for Zod schemas that provides type-safe input validation and transformation. This method would enforce that the provided input matches the schema’s defined input type (T["_input"]
) at compile time, ensuring a stricter contract compared toparse
.Motivation
Zod’s
parse
andsafeParse
methods currently acceptunknown
as input. While this is intentional for unstructured or untrusted data validation, there are common use cases where input data is already structured (e.g., from a typed API response, database query, or other controlled sources). For such cases, allowing type-safe input validation without relying on runtime type checking enhances both safety and developer experience.The proposed method aligns with Zod’s TypeScript-first philosophy by:
.transform()
) are applied, while maintaining type safety.Proposed API
Add a method
convert
to Zod schemas with the following behavior:Input Type (
T["_input"]
): Enforces that the input matches the schema's input type at compile time.Output Type (
T["_output"]
): Returns the validated and transformed data, adhering to the schema's transformation logic.Example Usage
zod/src/__tests__/convert.test-d.ts
Lines 21 to 59 in dad8820
Comparison with Existing Methods
parse
:unknown
input and validates it.convert
(Proposed):Potential Concerns
parse
,safeParse
,transform
,... to avoid ambiguity. Suggested name:convert
,inputToOutput
,parseConvert
?Example implementation
#3861
zod/src/types.ts
Lines 521 to 541 in dad8820
The text was updated successfully, but these errors were encountered: