diff --git a/src/process/validation/rules/__tests__/validateRequired.test.ts b/src/process/validation/rules/__tests__/validateRequired.test.ts index 7e57b2a9..6d86198c 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, simpleRadioField } from './fixtures/components'; import { processOne } from 'processes/processOne'; import { generateProcessorContext } from './fixtures/util'; import { ProcessorsContext, ValidationScope } from 'types'; @@ -25,6 +26,56 @@ 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: [ + { + 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 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); + 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;