From 06f44b95bd2b0fd168343a0d4cf074d010980dc8 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 24 Mar 2022 09:51:00 +0100 Subject: [PATCH] more schema stuff Signed-off-by: blam --- .../TemplateWizardPage/Stepper/schema.test.ts | 77 +++++++++++++++++++ .../next/TemplateWizardPage/Stepper/schema.ts | 24 ++++++ .../TemplateWizardPage/TemplateWizardPage.tsx | 2 +- 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.test.ts diff --git a/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.test.ts b/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.test.ts new file mode 100644 index 0000000000..b368b832a4 --- /dev/null +++ b/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.test.ts @@ -0,0 +1,77 @@ +/* + * Copyright 2022 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 { extractSchemaFromManifest } from './schema'; + +describe('schema utils', () => { + it('should do stuff', () => { + const inputSchema = { + type: 'object', + 'ui:welp': 'warp', + properties: { + field1: { + type: 'string', + 'ui:derp': 'herp', + }, + field2: { + type: 'object', + properties: { + fieldX: { + type: 'string', + 'ui:derp': 'xerp', + }, + }, + }, + }, + }; + const expectedSchema = { + type: 'object', + properties: { + field1: { + type: 'string', + }, + field2: { + type: 'object', + properties: { + fieldX: { + type: 'string', + }, + }, + }, + }, + }; + const expectedUiSchema = { + 'ui:welp': 'warp', + field1: { + 'ui:derp': 'herp', + }, + field2: { + fieldX: { + 'ui:derp': 'xerp', + }, + }, + }; + + expect( + extractSchemaFromManifest({ + title: 'test', + steps: [{ title: 'test', schema: inputSchema }], + }), + ).toEqual({ + schema: expectedSchema, + uiSchema: expectedUiSchema, + }); + }); +}); diff --git a/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.ts b/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.ts index b61d59e88d..6140cfd978 100644 --- a/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.ts +++ b/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/schema.ts @@ -13,3 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { UiSchema } from '@rjsf/core'; +import { JSONSchema7 } from 'json-schema'; + +import { TemplateParameterSchema } from '../../../types'; + +export const extractSchemaFromManifest = ( + manifest: TemplateParameterSchema, +): { uiSchema: UiSchema; schema: JSONSchema7 } => { + const schema = manifest.steps[0].schema; + const uiSchema = JSON.parse(JSON.stringify(schema), (key, value) => { + if (typeof value === 'object') { + return Object.fromEntries( + Object.entries(value).filter( + ([k, v]) => k.startsWith('ui:') || typeof v === 'object', + ), + ); + } + + return value; + }); + + console.log(uiSchema); + return { uiSchema, schema: {} }; +}; diff --git a/plugins/scaffolder/src/next/TemplateWizardPage/TemplateWizardPage.tsx b/plugins/scaffolder/src/next/TemplateWizardPage/TemplateWizardPage.tsx index 5e0b20d468..58059f55bf 100644 --- a/plugins/scaffolder/src/next/TemplateWizardPage/TemplateWizardPage.tsx +++ b/plugins/scaffolder/src/next/TemplateWizardPage/TemplateWizardPage.tsx @@ -90,7 +90,7 @@ export const TemplateWizardPage = (_props: TemplateWizardPageProps) => { noPadding titleTypographyProps={{ component: 'h2' }} > - + )}