Skip to content

Commit 64b569c

Browse files
authored
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.
1 parent 26fd0e9 commit 64b569c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/core/src/mappers/renderer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
isVisible,
7474
Resolve,
7575
resolveSchema,
76+
decode,
7677
} from '../util';
7778
import {
7879
Translator,
@@ -114,7 +115,7 @@ const isRequired = (
114115
rootSchema: JsonSchema
115116
): boolean => {
116117
const pathSegments = schemaPath.split('/');
117-
const lastSegment = pathSegments[pathSegments.length - 1];
118+
const lastSegment = decode(pathSegments[pathSegments.length - 1]);
118119
// Skip "properties", "items" etc. to resolve the parent
119120
const nextHigherSchemaSegments = pathSegments.slice(
120121
0,

packages/core/test/mappers/renderer.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,38 @@ test('mapStateToControlProps - i18n errors - custom keyword wins over all other
18711871
t.is(props.errors, 'this is my error custom error message');
18721872
});
18731873

1874+
test('mapStateToControlProps - required is calculated correctly from encoded JSON Pointer', (t) => {
1875+
const uischema: ControlElement = {
1876+
type: 'Control',
1877+
scope: '#/properties/~1firstName',
1878+
};
1879+
const schema = {
1880+
type: 'object',
1881+
properties: {
1882+
'/firstName': { type: 'string' },
1883+
},
1884+
required: ['/firstName'],
1885+
};
1886+
const ownProps = {
1887+
visible: true,
1888+
uischema,
1889+
path: 'foo',
1890+
schema,
1891+
};
1892+
const state = {
1893+
jsonforms: {
1894+
core: {
1895+
schema,
1896+
data: {},
1897+
uischema,
1898+
errors: [] as ErrorObject[],
1899+
},
1900+
},
1901+
};
1902+
const props = mapStateToControlProps(state, ownProps);
1903+
t.true(props.required === true);
1904+
});
1905+
18741906
test('mapStateToEnumControlProps - i18n - should not crash without i18n', (t) => {
18751907
const ownProps = {
18761908
uischema: coreUISchema,

0 commit comments

Comments
 (0)