Merge pull request #14667 from dagda1/transform-errors

give the ability to supply a transformErrors function to the rjsf/core
This commit is contained in:
Fredrik Adelöw
2022-11-17 16:49:58 +01:00
committed by GitHub
6 changed files with 62 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder': minor
---
Adds the ability to supply a `transformErrors` function to the `Stepper` for `/next`
+2
View File
@@ -11,6 +11,7 @@ import { BackstagePlugin } from '@backstage/core-plugin-api';
import { ComponentType } from 'react';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { Entity } from '@backstage/catalog-model';
import type { ErrorTransformer } from '@rjsf/utils';
import { Extension } from '@backstage/core-plugin-api';
import { ExternalRouteRef } from '@backstage/core-plugin-api';
import { FetchApi } from '@backstage/core-plugin-api';
@@ -262,6 +263,7 @@ export type NextRouterProps = {
TaskPageComponent?: React_2.ComponentType<{}>;
};
groups?: TemplateGroupFilter[];
transformErrors?: ErrorTransformer;
};
// @alpha
@@ -30,6 +30,7 @@ import { TemplateEntityV1beta3 } from '@backstage/plugin-scaffolder-common';
import { TemplateGroupFilter } from '../TemplateListPage/TemplateGroups';
import { nextSelectedTemplateRouteRef } from '../../routes';
import { SecretsContextProvider } from '../../components/secrets/SecretsContext';
import type { ErrorTransformer } from '@rjsf/utils';
/**
* The Props for the Scaffolder Router
@@ -44,6 +45,7 @@ export type NextRouterProps = {
TaskPageComponent?: React.ComponentType<{}>;
};
groups?: TemplateGroupFilter[];
transformErrors?: ErrorTransformer;
};
/**
@@ -18,6 +18,7 @@ import { TemplateParameterSchema } from '../../../types';
import { Stepper } from './Stepper';
import { renderInTestApp } from '@backstage/test-utils';
import { act, fireEvent } from '@testing-library/react';
import type { RJSFValidationError } from '@rjsf/utils';
describe('Stepper', () => {
it('should render the step titles for each step of the manifest', async () => {
@@ -140,4 +141,50 @@ describe('Stepper', () => {
expect(getByText('im a custom field extension')).toBeInTheDocument();
});
it('should transform default error message', async () => {
const manifest: TemplateParameterSchema = {
steps: [
{
title: 'Step 1',
schema: {
properties: {
postcode: {
type: 'string',
pattern: '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]',
},
},
},
},
],
title: 'transformErrors Form Test',
};
const transformErrors = (errors: RJSFValidationError[]) => {
return errors.map(err =>
err.property === '.postcode'
? { ...err, message: 'invalid postcode' }
: err,
);
};
const { getByText, getByRole } = await renderInTestApp(
<Stepper
manifest={manifest}
extensions={[]}
onComplete={jest.fn()}
transformErrors={transformErrors}
/>,
);
await fireEvent.change(getByRole('textbox', { name: 'postcode' }), {
target: { value: 'invalid' },
});
await act(async () => {
await fireEvent.click(getByRole('button', { name: 'Review' }));
});
expect(getByText('invalid postcode')).toBeInTheDocument();
});
});
@@ -36,6 +36,7 @@ import { useTemplateSchema } from './useTemplateSchema';
import { ReviewState } from './ReviewState';
import validator from '@rjsf/validator-ajv8';
import { selectedTemplateRouteRef } from '../../../routes';
import type { ErrorTransformer } from '@rjsf/utils';
const useStyles = makeStyles(theme => ({
backButton: {
@@ -56,6 +57,7 @@ export interface StepperProps {
manifest: TemplateParameterSchema;
extensions: NextFieldExtensionOptions<any, any>[];
onComplete: (values: Record<string, JsonValue>) => Promise<void>;
transformErrors?: ErrorTransformer;
}
// TODO(blam): We require here, as the types in this package depend on @rjsf/core explicitly
@@ -149,6 +151,7 @@ export const Stepper = (props: StepperProps) => {
onSubmit={handleNext}
fields={extensions}
showErrorList={false}
transformErrors={props.transformErrors}
>
<div className={styles.footer}>
<Button
@@ -44,9 +44,11 @@ import {
} from '../../routes';
import { SecretsContext } from '../../components/secrets/SecretsContext';
import { JsonValue } from '@backstage/types';
import type { ErrorTransformer } from '@rjsf/utils';
export interface TemplateWizardPageProps {
customFieldExtensions: NextFieldExtensionOptions<any, any>[];
transformErrors?: ErrorTransformer;
}
const useStyles = makeStyles<BackstageTheme>(() => ({
@@ -137,6 +139,7 @@ export const TemplateWizardPage = (props: TemplateWizardPageProps) => {
manifest={manifest}
extensions={props.customFieldExtensions}
onComplete={onComplete}
transformErrors={props.transformErrors}
/>
</InfoCard>
)}