chore: move the zod logic

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2023-02-23 12:34:16 +01:00
parent b239c818ff
commit b1edb1669e
3 changed files with 22 additions and 10 deletions
@@ -17,7 +17,7 @@
import { JsonObject } from '@backstage/types';
import { ConflictError, NotFoundError } from '@backstage/errors';
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import zodToJsonSchema from 'zod-to-json-schema';
/**
* Registry of all registered template actions.
* @public
@@ -31,7 +31,22 @@ export class TemplateActionRegistry {
`Template action with ID '${action.id}' has already been registered`,
);
}
this.actions.set(action.id, action);
// It's better to convert the zod here, and just deal with jsonschema everywhere
// rather than adding the zod check everywhere like the nunjucks engine, and the /actions/list
// endpoint to create jsonschema for the frontend.
const templateAction =
action.schema?.input && 'safeParseAsync' in action.schema.input
? {
...action,
schema: {
...action.schema,
input: zodToJsonSchema(action.schema.input),
},
}
: action;
this.actions.set(action.id, templateAction);
}
get(actionId: string): TemplateAction<JsonObject> {
@@ -287,13 +287,10 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
{};
if (action.schema?.input) {
// Check to see if the input is a zod schema without using instanceof.
const inputSchema =
'safeParseAsync' in action.schema.input
? zodToJsonSchema(action.schema.input)
: action.schema.input;
const validateResult = validateJsonSchema(input, inputSchema);
const validateResult = validateJsonSchema(
input,
action.schema.input,
);
if (!validateResult.valid) {
const errors = validateResult.errors.join(', ');
throw new InputError(
@@ -293,7 +293,7 @@ export async function createRouter(
id: action.id,
description: action.description,
examples: action.examples,
schema: action.schema,
schema: {},
};
});
res.json(actionsList);