Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/server/zod-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ZodV4Internal {
_zod?: {
def?: {
type?: string;
typeName?: string;
value?: unknown;
values?: unknown[];
shape?: Record<string, AnySchema> | (() => Record<string, AnySchema>);
Expand Down Expand Up @@ -236,15 +237,16 @@ export function getSchemaDescription(schema: AnySchema): string | undefined {
* Works with both Zod v3 and v4.
*/
export function isSchemaOptional(schema: AnySchema): boolean {
// Check for isOptional method first (works for both v3 and v4 usually)
if (typeof (schema as { isOptional?: () => boolean }).isOptional === 'function') {
return (schema as { isOptional: () => boolean }).isOptional();
}

if (isZ4Schema(schema)) {
const v4Schema = schema as unknown as ZodV4Internal;
return v4Schema._zod?.def?.type === 'optional';
return v4Schema._zod?.def?.type === 'optional' || v4Schema._zod?.def?.typeName === 'ZodOptional';
}
const v3Schema = schema as unknown as ZodV3Internal;
// v3 has isOptional() method
if (typeof (schema as { isOptional?: () => boolean }).isOptional === 'function') {
return (schema as { isOptional: () => boolean }).isOptional();
}
return v3Schema._def?.typeName === 'ZodOptional';
}

Expand Down
Loading