Skip to content

Commit

Permalink
wip: add zod validation to openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
kainpets committed Jul 16, 2024
1 parent 182ab4c commit fe40050
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {
Openapi,
OpenapiOperation,
OpenapiParameter,
OpenapiPathItem,
OpenapiPaths,
OpenapiSchema,
import {
type Openapi,
type OpenapiOperation,
type OpenapiParameter,
type OpenapiPathItem,
type OpenapiPaths,
type OpenapiSchema,
openapiSchema,
} from './openapi.js'

export interface Blueprint {
Expand Down Expand Up @@ -132,15 +133,16 @@ export interface TypesModule {
}

export const createBlueprint = ({ openapi }: TypesModule): Blueprint => {
const validatedOpenapi = openapiSchema.parse(openapi)
const isFakeData = openapi.info.title === 'Foo'
const targetPath = '/acs/systems/list'
const targetSchema = 'acs_system'

return {
title: openapi.info.title,
routes: createRoutes(openapi.paths, isFakeData, targetPath),
title: validatedOpenapi.info.title,
routes: createRoutes(validatedOpenapi.paths, isFakeData, targetPath),

Check failure on line 143 in src/lib/blueprint.ts

View workflow job for this annotation

GitHub Actions / Format code

Unsafe argument of type `any` assigned to a parameter of type `OpenapiPaths`
resources: createResources(
openapi.components.schemas,
validatedOpenapi.components.schemas,

Check failure on line 145 in src/lib/blueprint.ts

View workflow job for this annotation

GitHub Actions / Format code

Unsafe argument of type `any` assigned to a parameter of type `Record<string, OpenapiSchema>`
isFakeData,
targetSchema,
),
Expand Down
19 changes: 19 additions & 0 deletions src/lib/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { z } from "zod"

export interface Openapi {
openapi: string
info: OpenapiInfo
Expand Down Expand Up @@ -80,3 +82,20 @@ export interface OpenapiComponents {
}

export type OpenapiSecurity = Record<string, string[]>

const xPropertiesSchema = z.object({
'x-undocumented': z.boolean().optional(),
'x-deprecated': z.string().optional(),
}).passthrough()

const recursiveSchema: z.ZodType<any> = z.lazy(() =>
z.union([
xPropertiesSchema,
z.array(recursiveSchema),
z.record(recursiveSchema)
])
)

export const openapiSchema = recursiveSchema

export type ValidatedOpenapi = z.infer<typeof openapiSchema>

0 comments on commit fe40050

Please sign in to comment.