From f6d7a76b0b7e9da2a058fb1550e0071698549661 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Sun, 22 Oct 2023 14:14:11 +0300 Subject: [PATCH] fix: display string pattern in array items --- src/components/Fields/ArrayItemDetails.tsx | 11 +++- .../__tests__/FieldDetails.test.tsx | 37 +++++++++++ .../__snapshots__/FieldDetails.test.tsx.snap | 64 +++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/src/components/Fields/ArrayItemDetails.tsx b/src/components/Fields/ArrayItemDetails.tsx index 163f8b38e0..bbae19911d 100644 --- a/src/components/Fields/ArrayItemDetails.tsx +++ b/src/components/Fields/ArrayItemDetails.tsx @@ -10,15 +10,22 @@ export function ArrayItemDetails({ schema }: { schema: SchemaModel }) { const { hideSchemaPattern } = React.useContext(OptionsContext); if ( !schema || - (schema.type === 'string' && !schema.constraints.length) || ((!schema?.pattern || hideSchemaPattern) && !schema.items && !schema.displayFormat && - !schema.constraints.length) // return null for cases where all constraints are empty + !schema.constraints?.length) // return null for cases where all constraints are empty ) { return null; } + if (schema.type === 'string' && schema.pattern) { + return ( + + [] + + ); + } + return ( [ items diff --git a/src/components/__tests__/FieldDetails.test.tsx b/src/components/__tests__/FieldDetails.test.tsx index 04982bb93c..0d70fe55cd 100644 --- a/src/components/__tests__/FieldDetails.test.tsx +++ b/src/components/__tests__/FieldDetails.test.tsx @@ -76,4 +76,41 @@ describe('FieldDetailsComponent', () => { expect(wrapper.render()).toMatchSnapshot(); }); + + it('renders correctly when field items have string type and pattern', () => { + const mockFieldProps = { + showExamples: true, + field: { + schema: { + type: 'array', + displayType: 'Array of strings', + title: 'test title', + externalDocs: undefined, + constraints: [''], + items: { + type: 'string', + pattern: '^see regex[0-9]$', + constraints: [''], + externalDocs: undefined, + }, + } as any as SchemaModel, + example: 'example', + name: 'name', + expanded: false, + required: false, + kind: '', + deprecated: false, + collapse: jest.fn(), + toggle: jest.fn(), + explode: false, + expand: jest.fn(), + description: 'test description', + in: undefined, + }, + renderDiscriminatorSwitch: jest.fn(), + }; + const wrapper = shallow(withTheme()); + + expect(wrapper.render()).toMatchSnapshot(); + }); }); diff --git a/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap b/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap index 801106e744..f98b667a4f 100644 --- a/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap @@ -133,3 +133,67 @@ exports[`FieldDetailsComponent renders correctly when default value is object in `; + +exports[`FieldDetailsComponent renders correctly when field items have string type and pattern 1`] = ` +
+
+ + + Array of strings + + + (test title) + + + + + + + + + [ + + ^see regex[0-9]$ + + ] + +
+ +
+ + Example: + + + + "example" + +
+
+
+

+ test description +

+ + +
+
+
+`;