Skip to content

Commit

Permalink
Fix legacy type into zod fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Jun 9, 2024
1 parent 7d8f4ef commit a8cc538
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export default defineConfig({
],
}),
],
external: ['itty-router', 'zod', '@asteasolutions/zod-to-openapi', 'js-yaml'],
external: ['itty-router', 'zod', '@asteasolutions/zod-to-openapi', 'js-yaml', 'openapi3-ts'],
})
3 changes: 1 addition & 2 deletions src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from '@asteasolutions/zod-to-openapi'
import { OpenAPIRegistryMerger } from './zod/registry'
import { z } from 'zod'
import { OpenAPIObject } from 'openapi3-ts/oas31'
import yaml from 'js-yaml'

export type OpenAPIRouterType<M> = {
Expand Down Expand Up @@ -95,7 +94,7 @@ export class OpenAPIHandler {
}
}

getGeneratedSchema(): OpenAPIObject {
getGeneratedSchema() {
let openapiGenerator: any = OpenApiGeneratorV31
if (this.options?.openapiVersion === '3')
openapiGenerator = OpenApiGeneratorV3
Expand Down
27 changes: 16 additions & 11 deletions src/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
import { z } from 'zod'
import { isSpecificZodType, legacyTypeIntoZod } from './zod/utils'
import { RouteParameter } from '@asteasolutions/zod-to-openapi/dist/openapi-registry'
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'

extendZodWithOpenApi(z)
export function convertParams<M = z.ZodType>(field: any, params: any): M {
params = params || {}
if (params.required === false)
Expand Down Expand Up @@ -44,19 +46,13 @@ export function Obj(fields: object, params?: ParameterType): z.ZodObject<any> {
}

export function Num(params?: ParameterType): z.ZodNumber {
return convertParams<z.ZodNumber>(
z.number().or(z.string()).pipe(z.number()),
params
).openapi({
return convertParams<z.ZodNumber>(z.number(), params).openapi({
type: 'number',
})
}

export function Int(params?: ParameterType): z.ZodNumber {
return convertParams<z.ZodNumber>(
z.number().int().or(z.string()).pipe(z.number()),
params
).openapi({
return convertParams<z.ZodNumber>(z.number().int(), params).openapi({
type: 'integer',
})
}
Expand Down Expand Up @@ -223,11 +219,20 @@ export function coerceInputs(
if (_val === 'true' || _val === 'false') {
params[key] = _val === 'true'
}
} else if (isSpecificZodType(innerType, 'ZodNumber')) {
} else if (
isSpecificZodType(innerType, 'ZodNumber') ||
innerType instanceof z.ZodNumber
) {
params[key] = parseFloat(params[key])
} else if (isSpecificZodType(innerType, 'ZodBigInt')) {
} else if (
isSpecificZodType(innerType, 'ZodBigInt') ||
innerType instanceof z.ZodBigInt
) {
params[key] = parseInt(params[key])
} else if (isSpecificZodType(innerType, 'ZodDate')) {
} else if (
isSpecificZodType(innerType, 'ZodDate') ||
innerType instanceof z.ZodDate
) {
params[key] = new Date(params[key])
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/zod/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class OpenAPIRegistryMerger extends OpenAPIRegistry {
public _definitions: OpenAPIDefinitions[] = []

merge(registry: OpenAPIRegistryMerger): void {
if (!registry || !registry._definitions) return

for (const definition of registry._definitions) {
this._definitions.push({ ...definition })
}
Expand Down
8 changes: 2 additions & 6 deletions src/zod/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ export function legacyTypeIntoZod(type: any, params?: any): z.ZodType {
return type
}

// Legacy support
if (type.generator === true) {
return new type(params) as z.ZodType
}

if (type === String) {
return Str(params)
}
Expand Down Expand Up @@ -86,5 +81,6 @@ export function legacyTypeIntoZod(type: any, params?: any): z.ZodType {
return Obj(type, params)
}

throw new Error(`${type} not implemented`)
// Legacy support
return type(params)
}

0 comments on commit a8cc538

Please sign in to comment.