Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
62 changes: 28 additions & 34 deletions src/validation/json-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,51 +162,45 @@ function cycleThroughPropertiesAndApplyValues(schemaCopy: JsfSchema, computedVal
cycleThroughAttrsAndApplyValues(propertySchema, computedValues, computedAttrs)
}

if (propertySchema.type === 'object' && propertySchema.properties) {
cycleThroughPropertiesAndApplyValues(propertySchema, computedValues)
// If the schemas has properties, we need to cycle through each one and apply the computed values
if (typeof propertySchema.properties === 'object') {
for (const propertyName in propertySchema.properties) {
processProperty(propertySchema.properties[propertyName])
}
}

// deleting x-jsf-logic-computedAttrs to keep the schema clean
delete propertySchema['x-jsf-logic-computedAttrs']
}

// If the schemas has properties, we need to cycle through each one and apply the computed values
// Otherwise, just process the property
if (schemaCopy.properties) {
for (const propertyName in schemaCopy.properties) {
processProperty(schemaCopy.properties[propertyName])
// If the schema has an if statement, we need to cycle through the properties and apply the computed values
if (typeof propertySchema.if === 'object') {
cycleThroughPropertiesAndApplyValues(propertySchema.if, computedValues)
}
}
else {
processProperty(schemaCopy)
}

// If the schema has an if statement, we need to cycle through the properties and apply the computed values
if (typeof schemaCopy.if === 'object') {
cycleThroughPropertiesAndApplyValues(schemaCopy.if, computedValues)
}

/* If the schema has an allOf or anyOf property, we need to cycle through each property inside it and
* apply the computed values
*/
/* If the schema has an allOf or anyOf property, we need to cycle through each property inside it and
* apply the computed values
*/

if (schemaCopy.allOf && schemaCopy.allOf.length > 0) {
for (const schema of schemaCopy.allOf) {
cycleThroughPropertiesAndApplyValues(schema, computedValues)
if (propertySchema.allOf && propertySchema.allOf.length > 0) {
for (const schema of propertySchema.allOf) {
cycleThroughPropertiesAndApplyValues(schema, computedValues)
}
}
}

if (schemaCopy.anyOf && schemaCopy.anyOf.length > 0) {
for (const schema of schemaCopy.anyOf) {
cycleThroughPropertiesAndApplyValues(schema, computedValues)
if (propertySchema.anyOf && propertySchema.anyOf.length > 0) {
for (const schema of propertySchema.anyOf) {
cycleThroughPropertiesAndApplyValues(schema, computedValues)
}
}
}

if (schemaCopy.oneOf && schemaCopy.oneOf.length > 0) {
for (const schema of schemaCopy.oneOf) {
cycleThroughPropertiesAndApplyValues(schema, computedValues)
if (propertySchema.oneOf && propertySchema.oneOf.length > 0) {
for (const schema of propertySchema.oneOf) {
cycleThroughPropertiesAndApplyValues(schema, computedValues)
}
}

// deleting x-jsf-logic-computedAttrs to keep the schema clean
delete propertySchema['x-jsf-logic-computedAttrs']
}

processProperty(schemaCopy)
}

/**
Expand Down
55 changes: 55 additions & 0 deletions test/validation/json-logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,59 @@ describe('applyComputedAttrsToSchema', () => {
expect(conditionValue['x-jsf-logic-computedAttrs']).toBeUndefined()
expect(conditionValue.minimum).toBe(10)
})

it('allows to use computed values inside oneOf statements nested in a property', () => {
const schema: JsfObjectSchema = {
'type': 'object',
'properties': {
maximum_temperature: {
title: 'Maximum room temperature',
type: 'number',
description: 'What is the maximum room temperature in Celsius?',
},
temperature_setting: {
title: 'Select a preset temperature',
type: 'string',
oneOf: [
{
title: 'Low',
const: 18,
},
{
title: 'Medium',
const: 20,
},
{
'title': 'Maximum',
'x-jsf-logic-computedAttrs': {
const: 'maximum_temperature',
},
},
],
},
},
'required': [
'maximum_temperature',
'temperature_setting',
],
'x-jsf-logic': {
computedValues: {
maximum_temperature: {
rule: {
var: 'maximum_temperature',
},
},
},
},
};

// Mock the jsonLogic.apply to return 24
(jsonLogic.apply as jest.Mock).mockReturnValue(24)

const result = JsonLogicValidation.applyComputedAttrsToSchema(schema, schema['x-jsf-logic']?.computedValues, { maximum_temperature: 24 })

const temperatureSetting = result.properties?.temperature_setting as NonBooleanJsfSchema
expect(temperatureSetting['x-jsf-logic-computedAttrs']).toBeUndefined()
expect((temperatureSetting.oneOf?.[2] as NonBooleanJsfSchema)?.const).toBe(24)
})
})
Loading