Skip to content

Commit 13879c4

Browse files
Fixes nested fieldset validations
1 parent be89e5c commit 13879c4

File tree

3 files changed

+79
-35
lines changed

3 files changed

+79
-35
lines changed

v0/src/tests/createHeadlessForm.test.js

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import {
6363
schemaForErrorMessageSpecificity,
6464
jsfConfigForErrorMessageSpecificity,
6565
schemaInputTypeFile,
66-
schemaWithRootFieldsetsConditionals,
66+
schemaWithNestedFieldsetsConditionals,
6767
} from './helpers';
6868
import { mockConsole, restoreConsoleAndEnsureItWasNotCalled } from './testUtils';
6969
import { createHeadlessForm } from '@/createHeadlessForm';
@@ -2202,44 +2202,78 @@ describe('createHeadlessForm', () => {
22022202
});
22032203
});
22042204

2205-
describe('supports root fieldsets conditionals', () => {
2205+
describe('supports fieldsets conditionals over nested fieldsets', () => {
22062206
it('Given a basic retirement, the perks.has_pension is hidden', async () => {
22072207
const { fields, handleValidation } = createHeadlessForm(
2208-
schemaWithRootFieldsetsConditionals,
2208+
schemaWithNestedFieldsetsConditionals,
22092209
{}
22102210
);
22112211
const validateForm = (vals) => friendlyError(handleValidation(vals));
22122212

2213-
expect(validateForm({})).toEqual({
2213+
expect(validateForm({ perks: {} })).toEqual({
22142214
perks: {
2215-
retirement: 'Required field',
2215+
benefits_package: 'Required field',
2216+
},
2217+
});
2218+
2219+
// retirement_plan is not visible
2220+
expect(getField(fields, 'perks', 'retirement_plan').isVisible).toBe(false);
2221+
2222+
// can submit without retirement_plan
2223+
expect(validateForm({ perks: { benefits_package: 'basic' } })).toBeUndefined();
2224+
2225+
expect(
2226+
validateForm({
2227+
perks: { benefits_package: 'plus', has_retirement_plan: 'yes' },
2228+
})
2229+
).toEqual({
2230+
perks: {
2231+
retirement_plan: {
2232+
plan_name: 'Required field',
2233+
year: 'Required field',
2234+
amount: 'Required field',
2235+
},
22162236
},
22172237
});
22182238

2219-
// has_pension is not visible
2220-
expect(getField(fields, 'perks', 'has_pension').isVisible).toBe(false);
2239+
// retirement_plan becomes visible
2240+
expect(getField(fields, 'perks', 'retirement_plan').isVisible).toBe(true);
22212241

22222242
expect(
22232243
validateForm({
2224-
perks: { retirement: 'plus' },
2244+
perks: {
2245+
benefits_package: 'plus',
2246+
has_retirement_plan: 'yes',
2247+
retirement_plan: { plan_name: 'test', year: 2025 },
2248+
},
22252249
})
22262250
).toEqual({
22272251
perks: {
2228-
has_pension: 'Required field',
2252+
retirement_plan: {
2253+
amount: 'Required field',
2254+
},
22292255
},
22302256
});
22312257

2232-
// field becomes visible
2233-
expect(getField(fields, 'perks', 'has_pension').isVisible).toBe(true);
2258+
// submit with valid retirement_plan
2259+
expect(
2260+
validateForm({
2261+
perks: {
2262+
benefits_package: 'plus',
2263+
has_retirement_plan: 'yes',
2264+
retirement_plan: { plan_name: 'test', year: 2025, amount: 1000 },
2265+
},
2266+
})
2267+
).toBeUndefined();
22342268

22352269
expect(
22362270
validateForm({
2237-
perks: { retirement: 'basic' },
2271+
perks: { benefits_package: 'plus', has_retirement_plan: 'no' },
22382272
})
22392273
).toBeUndefined();
22402274

2241-
// field becomes invisible
2242-
expect(getField(fields, 'perks', 'has_pension').isVisible).toBe(false);
2275+
// retirement_plan becomes invisible
2276+
expect(getField(fields, 'perks', 'retirement_plan').isVisible).toBe(false);
22432277
});
22442278
});
22452279
});

v0/src/tests/helpers.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,6 @@ export const schemaWithConditionalToFieldset = {
19391939
then: {
19401940
properties: {
19411941
pto: {
1942-
$comment: '@BUG: This description does not disappear once activated.',
19431942
description: 'Above 30 hours, the PTO needs to be at least 20 days.',
19441943
minimum: 20,
19451944
},
@@ -1972,14 +1971,14 @@ export const schemaWithConditionalToFieldset = {
19721971
required: ['perks', 'work_hours_per_week'],
19731972
};
19741973

1975-
export const schemaWithRootFieldsetsConditionals = {
1974+
export const schemaWithNestedFieldsetsConditionals = {
19761975
additionalProperties: false,
19771976
type: 'object',
19781977
properties: {
19791978
perks: {
19801979
additionalProperties: false,
19811980
properties: {
1982-
retirement: {
1981+
benefits_package: {
19831982
oneOf: [
19841983
{
19851984
const: 'basic',
@@ -1990,13 +1989,13 @@ export const schemaWithRootFieldsetsConditionals = {
19901989
title: 'Plus',
19911990
},
19921991
],
1993-
title: 'Retirement',
1992+
title: 'Benefits package',
19941993
type: 'string',
19951994
'x-jsf-presentation': {
19961995
inputType: 'radio',
19971996
},
19981997
},
1999-
has_pension: {
1998+
has_retirement_plan: {
20001999
oneOf: [
20012000
{
20022001
const: 'yes',
@@ -2007,30 +2006,48 @@ export const schemaWithRootFieldsetsConditionals = {
20072006
title: 'No',
20082007
},
20092008
],
2010-
title: 'Has pension',
2009+
title: 'Has retirement plan?',
20112010
type: 'string',
20122011
'x-jsf-presentation': {
20132012
inputType: 'radio',
20142013
},
20152014
},
2015+
retirement_plan: {
2016+
type: 'object',
2017+
title: 'Retirement plan',
2018+
properties: {
2019+
plan_name: {
2020+
type: 'string',
2021+
title: 'Plan name',
2022+
},
2023+
year: {
2024+
type: 'number',
2025+
title: 'Year',
2026+
},
2027+
amount: {
2028+
type: 'number',
2029+
title: 'Amount',
2030+
},
2031+
},
2032+
required: ['plan_name', 'year', 'amount'],
2033+
},
20162034
},
2017-
required: ['retirement'],
2035+
required: ['benefits_package'],
20182036
title: 'Perks',
20192037
type: 'object',
20202038
'x-jsf-presentation': {
20212039
inputType: 'fieldset',
20222040
},
20232041
},
20242042
},
2025-
required: ['perks'],
20262043
allOf: [
20272044
{
20282045
if: {
20292046
properties: {
20302047
perks: {
20312048
properties: {
2032-
retirement: {
2033-
const: 'basic',
2049+
has_retirement_plan: {
2050+
const: 'no',
20342051
},
20352052
},
20362053
},
@@ -2040,18 +2057,11 @@ export const schemaWithRootFieldsetsConditionals = {
20402057
properties: {
20412058
perks: {
20422059
properties: {
2043-
has_pension: false,
2060+
retirement_plan: false,
20442061
},
20452062
},
20462063
},
20472064
},
2048-
else: {
2049-
properties: {
2050-
perks: {
2051-
required: ['has_pension'],
2052-
},
2053-
},
2054-
},
20552065
},
20562066
],
20572067
};

v0/src/yupSchema.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,9 @@ export function buildYupSchema(field, config, logic) {
467467
const fieldSetShape = {};
468468
innerFields.forEach((fieldSetfield) => {
469469
if (fieldSetfield.fields) {
470-
fieldSetShape[fieldSetfield.name] = object().shape(
471-
buildFieldSetSchema(fieldSetfield.fields)
472-
);
470+
fieldSetShape[fieldSetfield.name] = object()
471+
.shape(buildFieldSetSchema(fieldSetfield.fields))
472+
.nullable();
473473
} else {
474474
fieldSetShape[fieldSetfield.name] = buildYupSchema(
475475
{

0 commit comments

Comments
 (0)