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
6 changes: 5 additions & 1 deletion src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ interface Context extends Required<BlueprintOptions> {
codeSampleDefinitions: CodeSampleDefinition[]
resourceSampleDefinitions: ResourceSampleDefinition[]
validActionAttemptTypes: string[]
schemas: Record<string, unknown>
}

export const TypesModuleSchema = z.object({
Expand All @@ -427,6 +428,7 @@ export const TypesModuleSchema = z.object({
.default([]),
// TODO: Import and use openapi zod schema here
openapi: z.any(),
schemas: z.record(z.string(), z.unknown()).optional().default({}),
})

export type TypesModuleInput = z.input<typeof TypesModuleSchema>
Expand All @@ -441,7 +443,7 @@ export const createBlueprint = async (
typesModule: TypesModuleInput,
{ formatCode = async (content) => content }: BlueprintOptions = {},
): Promise<Blueprint> => {
const { codeSampleDefinitions, resourceSampleDefinitions } =
const { schemas, codeSampleDefinitions, resourceSampleDefinitions } =
TypesModuleSchema.parse(typesModule)

// TODO: Move openapi to TypesModuleSchema
Expand All @@ -455,6 +457,7 @@ export const createBlueprint = async (
codeSampleDefinitions,
resourceSampleDefinitions,
formatCode,
schemas,
validActionAttemptTypes,
}

Expand Down Expand Up @@ -1171,6 +1174,7 @@ const createResource = async (
async (resourceSampleDefinition) =>
await createResourceSample(resourceSampleDefinition, {
resource,
schemas: context.schemas,
formatCode: context.formatCode,
}),
),
Expand Down
19 changes: 18 additions & 1 deletion src/lib/samples/resource-sample.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { z } from 'zod'
import { z, type ZodSchema } from 'zod'

import type { Resource as BlueprintResource } from 'lib/blueprint.js'
import { JsonSchema } from 'lib/json.js'
Expand All @@ -24,6 +24,7 @@ export type ResourceSampleDefinition = z.output<

export interface ResourceSampleContext {
resource: Omit<BlueprintResource, 'resourceSamples'>
schemas: Record<string, unknown>
formatCode: (content: string, syntax: SyntaxName) => Promise<string>
}

Expand All @@ -48,6 +49,15 @@ export const createResourceSample = async (
resourceSampleDefinition: ResourceSampleDefinition,
context: ResourceSampleContext,
): Promise<ResourceSample> => {
const resourceType = resourceSampleDefinition.resource_type
const schema = toPartialZodSchema(context.schemas[resourceType])
if (schema != null) {
schema.parse(resourceSampleDefinition.properties)
} else {
// eslint-disable-next-line no-console
console.warn(`Missing Zod schema for resource ${resourceType}.`)
}

const resource: Resource = {
seam_cli: {
title: 'Seam CLI',
Expand All @@ -64,3 +74,10 @@ export const createResourceSample = async (
resource: await formatResourceRecords(resource, context),
}
}

const toPartialZodSchema = (input: unknown): ZodSchema | null => {
if (typeof input !== 'object') return null
if (input == null) return null
if ('deepPartial' in input) return input as unknown as ZodSchema
return null
}