From 6ca13414f25faaac6fcd9a95b0ada1f0ca3af71d Mon Sep 17 00:00:00 2001 From: Bogdan Nechyporenko Date: Thu, 2 Mar 2023 15:37:43 +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.test.ts | 35 +++++++++++++++++++ .../Stepper/createAsyncValidators.ts | 20 +++++++++++ 2 files changed, 55 insertions(+) diff --git a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.test.ts b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.test.ts index 38543b2148..3633410769 100644 --- a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.test.ts +++ b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.test.ts @@ -406,4 +406,39 @@ describe('createAsyncValidators', () => { expect(validators.TagField).not.toHaveBeenCalled(); }); + + it('should call validator for array object property from a custom field extension', async () => { + const schema: JsonObject = { + type: 'object', + properties: { + links: { + title: 'Links', + type: 'array', + items: { + type: 'object', + required: ['url', 'title', 'icon'], + properties: { + url: { + title: 'url', + description: 'url', + type: 'object', + 'ui:field': 'CustomLinkField', + }, + }, + }, + }, + }, + }; + const validators = { CustomLinkField: jest.fn() }; + + const validate = createAsyncValidators(schema, validators, { + apiHolder: { get: jest.fn() }, + }); + + await validate({ + links: [{ url: 'http://my-url.spotify.com' }], + }); + + expect(validators.CustomLinkField).toHaveBeenCalled(); + }); }); diff --git a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts index 31185d1c12..f5f3088c93 100644 --- a/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts +++ b/plugins/scaffolder-react/src/next/components/Stepper/createAsyncValidators.ts @@ -103,6 +103,26 @@ export const createAsyncValidators = ( itemsUiSchema, ); } + } else if ( + definitionInSchema && + definitionInSchema.items && + 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, + ); + } + } } else if (isObject(value)) { formValidation[key] = await validate(formData, path, value); }