diff --git a/src/process/validation/i18n/en.ts b/src/process/validation/i18n/en.ts index d1de6ca3..53a78a3b 100644 --- a/src/process/validation/i18n/en.ts +++ b/src/process/validation/i18n/en.ts @@ -33,5 +33,8 @@ export const EN_ERRORS = { valueIsNotAvailable: '{{ field }} is an invalid value.', time: '{{field}} is not a valid time.', invalidDate: '{{field}} is not a valid date', - number: '{{field}} is not a valid number.' + number: '{{field}} is not a valid number.', + requiredDayField: '{{ field }} is required', + requiredMonthField: '{{ field }} is required', + requiredYearField: '{{ field }} is required' }; diff --git a/src/process/validation/rules/__tests__/validateRequiredDay.test.ts b/src/process/validation/rules/__tests__/validateRequiredDay.test.ts new file mode 100644 index 00000000..6492843c --- /dev/null +++ b/src/process/validation/rules/__tests__/validateRequiredDay.test.ts @@ -0,0 +1,53 @@ +import {expect} from 'chai' +import {FieldError} from 'error'; +import {simpleDayField} from './fixtures/components'; +import {generateProcessorContext} from "./fixtures/util"; +import {validateRequiredDay} from '../validateRequiredDay' + +it('Validating a day component without data will return a requiredDayEmpty FieldError', async () => { + const component = { + ...simpleDayField, + fields: {day: {required: true}, month: {required: false}, year: {required: false}} + }; + const data = {} + const context = generateProcessorContext(component, data); + const result = await validateRequiredDay(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('requiredDayEmpty'); +}); + +it('Validating a day component that requires day will return a FieldError if day is not given', async () => { + const component = { + ...simpleDayField, + fields: {day: {required: true}} + } + const data = {component: "01/00/2024"}; + const context = generateProcessorContext(component, data); + const result = await validateRequiredDay(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('requiredDayField'); +}); + +it('Validating a day component that requires month will return a FieldError if month is not given', async () => { + const component = { + ...simpleDayField, + fields: {month: {required: true}} + } + const data = {component: "00/01/2024"}; + const context = generateProcessorContext(component, data); + const result = await validateRequiredDay(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('requiredMonthField'); +}); + +it('Validating a day component that requires year will return a FieldError if year is not given', async () => { + const component = { + ...simpleDayField, + fields: {year: {required: true}} + }; + const data = {component: "01/01/0000"}; + const context = generateProcessorContext(component, data); + const result = await validateRequiredDay(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('requiredYearField'); +}); diff --git a/src/process/validation/rules/validateRequiredDay.ts b/src/process/validation/rules/validateRequiredDay.ts index dcc08095..f2917e9a 100644 --- a/src/process/validation/rules/validateRequiredDay.ts +++ b/src/process/validation/rules/validateRequiredDay.ts @@ -6,17 +6,13 @@ const isValidatableDayComponent = (component: any): component is DayComponent => return ( component && component.type === 'day' && - component.fields.day && - component.fields.day.required + (component.fields?.day?.required || component.fields?.month?.required || component.fields?.year?.required) ); }; export const shouldValidate = (context: ValidationContext) => { const { component } = context; - if (!isValidatableDayComponent(component)) { - return false; - } - return true; + return isValidatableDayComponent(component); }; export const validateRequiredDay: RuleFn = async (context: ValidationContext) => { @@ -28,6 +24,9 @@ export const validateRequiredDaySync: RuleFnSync = (context: ValidationContext) if (!shouldValidate(context)) { return null; } + if(!isValidatableDayComponent(component)){ + return null; + } if (!value) { return new FieldError('requiredDayEmpty', context, 'day'); } @@ -38,19 +37,19 @@ export const validateRequiredDaySync: RuleFnSync = (context: ValidationContext) 'validate:validateRequiredDay' ); } - const [DAY, MONTH, YEAR] = (component as DayComponent).dayFirst ? [0, 1, 2] : [1, 0, 2]; + const [DAY, MONTH, YEAR] = (component).dayFirst ? [0, 1, 2] : [1, 0, 2]; const values = value.split('/').map((x) => parseInt(x, 10)), day = values[DAY], month = values[MONTH], year = values[YEAR]; - if (!day && (component as DayComponent).fields.day.required === true) { + if (!day && component.fields?.day?.required) { return new FieldError('requiredDayField', context, 'day'); } - if (!month && (component as DayComponent).fields.month.required === true) { + if (!month && component.fields?.month?.required) { return new FieldError('requiredMonthField', context, 'day'); } - if (!year && (component as DayComponent).fields.year.required === true) { + if (!year && component.fields?.year?.required) { return new FieldError('requiredYearField', context, 'day'); } return null;