From 64b569c905cb6cea48187ce8de3d7b1f17824632 Mon Sep 17 00:00:00 2001 From: adamskeeled <66061908+adamskeeled@users.noreply.github.com> Date: Fri, 16 Aug 2024 14:08:44 +0100 Subject: [PATCH] fix: decode before performing 'required' check Previously the segment which was checked against 'required' was not decoded, leading to issues in paths with '/' or '~' characters. --- packages/core/src/mappers/renderer.ts | 3 +- packages/core/test/mappers/renderer.test.ts | 32 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/core/src/mappers/renderer.ts b/packages/core/src/mappers/renderer.ts index 60e8adfca..a3ba1a034 100644 --- a/packages/core/src/mappers/renderer.ts +++ b/packages/core/src/mappers/renderer.ts @@ -73,6 +73,7 @@ import { isVisible, Resolve, resolveSchema, + decode, } from '../util'; import { Translator, @@ -114,7 +115,7 @@ const isRequired = ( rootSchema: JsonSchema ): boolean => { const pathSegments = schemaPath.split('/'); - const lastSegment = pathSegments[pathSegments.length - 1]; + const lastSegment = decode(pathSegments[pathSegments.length - 1]); // Skip "properties", "items" etc. to resolve the parent const nextHigherSchemaSegments = pathSegments.slice( 0, diff --git a/packages/core/test/mappers/renderer.test.ts b/packages/core/test/mappers/renderer.test.ts index f52a3b470..8e686e884 100644 --- a/packages/core/test/mappers/renderer.test.ts +++ b/packages/core/test/mappers/renderer.test.ts @@ -1871,6 +1871,38 @@ test('mapStateToControlProps - i18n errors - custom keyword wins over all other t.is(props.errors, 'this is my error custom error message'); }); +test('mapStateToControlProps - required is calculated correctly from encoded JSON Pointer', (t) => { + const uischema: ControlElement = { + type: 'Control', + scope: '#/properties/~1firstName', + }; + const schema = { + type: 'object', + properties: { + '/firstName': { type: 'string' }, + }, + required: ['/firstName'], + }; + const ownProps = { + visible: true, + uischema, + path: 'foo', + schema, + }; + const state = { + jsonforms: { + core: { + schema, + data: {}, + uischema, + errors: [] as ErrorObject[], + }, + }, + }; + const props = mapStateToControlProps(state, ownProps); + t.true(props.required === true); +}); + test('mapStateToEnumControlProps - i18n - should not crash without i18n', (t) => { const ownProps = { uischema: coreUISchema,