diff --git a/.changeset/afraid-trees-stare.md b/.changeset/afraid-trees-stare.md new file mode 100644 index 0000000000..ac1a53b4e4 --- /dev/null +++ b/.changeset/afraid-trees-stare.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': minor +--- + +zod to json conversion funcitonality for action template schema diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 293ff7ed29..a25da2045a 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -69,8 +69,10 @@ export function createCatalogRegisterAction(options: { } >; -// @public -export function createCatalogWriteAction(): TemplateAction_2<{ +// Warning: (ae-missing-release-tag) "createCatalogWriteAction" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export function createCatalogWriteAction(): TemplateAction<{ filePath?: string | undefined; entity: Entity; }>; diff --git a/plugins/scaffolder-backend/package.json b/plugins/scaffolder-backend/package.json index 12ef31899c..8a61c7dfac 100644 --- a/plugins/scaffolder-backend/package.json +++ b/plugins/scaffolder-backend/package.json @@ -92,7 +92,8 @@ "winston": "^3.2.1", "yaml": "^2.0.0", "zen-observable": "^0.10.0", - "zod": "~3.18.0" + "zod": "~3.18.0", + "zod-to-json-schema": "~3.18.0" }, "devDependencies": { "@backstage/backend-test-utils": "workspace:^", diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/write.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/write.ts index 5c5a62956f..ad1b1eb0c3 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/write.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/write.ts @@ -19,6 +19,8 @@ import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; import * as yaml from 'yaml'; import { Entity } from '@backstage/catalog-model'; import { resolveSafeChildPath } from '@backstage/backend-common'; +import { z } from 'zod'; +import { convertZodtoJson } from '../../../tasks'; const id = 'catalog:write'; @@ -56,28 +58,24 @@ const examples = [ * Writes a catalog descriptor file containing the provided entity to a path in the workspace. * @public */ + export function createCatalogWriteAction() { + const inputSchema = convertZodtoJson( + z.object({ + filePath: z.string().optional().describe('Defaults to catalog-info.yaml'), + entity: z + .string() + .optional() + .describe('You can provide the same values used in the Entity schema.'), + }), + ); + return createTemplateAction<{ filePath?: string; entity: Entity }>({ id, description: 'Writes the catalog-info.yaml for your template', examples, schema: { - input: { - type: 'object', - properties: { - filePath: { - title: 'Catalog file path', - description: 'Defaults to catalog-info.yaml', - type: 'string', - }, - entity: { - title: 'Entity info to write catalog-info.yaml', - description: - 'You can provide the same values used in the Entity schema.', - type: 'object', - }, - }, - }, + input: inputSchema, }, supportsDryRun: true, async handler(ctx) { diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/convertZodtoJson.ts b/plugins/scaffolder-backend/src/scaffolder/actions/convertZodtoJson.ts new file mode 100644 index 0000000000..71104814a3 --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/actions/convertZodtoJson.ts @@ -0,0 +1,41 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { z } from 'zod'; +import zodToJsonSchema from 'zod-to-json-schema'; +import { Schema } from 'jsonschema'; + +interface TemplateActionSchema { + readonly schema: { + input?: Schema; + output?: Schema; + }; +} + +/** @public */ +export function convertZodtoJson< + TInputSchema extends z.ZodType = z.ZodType, + TOutputSchema extends z.ZodType = z.ZodType, +>( + zodInputSchema?: TInputSchema, + zodOutputSchema?: TOutputSchema, +): TemplateActionSchema { + return { + schema: { + input: zodInputSchema ? zodToJsonSchema(zodInputSchema) : undefined, + output: zodOutputSchema ? zodToJsonSchema(zodOutputSchema) : undefined, + }, + }; +} diff --git a/yarn.lock b/yarn.lock index d365859add..7c9bae5be5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7716,6 +7716,7 @@ __metadata: yaml: ^2.0.0 zen-observable: ^0.10.0 zod: ~3.18.0 + zod-to-json-schema: ~3.18.0 languageName: unknown linkType: soft