Skip to content

Commit

Permalink
fix: decode before performing 'required' check
Browse files Browse the repository at this point in the history
Previously the segment which was checked against 'required'
was not decoded, leading to issues in paths with '/' or '~' characters.
  • Loading branch information
adamskeeled authored Aug 16, 2024
1 parent 26fd0e9 commit 64b569c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/src/mappers/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
isVisible,
Resolve,
resolveSchema,
decode,
} from '../util';
import {
Translator,
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions packages/core/test/mappers/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 64b569c

Please sign in to comment.