diff --git a/src/process/validation/rules/__tests__/validateDay.test.ts b/src/process/validation/rules/__tests__/validateDay.test.ts index 52abde36..a35f4865 100644 --- a/src/process/validation/rules/__tests__/validateDay.test.ts +++ b/src/process/validation/rules/__tests__/validateDay.test.ts @@ -57,3 +57,73 @@ it('Validating a day component with a valid Date object will return a field erro expect(result).to.be.instanceOf(FieldError); expect(result?.errorKeyOrMessage).to.equal('invalidDay'); }); + +it('Validating a day component with hidden day field with an valid date string value will return null', async () => { + const component = simpleDayField; + component.fields.day.hide = true; + const data = { + component: '03/2023', + }; + const context = generateProcessorContext(component, data); + const result = await validateDay(context); + expect(result).to.equal(null); +}); + +it('Validating a day component with hidden day field with invalid date will return a field error', async () => { + const component = simpleDayField; + component.fields.day.hide = true; + const data = { + component: '13/2023', + }; + const context = generateProcessorContext(component, data); + const result = await validateDay(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('invalidDay'); +}); + +it('Validating a day component with hidden month field with an valid date string value will return null', async () => { + const component = simpleDayField; + component.fields.month.hide = true; + const data = { + component: '23/2023', + }; + const context = generateProcessorContext(component, data); + const result = await validateDay(context); + expect(result).to.equal(null); +}); + +it('Validating a day component with hidden month field with invalid date will return a field error', async () => { + const component = simpleDayField; + component.fields.month.hide = true; + const data = { + component: '130/2023', + }; + const context = generateProcessorContext(component, data); + const result = await validateDay(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('invalidDay'); +}); + +it('Validating a day component with hidden year field with an valid date string value will return null', async () => { + const component = simpleDayField; + component.fields.year.hide = true; + const data = { + component: '01/23', + }; + const context = generateProcessorContext(component, data); + const result = await validateDay(context); + expect(result).to.equal(null); +}); + +it('Validating a day component with hidden year field with invalid date will return a field error', async () => { + const component = simpleDayField; + component.fields.year.hide = true; + const data = { + component: '13/23', + }; + const context = generateProcessorContext(component, data); + const result = await validateDay(context); + expect(result).to.be.instanceOf(FieldError); + expect(result?.errorKeyOrMessage).to.equal('invalidDay'); +}); + diff --git a/src/process/validation/rules/validateDay.ts b/src/process/validation/rules/validateDay.ts index 73ec54bb..5b7a81e0 100644 --- a/src/process/validation/rules/validateDay.ts +++ b/src/process/validation/rules/validateDay.ts @@ -57,13 +57,38 @@ export const validateDaySync: RuleFnSync = (context: ValidationContext) => { if (typeof value !== 'string') { return error; } - const [DAY, MONTH, YEAR] = (component as DayComponent).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], - maxDay = getDaysInMonthCount(month, year); + let [DAY, MONTH, YEAR] = (component as DayComponent).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 as DayComponent).fields.day.hide) { + MONTH = MONTH === 0 ? 0 : MONTH - 1; + YEAR = YEAR - 1; + day = 0; + month = values[MONTH]; + year = values[YEAR]; + + }; + if((component as DayComponent).fields.month.hide) { + DAY = DAY === 0 ? 0 : DAY - 1; + YEAR = YEAR - 1; + day = values[DAY]; + month = 0; + year = values[YEAR]; + }; + if((component as DayComponent).fields.year.hide) { + day = values[DAY]; + month = values[MONTH]; + year = 0; + }; + } + + const maxDay = getDaysInMonthCount(month, year); + if (isNaN(day) || day < 0 || day > maxDay) { return error; }