chore: make sure to keep the existing uiSchema when parsing dependencies

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-01-29 14:12:36 +01:00
parent c2c2cd636b
commit a8b0c802b0
2 changed files with 114 additions and 3 deletions
@@ -412,6 +412,113 @@ describe('extractSchemaFromStep', () => {
});
});
it('doesnt override existing uiSchema with things from dependencies', () => {
const inputSchema: JsonObject = {
type: 'object',
title:
"Field 2 depend on field 1, field 1 is a radio button but it's visible as a field",
required: ['exampleField1'],
properties: {
exampleField0: {
title: 'Radio button that is not a dependency',
type: 'string',
enum: ['foo', 'bar'],
'ui:widget': 'radio',
},
exampleField1: {
title: 'Radio button input that is a dependency',
type: 'string',
enum: ['visible', 'hidden'],
'ui:widget': 'radio',
},
},
dependencies: {
exampleField1: {
oneOf: [
{
properties: {
exampleField1: {
enum: ['visible'],
},
exampleField2: {
title: 'FIELD 2',
type: 'string',
description: 'Explanation',
},
},
},
{
properties: {
exampleField1: {
enum: ['hidden'],
},
},
},
],
},
},
};
const expectedSchema = {
type: 'object',
title:
"Field 2 depend on field 1, field 1 is a radio button but it's visible as a field",
required: ['exampleField1'],
properties: {
exampleField0: {
title: 'Radio button that is not a dependency',
type: 'string',
enum: ['foo', 'bar'],
},
exampleField1: {
title: 'Radio button input that is a dependency',
type: 'string',
enum: ['visible', 'hidden'],
},
},
dependencies: {
exampleField1: {
oneOf: [
{
properties: {
exampleField1: {
enum: ['visible'],
},
exampleField2: {
title: 'FIELD 2',
type: 'string',
description: 'Explanation',
},
},
},
{
properties: {
exampleField1: {
enum: ['hidden'],
},
},
},
],
},
},
};
const expectedUiSchema = {
exampleField0: {
'ui:widget': 'radio',
},
exampleField1: {
'ui:widget': 'radio',
},
exampleField2: {},
};
expect(extractSchemaFromStep(inputSchema)).toEqual({
schema: expectedSchema,
uiSchema: expectedUiSchema,
});
});
it('transforms conditional schema', () => {
const inputSchema: JsonObject = {
type: 'object',
@@ -58,9 +58,13 @@ function extractUiSchema(schema: JsonObject, uiSchema: JsonObject) {
if (!isObject(schemaNode)) {
continue;
}
const innerUiSchema = {};
uiSchema[propName] = innerUiSchema;
extractUiSchema(schemaNode, innerUiSchema);
if (!isObject(uiSchema[propName])) {
const innerUiSchema = {};
uiSchema[propName] = innerUiSchema;
}
extractUiSchema(schemaNode, uiSchema[propName] as JsonObject);
}
}