Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: matching required paths with slashes #2367

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 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];
let lastSegment = pathSegments[pathSegments.length - 1];
adamskeeled marked this conversation as resolved.
Show resolved Hide resolved
// Skip "properties", "items" etc. to resolve the parent
const nextHigherSchemaSegments = pathSegments.slice(
0,
Expand All @@ -127,6 +128,9 @@ const isRequired = (
rootSchema
);

// decode JSON Pointer escape sequences
lastSegment = decode(lastSegment);

adamskeeled marked this conversation as resolved.
Show resolved Hide resolved
return (
nextHigherSchema !== undefined &&
nextHigherSchema.required !== undefined &&
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
Loading