-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a passthrough zod validator for API types (#29475)
GitOrigin-RevId: cae8c38695cefb030d99713e310633ffb33c5daf
- Loading branch information
Showing
9 changed files
with
104 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { z } from "zod"; | ||
|
||
/** | ||
* Convenience wrapper for z.object(...).passthrough(). | ||
* | ||
* This object validator allows extra properties and passes them through. | ||
* This is useful for forwards compatibility if the server adds extra unknown | ||
* fields. | ||
*/ | ||
export function looseObject<T extends z.ZodRawShape>( | ||
shape: T, | ||
params?: z.RawCreateParams, | ||
): z.ZodObject< | ||
T, | ||
"passthrough", | ||
z.ZodTypeAny, | ||
{ | ||
[k_1 in keyof z.objectUtil.addQuestionMarks< | ||
z.baseObjectOutputType<T>, | ||
{ | ||
[k in keyof z.baseObjectOutputType<T>]: undefined extends z.baseObjectOutputType<T>[k] | ||
? never | ||
: k; | ||
}[keyof T] | ||
>]: z.objectUtil.addQuestionMarks< | ||
z.baseObjectOutputType<T>, | ||
{ | ||
[k in keyof z.baseObjectOutputType<T>]: undefined extends z.baseObjectOutputType<T>[k] | ||
? never | ||
: k; | ||
}[keyof T] | ||
>[k_1]; | ||
}, | ||
{ [k_2 in keyof z.baseObjectInputType<T>]: z.baseObjectInputType<T>[k_2] } | ||
> { | ||
return z.object(shape, params).passthrough(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters