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..d42d63aa 100644 --- a/src/process/validation/rules/validateDay.ts +++ b/src/process/validation/rules/validateDay.ts @@ -50,20 +50,45 @@ export const validateDay: RuleFn = async (context: ValidationContext) => { export const validateDaySync: RuleFnSync = (context: ValidationContext) => { const { component, value } = context; - if (!shouldValidate(context)) { + if (!shouldValidate(context) || !isDayComponent(component)) { return null; } const error = new FieldError('invalidDay', context, 'day'); 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.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 = values[DAY]; + month = 0; + year = values[YEAR]; + }; + if (component.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; }