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-8645: added tests and translations for validateRequiredDay #119

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
5 changes: 4 additions & 1 deletion src/process/validation/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ export const EN_ERRORS = {
valueIsNotAvailable: '{{ field }} is an invalid value.',
time: '{{field}} is not a valid time.',
invalidDate: '{{field}} is not a valid date',
number: '{{field}} is not a valid number.'
number: '{{field}} is not a valid number.',
requiredDayField: '{{ field }} is required',
requiredMonthField: '{{ field }} is required',
requiredYearField: '{{ field }} is required'
};
53 changes: 53 additions & 0 deletions src/process/validation/rules/__tests__/validateRequiredDay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {expect} from 'chai'
import {FieldError} from 'error';
import {simpleDayField} from './fixtures/components';
import {generateProcessorContext} from "./fixtures/util";
import {validateRequiredDay} from '../validateRequiredDay'

it('Validating a day component without data will return a requiredDayEmpty FieldError', async () => {
const component = {
...simpleDayField,
fields: {day: {required: true}, month: {required: false}, year: {required: false}}
};
const data = {}
const context = generateProcessorContext(component, data);
const result = await validateRequiredDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('requiredDayEmpty');
});

it('Validating a day component that requires day will return a FieldError if day is not given', async () => {
const component = {
...simpleDayField,
fields: {day: {required: true}}
}
const data = {component: "01/00/2024"};
const context = generateProcessorContext(component, data);
const result = await validateRequiredDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('requiredDayField');
});

it('Validating a day component that requires month will return a FieldError if month is not given', async () => {
const component = {
...simpleDayField,
fields: {month: {required: true}}
}
const data = {component: "00/01/2024"};
const context = generateProcessorContext(component, data);
const result = await validateRequiredDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('requiredMonthField');
});

it('Validating a day component that requires year will return a FieldError if year is not given', async () => {
const component = {
...simpleDayField,
fields: {year: {required: true}}
};
const data = {component: "01/01/0000"};
const context = generateProcessorContext(component, data);
const result = await validateRequiredDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('requiredYearField');
});
19 changes: 9 additions & 10 deletions src/process/validation/rules/validateRequiredDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ const isValidatableDayComponent = (component: any): component is DayComponent =>
return (
component &&
component.type === 'day' &&
component.fields.day &&
component.fields.day.required
(component.fields?.day?.required || component.fields?.month?.required || component.fields?.year?.required)
);
};

export const shouldValidate = (context: ValidationContext) => {
const { component } = context;
if (!isValidatableDayComponent(component)) {
return false;
}
return true;
return isValidatableDayComponent(component);
};

export const validateRequiredDay: RuleFn = async (context: ValidationContext) => {
Expand All @@ -28,6 +24,9 @@ export const validateRequiredDaySync: RuleFnSync = (context: ValidationContext)
if (!shouldValidate(context)) {
return null;
}
if(!isValidatableDayComponent(component)){
return null;
}
if (!value) {
return new FieldError('requiredDayEmpty', context, 'day');
}
Expand All @@ -38,19 +37,19 @@ export const validateRequiredDaySync: RuleFnSync = (context: ValidationContext)
'validate:validateRequiredDay'
);
}
const [DAY, MONTH, YEAR] = (component as DayComponent).dayFirst ? [0, 1, 2] : [1, 0, 2];
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];

if (!day && (component as DayComponent).fields.day.required === true) {
if (!day && component.fields?.day?.required) {
return new FieldError('requiredDayField', context, 'day');
}
if (!month && (component as DayComponent).fields.month.required === true) {
if (!month && component.fields?.month?.required) {
return new FieldError('requiredMonthField', context, 'day');
}
if (!year && (component as DayComponent).fields.year.required === true) {
if (!year && component.fields?.year?.required) {
return new FieldError('requiredYearField', context, 'day');
}
return null;
Expand Down
Loading