Skip to content

Commit

Permalink
FIO-8798: updated day component validation (#140)
Browse files Browse the repository at this point in the history
* FIO-8798: updated day component validation

* FIO-8798: add isDayComponent type guard
  • Loading branch information
Maria-Golomb authored Aug 26, 2024
1 parent 7e1bebc commit 630bc96
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 7 deletions.
70 changes: 70 additions & 0 deletions src/process/validation/rules/__tests__/validateDay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

39 changes: 32 additions & 7 deletions src/process/validation/rules/validateDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 630bc96

Please sign in to comment.