From 3ce12b6f0c8e61652ac7c7685144d7fadf6a9c78 Mon Sep 17 00:00:00 2001 From: Bogdan Nechyporenko Date: Thu, 2 Mar 2023 15:53:37 +0100 Subject: [PATCH] Fix the scaffolder validator for arrays when the item is a field in the object Signed-off-by: Bogdan Nechyporenko --- .../Stepper/createAsyncValidators.ts | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts index f5f3088c93..ceef701255 100644 --- a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts +++ b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts @@ -77,51 +77,39 @@ export const createAsyncValidators = ( const definitionInSchema = parsedSchema.getSchema(path, formData); const { schema, uiSchema } = extractSchemaFromStep(definitionInSchema); - if (definitionInSchema && 'ui:field' in definitionInSchema) { - if ('ui:field' in definitionInSchema) { - await validateForm( - definitionInSchema['ui:field'], - key, - value, - schema, - uiSchema, - ); - } - } else if ( - definitionInSchema && - definitionInSchema.items && - 'ui:field' in definitionInSchema.items - ) { - if ('ui:field' in definitionInSchema.items) { + const hasItems = definitionInSchema && definitionInSchema.items; + + const doValidateItem = async ( + propValue: JsonObject, + itemSchema: JsonObject, + itemUiSchema: NextFieldExtensionUiSchema, + ) => { + await validateForm( + propValue['ui:field'] as string, + key, + value, + itemSchema, + itemUiSchema, + ); + }; + + const doValidate = async (propValue: JsonObject) => { + if ('ui:field' in propValue) { const { schema: itemsSchema, uiSchema: itemsUiSchema } = extractSchemaFromStep(definitionInSchema.items); - await validateForm( - definitionInSchema.items['ui:field'], - key, - value, - itemsSchema, - itemsUiSchema, - ); + await doValidateItem(propValue, itemsSchema, itemsUiSchema); } - } else if ( - definitionInSchema && - definitionInSchema.items && - definitionInSchema.items.type === 'object' - ) { + }; + + if (definitionInSchema && 'ui:field' in definitionInSchema) { + await doValidateItem(definitionInSchema, schema, uiSchema); + } else if (hasItems && 'ui:field' in definitionInSchema.items) { + await doValidate(definitionInSchema.items); + } else if (hasItems && definitionInSchema.items.type === 'object') { const properties = (definitionInSchema.items?.properties ?? []) as JsonObject[]; for (const [, propValue] of Object.entries(properties)) { - if ('ui:field' in propValue) { - const { schema: itemsSchema, uiSchema: itemsUiSchema } = - extractSchemaFromStep(definitionInSchema.items); - await validateForm( - propValue['ui:field'] as string, - key, - value, - itemsSchema, - itemsUiSchema, - ); - } + await doValidate(propValue); } } else if (isObject(value)) { formValidation[key] = await validate(formData, path, value);