From 8e7f699344cc8ac874af19051c78a2f4b3d92e0c Mon Sep 17 00:00:00 2001 From: Luka Dornhecker Date: Tue, 20 May 2025 12:56:25 +0200 Subject: [PATCH] fix(next): fixes a bug where a fields required status was not updated through conditional schemas --- next/src/mutations.ts | 9 +++ next/test/fields/mutation.test.ts | 93 +++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/next/src/mutations.ts b/next/src/mutations.ts index 634818ee0..93ed53d01 100644 --- a/next/src/mutations.ts +++ b/next/src/mutations.ts @@ -154,6 +154,15 @@ function processBranch(fields: Field[], values: SchemaValue, branch: JsfSchema, } } + // Go through the `required` array and mark all fields included in the array as required + if (Array.isArray(branch.required)) { + fields.forEach((field) => { + if (branch.required!.includes(field.name)) { + field.required = true + } + }) + } + // Apply rules to the branch applySchemaRules(fields, values, branch as JsfObjectSchema, options) } diff --git a/next/test/fields/mutation.test.ts b/next/test/fields/mutation.test.ts index a0ff91c94..9dba953ec 100644 --- a/next/test/fields/mutation.test.ts +++ b/next/test/fields/mutation.test.ts @@ -350,4 +350,97 @@ describe('field mutation', () => { }) }) }) + + it('correctly updates required on fields', () => { + const schema: JsfObjectSchema = { + 'additionalProperties': false, + 'allOf': [ + { + else: { + properties: { + dependent_details: false, + }, + }, + if: { + properties: { + has_dependents: { + const: 'yes', + }, + }, + required: [ + 'has_dependents', + ], + }, + then: { + required: [ + 'dependent_details', + ], + }, + }, + ], + 'properties': { + dependent_details: { + 'items': { + 'properties': { + first_name: { + 'maxLength': 255, + 'title': 'First name', + 'type': 'string', + 'x-jsf-presentation': { + inputType: 'text', + }, + }, + }, + 'required': [ + 'first_name', + ], + 'type': 'object', + 'x-jsf-order': [ + 'first_name', + ], + }, + 'title': 'Dependent', + 'type': 'array', + 'x-jsf-presentation': { + addFieldText: 'Add new section for dependent', + inputType: 'group-array', + }, + }, + has_dependents: { + 'oneOf': [ + { + const: 'yes', + title: 'Yes', + }, + { + const: 'no', + title: 'No', + }, + ], + 'title': 'Do you have dependents to claim?', + 'type': 'string', + 'x-jsf-presentation': { + direction: 'row', + inputType: 'radio', + }, + }, + }, + 'required': [ + 'has_dependents', + ], + 'type': 'object', + 'x-jsf-order': [ + 'has_dependents', + 'dependent_details', + ], + } + + const form = createHeadlessForm(schema) + + expect(form.fields[1].required).toBe(false) + + form.handleValidation({ has_dependents: 'yes' }) + + expect(form.fields[1].required).toBe(true) + }) })