Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-8798: updated day component validation #140

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading