diff --git a/src/process/validation/rules/__tests__/validateRequiredDay.test.ts b/src/process/validation/rules/__tests__/validateRequiredDay.test.ts index 7f4a272c..e32adef5 100644 --- a/src/process/validation/rules/__tests__/validateRequiredDay.test.ts +++ b/src/process/validation/rules/__tests__/validateRequiredDay.test.ts @@ -52,4 +52,49 @@ describe('validateRequiredDay', function () { expect(result).to.be.instanceOf(FieldError); expect(result?.errorKeyOrMessage).to.equal('requiredYearField'); }); + + it('Validating a day component that requires month and year will not return a FieldError if year and month are given', async function () { + const component = { + ...simpleDayField, + fields: { + year: { required: true }, + month: { required: true }, + day: { hide: true } + }, + }; + const data = { component: '07/2024' }; + const context = generateProcessorContext(component, data); + const result = await validateRequiredDay(context); + expect(result).to.equal(null); + }); + + it('Validating a day component that requires day and year will not return a FieldError if year and day are given', async function () { + const component = { + ...simpleDayField, + fields: { + year: { required: true }, + day: { required: true }, + month: { hide: true } + }, + }; + const data = { component: '24/2024' }; + const context = generateProcessorContext(component, data); + const result = await validateRequiredDay(context); + expect(result).to.equal(null); + }); + + it('Validating a day component that requires day and month will not return a FieldError if day and month are given', async function () { + const component = { + ...simpleDayField, + fields: { + month: { required: true }, + day: { required: true }, + year: { hide: true } + }, + }; + const data = { component: '07/24' }; + const context = generateProcessorContext(component, data); + const result = await validateRequiredDay(context); + expect(result).to.equal(null); + }); }); diff --git a/src/process/validation/rules/validateRequiredDay.ts b/src/process/validation/rules/validateRequiredDay.ts index f56aa847..cd2e1078 100644 --- a/src/process/validation/rules/validateRequiredDay.ts +++ b/src/process/validation/rules/validateRequiredDay.ts @@ -39,11 +39,33 @@ export const validateRequiredDaySync: RuleFnSync = (context: ValidationContext) 'validate:validateRequiredDay', ); } - 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]; + let [DAY, MONTH, YEAR] = component.dayFirst ? [0, 1, 2] : [1, 0, 2]; + const values = value.split('/').map((x) => parseInt(x, 10)); + let day = values[DAY]; + let month = values[MONTH]; + let year = values[YEAR]; + + if (values.length !== 3) { + if (component.fields.day.hide) { + MONTH = MONTH === 0 ? 0 : MONTH - 1; + YEAR = YEAR - 1; + day = 0; + month = values[MONTH]; + year = values[YEAR]; + } + if (component.fields.month.hide) { + DAY = DAY === 0 ? 0 : DAY - 1; + YEAR = YEAR - 1; + day = component.fields.day.hide && day === 0 ? 0 : values[DAY]; + month = 0; + year = values[YEAR]; + } + if (component.fields.year.hide) { + day = component.fields.day.hide && day === 0 ? 0 : values[DAY]; + month = component.fields.month.hide && month === 0 ? 0 : values[MONTH]; + year = 0; + } + } if (!day && component.fields?.day?.required) { return new FieldError('requiredDayField', context, 'day');