Skip to content

Commit 12413c6

Browse files
authored
Consider 'const' when calculating fitting schema
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.
1 parent fc2d67f commit 12413c6

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

packages/core/src/util/renderer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,8 @@ const mapStateToCombinatorRendererProps = (
868868
'required',
869869
'additionalProperties',
870870
'type',
871-
'enum'
871+
'enum',
872+
'const'
872873
];
873874
const dataIsValid = (errors: ErrorObject[]): boolean => {
874875
return (

packages/core/test/util/renderer.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ import {
5151
JsonFormsCore,
5252
JsonFormsState,
5353
JsonSchema,
54+
JsonSchema7,
55+
mapStateToAnyOfProps,
56+
OwnPropsOfControl,
5457
rankWith,
5558
RuleEffect,
5659
UISchemaElement
@@ -1218,3 +1221,59 @@ test('computeLabel - should add asterisk if required but hideRequiredAsterisk is
12181221
const computedLabel = computeLabel('Test Label', true, false);
12191222
t.is(computedLabel, 'Test Label*');
12201223
});
1224+
1225+
test('mapStateToAnyOfProps - const constraint in anyOf schema should return correct indexOfFittingSchema', t => {
1226+
const uischema: ControlElement = {
1227+
type: 'Control',
1228+
scope: '#'
1229+
};
1230+
const schema: JsonSchema7 = {
1231+
anyOf: [
1232+
{
1233+
type: "object",
1234+
properties: {
1235+
type: {
1236+
const: "type1"
1237+
}
1238+
}
1239+
},
1240+
{
1241+
type: "object",
1242+
properties: {
1243+
type: {
1244+
const: "type2"
1245+
}
1246+
}
1247+
},
1248+
{
1249+
type: "object",
1250+
properties: {
1251+
type: {
1252+
const: "type3"
1253+
}
1254+
}
1255+
}
1256+
]
1257+
};
1258+
const ownProps: OwnPropsOfControl = {
1259+
visible: true,
1260+
uischema,
1261+
path: 'foo'
1262+
};
1263+
const state = {
1264+
jsonforms: {
1265+
core: {
1266+
ajv: createAjv(),
1267+
schema,
1268+
data: {
1269+
foo: { type: "type3"}
1270+
},
1271+
uischema,
1272+
errors: [] as ErrorObject[]
1273+
}
1274+
}
1275+
};
1276+
const props = mapStateToAnyOfProps(state, ownProps);
1277+
console.log(JSON.stringify(props, null, 2));
1278+
t.is(props.indexOfFittingSchema, 2);
1279+
});

0 commit comments

Comments
 (0)