Skip to content

Commit 76d5422

Browse files
committed
fix(next): fixes a bug where a fields required status was not updated through conditional schemas
1 parent 44375c8 commit 76d5422

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

next/src/mutations.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ function processBranch(fields: Field[], values: SchemaValue, branch: JsfSchema,
154154
}
155155
}
156156

157+
// Go through the `required` array and mark all fields included in the array as required
158+
if (Array.isArray(branch.required)) {
159+
fields.forEach((field) => {
160+
if (branch.required!.includes(field.name)) {
161+
field.required = true
162+
}
163+
})
164+
}
165+
157166
// Apply rules to the branch
158167
applySchemaRules(fields, values, branch as JsfObjectSchema, options)
159168
}

next/test/fields/mutation.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,97 @@ describe('field mutation', () => {
350350
})
351351
})
352352
})
353+
354+
it('blah', () => {
355+
const schema: JsfObjectSchema = {
356+
'additionalProperties': false,
357+
'allOf': [
358+
{
359+
else: {
360+
properties: {
361+
dependent_details: false,
362+
},
363+
},
364+
if: {
365+
properties: {
366+
has_dependents: {
367+
const: 'yes',
368+
},
369+
},
370+
required: [
371+
'has_dependents',
372+
],
373+
},
374+
then: {
375+
required: [
376+
'dependent_details',
377+
],
378+
},
379+
},
380+
],
381+
'properties': {
382+
dependent_details: {
383+
'items': {
384+
'properties': {
385+
first_name: {
386+
'maxLength': 255,
387+
'title': 'First name',
388+
'type': 'string',
389+
'x-jsf-presentation': {
390+
inputType: 'text',
391+
},
392+
},
393+
},
394+
'required': [
395+
'first_name',
396+
],
397+
'type': 'object',
398+
'x-jsf-order': [
399+
'first_name',
400+
],
401+
},
402+
'title': 'Dependent',
403+
'type': 'array',
404+
'x-jsf-presentation': {
405+
addFieldText: 'Add new section for dependent',
406+
inputType: 'group-array',
407+
},
408+
},
409+
has_dependents: {
410+
'oneOf': [
411+
{
412+
const: 'yes',
413+
title: 'Yes',
414+
},
415+
{
416+
const: 'no',
417+
title: 'No',
418+
},
419+
],
420+
'title': 'Do you have dependents to claim?',
421+
'type': 'string',
422+
'x-jsf-presentation': {
423+
direction: 'row',
424+
inputType: 'radio',
425+
},
426+
},
427+
},
428+
'required': [
429+
'has_dependents',
430+
],
431+
'type': 'object',
432+
'x-jsf-order': [
433+
'has_dependents',
434+
'dependent_details',
435+
],
436+
}
437+
438+
const form = createHeadlessForm(schema)
439+
440+
expect(form.fields[1].required).toBe(false)
441+
442+
form.handleValidation({ has_dependents: 'yes' })
443+
444+
expect(form.fields[1].required).toBe(true)
445+
})
353446
})

0 commit comments

Comments
 (0)