diff --git a/plugins/scaffolder-react/src/next/components/ReviewState/ReviewState.tsx b/plugins/scaffolder-react/src/next/components/ReviewState/ReviewState.tsx index b601f77bf6..e3c7bc303e 100644 --- a/plugins/scaffolder-react/src/next/components/ReviewState/ReviewState.tsx +++ b/plugins/scaffolder-react/src/next/components/ReviewState/ReviewState.tsx @@ -38,10 +38,11 @@ export const ReviewState = (props: ReviewStateProps) => { .map(([key, value]) => { for (const step of props.schemas) { const parsedSchema = new JSONSchema(step.mergedSchema); - const definitionInSchema = parsedSchema.getSchema( - `#/${key}`, - props.formState, - ); + const definitionInSchema = parsedSchema.getSchema({ + pointer: `#/${key}`, + data: props.formState, + }); + if (definitionInSchema) { const backstageReviewOptions = definitionInSchema['ui:backstage']?.review; diff --git a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts index f50f44d8f5..d11d0b9b3b 100644 --- a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts +++ b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts @@ -17,7 +17,11 @@ import { FieldValidation } from '@rjsf/utils'; import type { JsonObject, JsonValue } from '@backstage/types'; import { ApiHolder } from '@backstage/core-plugin-api'; -import { Draft07 as JSONSchema } from 'json-schema-library'; +import { + Draft07 as JSONSchema, + JsonError, + JsonSchema, +} from 'json-schema-library'; import { createFieldValidation, extractSchemaFromStep } from '../../lib'; import { CustomFieldValidator, @@ -30,6 +34,11 @@ export type FormValidation = { [name: string]: FieldValidation | FormValidation; }; +const isJsonError = ( + value: JsonError | JsonSchema, +): value is { type: 'error'; message: string } => + 'type' in value && value.type === 'error'; + /** @alpha */ export const createAsyncValidators = ( rootSchema: JsonObject, @@ -74,9 +83,23 @@ export const createAsyncValidators = ( }; for (const [key, value] of Object.entries(current)) { - const path = `${pathPrefix}/${key}`; - const definitionInSchema = parsedSchema.getSchema(path, formData); - const { schema, uiSchema } = extractSchemaFromStep(definitionInSchema); + const pointer = `${pathPrefix}/${key}`; + const definitionInSchema = parsedSchema.getSchema({ + pointer, + data: formData, + }); + + if (!definitionInSchema) { + continue; + } + + if (isJsonError(definitionInSchema)) { + throw new Error(definitionInSchema.message); + } + + const { schema, uiSchema } = extractSchemaFromStep( + definitionInSchema as JsonObject, + ); const hasItems = definitionInSchema && definitionInSchema.items; @@ -102,7 +125,7 @@ export const createAsyncValidators = ( } }; - if (definitionInSchema && 'ui:field' in definitionInSchema) { + if ('ui:field' in definitionInSchema) { await doValidateItem(definitionInSchema, schema, uiSchema); } else if (hasItems && 'ui:field' in definitionInSchema.items) { await doValidate(definitionInSchema.items); @@ -113,7 +136,7 @@ export const createAsyncValidators = ( await doValidate(propValue); } } else if (isObject(value)) { - formValidation[key] = await validate(formData, path, value); + formValidation[key] = await validate(formData, pointer, value); } }