Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 18 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"npm": ">= 9.0.0"
},
"peerDependencies": {
"zod": "^3.0.0"
"zod": "^3.0.0 || ^4.0.0"
},
"devDependencies": {
"@seamapi/url-search-params-serializer": "^3.0.0",
Expand All @@ -86,7 +86,8 @@
"tsc-alias": "^1.8.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2",
"typescript": "~5.3.3",
"zod": "^3.24.2"
"typescript": "~5.8.3",
"zod": "^3.24.2",
"zod-v4": "npm:zod@^4.0.0"
}
}
30 changes: 30 additions & 0 deletions src/lib/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,36 @@ test('nullable schemas', valueType, z.string().nullable(), 'string')
test('nullish schemas', valueType, z.number().nullish(), 'number')
test('default schemas', valueType, z.number().default(0), 'number')
test('readonly schemas', valueType, z.number().readonly(), 'number')
test(
'refined schemas',
valueType,
z.number().refine((v) => v > 0),
'number',
)
test(
'transformed schemas',
valueType,
z.number().transform((v) => String(v)),
'number',
)
test(
'preprocessed schemas',
valueType,
z.preprocess((v) => v, z.number()),
'number',
)
test('piped schemas', valueType, z.number().pipe(z.number().min(0)), 'number')

test('zodSchemaToParamSchema: parses refined object schemas', (t) => {
t.deepEqual(
zodSchemaToParamSchema(
z.object({ foo: z.string() }).refine((data) => data.foo !== 'a'),
),
{
foo: 'string',
},
)
})

test('string arrays', valueType, z.array(z.string()), 'string_array')
test('number arrays', valueType, z.array(z.number()), 'number_array')
Expand Down
47 changes: 30 additions & 17 deletions src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
isZodUnion,
unwrapZodSchema,
zodArrayElementType,
zodLiteralValue,
zodNativeEnumValues,
zodEnumValues,
zodLiteralValues,
zodRecordKeyType,
zodRecordValueType,
zodSchemaName,
Expand Down Expand Up @@ -376,8 +376,9 @@ const primitiveToValueType = (
if (isZodDate(schema)) return 'date'
if (isZodNull(schema)) return 'null'
if (isZodNever(schema)) return 'never'
if (isZodEnum(schema)) return 'string'
if (isZodNativeEnum(schema)) return nativeEnumToValueType(schema, path)
if (isZodEnum(schema) || isZodNativeEnum(schema)) {
return enumToValueType(schema, path)
}
if (isZodLiteral(schema)) return literalToValueType(schema, path)

throw new UnparseableSchemaError(
Expand All @@ -386,11 +387,8 @@ const primitiveToValueType = (
)
}

const nativeEnumToValueType = (
schema: ZodTypeAny,
path: string[],
): ValueType => {
const values = zodNativeEnumValues(schema)
const enumToValueType = (schema: ZodTypeAny, path: string[]): ValueType => {
const values = zodEnumValues(schema)

const valueTypes = [
...new Set(
Expand All @@ -407,21 +405,36 @@ const nativeEnumToValueType = (

throw new UnparseableSchemaError(
path,
'a native enum schema must have only string values or only number values',
'an enum schema must have only string values or only number values',
)
}

const literalToValueType = (schema: ZodTypeAny, path: string[]): ValueType => {
const value = zodLiteralValue(schema)
if (value === null) return 'null'
if (typeof value === 'string') return 'string'
if (typeof value === 'number') return 'number'
if (typeof value === 'boolean') return 'boolean'
if (value instanceof Date) return 'date'
const valueTypes = [
...new Set(
zodLiteralValues(schema).map((value) => {
if (value === null) return 'null'
if (typeof value === 'string') return 'string'
if (typeof value === 'number') return 'number'
if (typeof value === 'boolean') return 'boolean'
if (value instanceof Date) return 'date'
throw new UnparseableSchemaError(
path,
`a literal schema of type ${typeof value} is not supported`,
)
}),
),
]

const nonNullTypes = valueTypes.filter((t) => t !== 'null')

const [first] = nonNullTypes
if (first == null) return 'null'
if (nonNullTypes.length === 1) return first

throw new UnparseableSchemaError(
path,
`a literal schema of type ${typeof value} is not supported`,
'a literal schema must have values of a single type',
)
}

Expand Down
Loading
Loading