From 42addd8c8df87a0a93e9ae824acdf11f43e55030 Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Fri, 18 Nov 2022 14:16:58 +0100 Subject: [PATCH 1/4] added formData to validation function Signed-off-by: Alex Rybchenko --- plugins/scaffolder/api-report.md | 9 +++++---- plugins/scaffolder/src/extensions/types.ts | 3 ++- .../TemplateWizardPage/Stepper/createAsyncValidators.ts | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/scaffolder/api-report.md b/plugins/scaffolder/api-report.md index 33a5f8c2fe..f94cb26d21 100644 --- a/plugins/scaffolder/api-report.md +++ b/plugins/scaffolder/api-report.md @@ -110,9 +110,9 @@ export type EntityPickerUiOptions = export const EntityTagsPickerFieldExtension: FieldExtensionComponent< string[], { - showCounts?: boolean | undefined; - kinds?: string[] | undefined; helperText?: string | undefined; + kinds?: string[] | undefined; + showCounts?: boolean | undefined; } >; @@ -120,9 +120,9 @@ export const EntityTagsPickerFieldExtension: FieldExtensionComponent< export const EntityTagsPickerFieldSchema: FieldSchema< string[], { - showCounts?: boolean | undefined; - kinds?: string[] | undefined; helperText?: string | undefined; + kinds?: string[] | undefined; + showCounts?: boolean | undefined; } >; @@ -224,6 +224,7 @@ export type NextCustomFieldValidator = ( field: FieldValidation_2, context: { apiHolder: ApiHolder; + formData: JsonObject; }, ) => void | Promise; diff --git a/plugins/scaffolder/src/extensions/types.ts b/plugins/scaffolder/src/extensions/types.ts index 3834b39c0a..5e133c676b 100644 --- a/plugins/scaffolder/src/extensions/types.ts +++ b/plugins/scaffolder/src/extensions/types.ts @@ -23,6 +23,7 @@ import { } from '@rjsf/utils'; import { PropsWithChildren } from 'react'; import { JSONSchema7 } from 'json-schema'; +import { JsonObject } from '@backstage/types'; /** * Field validation type for Custom Field Extensions. @@ -100,7 +101,7 @@ export interface NextFieldExtensionComponentProps< export type NextCustomFieldValidator = ( data: TFieldReturnValue, field: FieldValidationV5, - context: { apiHolder: ApiHolder }, + context: { apiHolder: ApiHolder; formData: JsonObject }, ) => void | Promise; /** diff --git a/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/createAsyncValidators.ts b/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/createAsyncValidators.ts index 1355387eda..6483ce2393 100644 --- a/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/createAsyncValidators.ts +++ b/plugins/scaffolder/src/next/TemplateWizardPage/Stepper/createAsyncValidators.ts @@ -42,7 +42,7 @@ export const createAsyncValidators = ( if (validator) { const fieldValidation = createFieldValidation(); try { - await validator(value, fieldValidation, context); + await validator(value, fieldValidation, { ...context, formData }); } catch (ex) { fieldValidation.addError(ex.message); } From bef58bf44210fef30b3a4f2b4cea79ce1176c203 Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Fri, 18 Nov 2022 14:55:29 +0100 Subject: [PATCH 2/4] api report Signed-off-by: Alex Rybchenko --- plugins/scaffolder/api-report.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/scaffolder/api-report.md b/plugins/scaffolder/api-report.md index f94cb26d21..46b2ea3c8a 100644 --- a/plugins/scaffolder/api-report.md +++ b/plugins/scaffolder/api-report.md @@ -110,9 +110,9 @@ export type EntityPickerUiOptions = export const EntityTagsPickerFieldExtension: FieldExtensionComponent< string[], { - helperText?: string | undefined; - kinds?: string[] | undefined; showCounts?: boolean | undefined; + kinds?: string[] | undefined; + helperText?: string | undefined; } >; @@ -120,9 +120,9 @@ export const EntityTagsPickerFieldExtension: FieldExtensionComponent< export const EntityTagsPickerFieldSchema: FieldSchema< string[], { - helperText?: string | undefined; - kinds?: string[] | undefined; showCounts?: boolean | undefined; + kinds?: string[] | undefined; + helperText?: string | undefined; } >; From 9000952e872d9ea5b27a5b09afe47722657d87ae Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Tue, 29 Nov 2022 17:55:10 +0100 Subject: [PATCH 3/4] added changeset Signed-off-by: Alex Rybchenko --- .changeset/nasty-dragons-melt.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/nasty-dragons-melt.md diff --git a/.changeset/nasty-dragons-melt.md b/.changeset/nasty-dragons-melt.md new file mode 100644 index 0000000000..b083ebce62 --- /dev/null +++ b/.changeset/nasty-dragons-melt.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder': minor +--- + +All form data is now passed to validator functions in 'next' scaffolder, so it's now possible to perform validation for fields that depend on other field values From 6b261d9b29da56c64b059a5c2321ea1f7a37ade2 Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Tue, 13 Dec 2022 18:05:44 +0100 Subject: [PATCH 4/4] updated changeset Signed-off-by: Alex Rybchenko --- .changeset/nasty-dragons-melt.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.changeset/nasty-dragons-melt.md b/.changeset/nasty-dragons-melt.md index b083ebce62..2f6610ff31 100644 --- a/.changeset/nasty-dragons-melt.md +++ b/.changeset/nasty-dragons-melt.md @@ -1,5 +1,15 @@ --- -'@backstage/plugin-scaffolder': minor +'@backstage/plugin-scaffolder': patch --- -All form data is now passed to validator functions in 'next' scaffolder, so it's now possible to perform validation for fields that depend on other field values +Form data is now passed to validator functions in 'next' scaffolder, so it's now possible to perform validation for fields that depend on other field values. This is something that we discourage due to the coupling that it creates, but is sometimes still the most sensible solution. + +```typescript jsx +export const myCustomValidation = ( + value: string, + validation: FieldValidation, + { apiHolder, formData }: { apiHolder: ApiHolder; formData: JsonObject }, +) => { + // validate +}; +```