Skip to content

Commit

Permalink
Consider 'const' when calculating fitting schema
Browse files Browse the repository at this point in the history
One of the combinator props is the index of a fitting schema for
the respective data. We now also consider `const` errors when
determining this schema.

Includes a test case.
  • Loading branch information
richturner authored Jul 30, 2021
1 parent fc2d67f commit 12413c6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,8 @@ const mapStateToCombinatorRendererProps = (
'required',
'additionalProperties',
'type',
'enum'
'enum',
'const'
];
const dataIsValid = (errors: ErrorObject[]): boolean => {
return (
Expand Down
59 changes: 59 additions & 0 deletions packages/core/test/util/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ import {
JsonFormsCore,
JsonFormsState,
JsonSchema,
JsonSchema7,
mapStateToAnyOfProps,
OwnPropsOfControl,
rankWith,
RuleEffect,
UISchemaElement
Expand Down Expand Up @@ -1218,3 +1221,59 @@ test('computeLabel - should add asterisk if required but hideRequiredAsterisk is
const computedLabel = computeLabel('Test Label', true, false);
t.is(computedLabel, 'Test Label*');
});

test('mapStateToAnyOfProps - const constraint in anyOf schema should return correct indexOfFittingSchema', t => {
const uischema: ControlElement = {
type: 'Control',
scope: '#'
};
const schema: JsonSchema7 = {
anyOf: [
{
type: "object",
properties: {
type: {
const: "type1"
}
}
},
{
type: "object",
properties: {
type: {
const: "type2"
}
}
},
{
type: "object",
properties: {
type: {
const: "type3"
}
}
}
]
};
const ownProps: OwnPropsOfControl = {
visible: true,
uischema,
path: 'foo'
};
const state = {
jsonforms: {
core: {
ajv: createAjv(),
schema,
data: {
foo: { type: "type3"}
},
uischema,
errors: [] as ErrorObject[]
}
}
};
const props = mapStateToAnyOfProps(state, ownProps);
console.log(JSON.stringify(props, null, 2));
t.is(props.indexOfFittingSchema, 2);
});

0 comments on commit 12413c6

Please sign in to comment.