From 2dcdf8caba8adf7c4d090037e0303e624b50cef4 Mon Sep 17 00:00:00 2001 From: Rich Turner Date: Thu, 22 Jul 2021 11:47:08 +0100 Subject: [PATCH] Add support for const constraint to mapStateToCombinatorRendererProps indexOfFittingSchema resolution --- packages/core/src/util/renderer.ts | 3 +- packages/core/test/util/renderer.test.ts | 59 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/core/src/util/renderer.ts b/packages/core/src/util/renderer.ts index cd43d3ee5..c0217a5c7 100644 --- a/packages/core/src/util/renderer.ts +++ b/packages/core/src/util/renderer.ts @@ -877,7 +877,8 @@ const mapStateToCombinatorRendererProps = ( 'required', 'additionalProperties', 'type', - 'enum' + 'enum', + 'const' ]; const dataIsValid = (errors: ErrorObject[]): boolean => { return ( diff --git a/packages/core/test/util/renderer.test.ts b/packages/core/test/util/renderer.test.ts index de25b96f2..fb3237064 100644 --- a/packages/core/test/util/renderer.test.ts +++ b/packages/core/test/util/renderer.test.ts @@ -51,6 +51,9 @@ import { JsonFormsCore, JsonFormsState, JsonSchema, + JsonSchema7, + mapStateToAnyOfProps, + OwnPropsOfControl, rankWith, RuleEffect, UISchemaElement @@ -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); +});