From a100e980ce37fadbe4515f95eb424dcaa618708f Mon Sep 17 00:00:00 2001 From: Ralf Eisele Date: Wed, 31 Jan 2024 20:39:20 +0100 Subject: [PATCH 1/3] Fixed required validation considering false value falsy --- .../rules/__tests__/validateRequired.test.ts | 16 ++++++++++++++++ src/process/validation/rules/validateRequired.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/process/validation/rules/__tests__/validateRequired.test.ts b/src/process/validation/rules/__tests__/validateRequired.test.ts index f9bba3df..aebd5733 100644 --- a/src/process/validation/rules/__tests__/validateRequired.test.ts +++ b/src/process/validation/rules/__tests__/validateRequired.test.ts @@ -22,6 +22,22 @@ it('Validating a simple component that is required and present in the data will expect(result).to.equal(null); }); +it('Validating a simple component that is required and present in the data with value=0 will return null', async () => { + const component = { ...simpleTextField, validate: { required: true } }; + const data = { component: 0 }; + const context = generateProcessContext(component, data); + const result = await validateRequired(context); + expect(result).to.equal(null); +}); + +it('Validating a simple component that is required and present in the data with value=false will return null', async () => { + const component = { ...simpleTextField, validate: { required: true } }; + const data = { component: false }; + const context = generateProcessContext(component, data); + const result = await validateRequired(context); + expect(result).to.equal(null); +}); + it('Validating a simple component that is not required and present in the data will return null', async () => { const component = simpleTextField; const data = { component: 'a simple value' }; diff --git a/src/process/validation/rules/validateRequired.ts b/src/process/validation/rules/validateRequired.ts index 1feb893e..1bf5257c 100644 --- a/src/process/validation/rules/validateRequired.ts +++ b/src/process/validation/rules/validateRequired.ts @@ -26,7 +26,7 @@ export const validateRequiredSync: RuleFnSync = (context: ValidationContext) => return null; } if ( - (value === null || value === undefined || isEmptyObject(value) || (!!value === false && value !== 0)) && + (value === null || value === undefined || isEmptyObject(value) || (!!value === false && value !== 0 && value !==false )) && !component.hidden ) { return error; From e8dcb0ab1a5b03c4b07d99f56b70eac2ee1ecc8a Mon Sep 17 00:00:00 2001 From: Ralf Eisele Date: Sat, 2 Mar 2024 08:43:59 +0100 Subject: [PATCH 2/3] Switch 2 tests from simpleTextField to simpleSelectBoxes to test required validation --- .../rules/__tests__/validateRequired.test.ts | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/process/validation/rules/__tests__/validateRequired.test.ts b/src/process/validation/rules/__tests__/validateRequired.test.ts index 878cc4af..8e62bdf6 100644 --- a/src/process/validation/rules/__tests__/validateRequired.test.ts +++ b/src/process/validation/rules/__tests__/validateRequired.test.ts @@ -2,7 +2,8 @@ import { expect } from 'chai'; import { FieldError } from 'error'; import { validateRequired } from '../validateRequired'; -import { conditionallyHiddenRequiredHiddenField, hiddenRequiredField, requiredNonInputField, simpleTextField } from './fixtures/components'; + +import { conditionallyHiddenRequiredHiddenField, hiddenRequiredField, requiredNonInputField, simpleTextField, simpleSelectBoxes } from './fixtures/components'; import { processOne } from 'processes/processOne'; import { generateProcessContext } from './fixtures/util'; import { ProcessorsContext, ValidationScope } from 'types'; @@ -25,16 +26,32 @@ it('Validating a simple component that is required and present in the data will expect(result).to.equal(null); }); -it('Validating a simple component that is required and present in the data with value=0 will return null', async () => { - const component = { ...simpleTextField, validate: { required: true } }; +it('Validating a simple selectbox that is required and present in the data with value=0 will return null', async () => { + const component = { ...simpleSelectBoxes, validate: { required: true }, values: [ + { + label: 'true', + value: 'true', + }, + { + label: 'Null', + value: '0', + }] }; const data = { component: 0 }; const context = generateProcessContext(component, data); const result = await validateRequired(context); expect(result).to.equal(null); }); -it('Validating a simple component that is required and present in the data with value=false will return null', async () => { - const component = { ...simpleTextField, validate: { required: true } }; +it('Validating a simple selectbox that is required and present in the data with value=false will return null', async () => { + const component = { ...simpleSelectBoxes, validate: { required: true }, values: [ + { + label: 'true', + value: 'true', + }, + { + label: 'false', + value: 'false', + }] }; const data = { component: false }; const context = generateProcessContext(component, data); const result = await validateRequired(context); From 8fe567d0e03e062fb98dba029b16cb2c1ad256be Mon Sep 17 00:00:00 2001 From: Ralf Eisele Date: Sat, 2 Mar 2024 08:57:20 +0100 Subject: [PATCH 3/3] adding select component test for required validiation with value=false --- .../rules/__tests__/validateRequired.test.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/process/validation/rules/__tests__/validateRequired.test.ts b/src/process/validation/rules/__tests__/validateRequired.test.ts index 8e62bdf6..fe3ae4e8 100644 --- a/src/process/validation/rules/__tests__/validateRequired.test.ts +++ b/src/process/validation/rules/__tests__/validateRequired.test.ts @@ -3,7 +3,7 @@ import { expect } from 'chai'; import { FieldError } from 'error'; import { validateRequired } from '../validateRequired'; -import { conditionallyHiddenRequiredHiddenField, hiddenRequiredField, requiredNonInputField, simpleTextField, simpleSelectBoxes } from './fixtures/components'; +import { conditionallyHiddenRequiredHiddenField, hiddenRequiredField, requiredNonInputField, simpleTextField, simpleSelectBoxes, simpleRadioField } from './fixtures/components'; import { processOne } from 'processes/processOne'; import { generateProcessContext } from './fixtures/util'; import { ProcessorsContext, ValidationScope } from 'types'; @@ -26,6 +26,24 @@ it('Validating a simple component that is required and present in the data will expect(result).to.equal(null); }); + +it('Validating a simple radio component that is required and present in the data with value=false will return null', async () => { + const component = { ...simpleRadioField, validate: { required: true }, values: [ + { + label: 'Yes', + value: 'true', + }, + { + label: 'No', + value: 'false', + }] }; + const data = { component: false }; + const context = generateProcessContext(component, data); + const result = await validateRequired(context); + expect(result).to.equal(null); +}); + + it('Validating a simple selectbox that is required and present in the data with value=0 will return null', async () => { const component = { ...simpleSelectBoxes, validate: { required: true }, values: [ {