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-9344 fixed require validation for day component #192

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
45 changes: 45 additions & 0 deletions src/process/validation/rules/__tests__/validateRequiredDay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
32 changes: 27 additions & 5 deletions src/process/validation/rules/validateRequiredDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading