Skip to content

Commit

Permalink
added tests and translations for validateRequiredDay
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenMasterJacob20011 committed Jul 17, 2024
1 parent 4432142 commit 9a10fd2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
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

0 comments on commit 9a10fd2

Please sign in to comment.