Skip to content
Merged
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
9 changes: 9 additions & 0 deletions next/src/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
93 changes: 93 additions & 0 deletions next/test/fields/mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})