Skip to content

Commit

Permalink
FIO-9344 fixed require validation for day component (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
HannaKurban authored Nov 14, 2024
1 parent 310d86d commit 4cc23fc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
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

0 comments on commit 4cc23fc

Please sign in to comment.