Skip to content

Commit

Permalink
FIO-9511: fixed day component min/max validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
TanyaGashtold committed Jan 3, 2025
1 parent 53fdf5f commit e634ab4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('validateMaximumDay', function () {
const context = generateProcessorContext(component, data);
const result = await validateMaximumDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('maxDay');
expect(result?.errorKeyOrMessage).to.equal('maxDate');
});

it('Validating a day component with a day before the maximum day will return null', async function () {
Expand All @@ -45,7 +45,7 @@ describe('validateMaximumDay', function () {
const context = generateProcessorContext(component, data);
const result = await validateMaximumDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('maxDay');
expect(result?.errorKeyOrMessage).to.equal('maxDate');
});

it('Validating a day-first day component with a day before the maximum day will return null', async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('validateMinimumDay', function () {
const context = generateProcessorContext(component, data);
const result = await validateMinimumDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('minDay');
expect(result?.errorKeyOrMessage).to.equal('minDate');
});

it('Validating a day component with a day after the minimum day will return null', async function () {
Expand All @@ -45,7 +45,7 @@ describe('validateMinimumDay', function () {
const context = generateProcessorContext(component, data);
const result = await validateMinimumDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.contain('minDay');
expect(result?.errorKeyOrMessage).to.contain('minDate');
});

it('Validating a day-first day component with a day after the minimum day will return null', async function () {
Expand Down
12 changes: 9 additions & 3 deletions src/process/validation/rules/validateMaximumDay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ProcessorError, FieldError } from 'error';
import { DayComponent, RuleFn, RuleFnSync, ValidationContext } from 'types';
import { dayjs, isPartialDay, getDateValidationFormat, getDateSetting } from 'utils/date';
import {
dayjs,
isPartialDay,
getDateValidationFormat,
getDateSetting,
getDayFormat,
} from 'utils/date';
import { ProcessorInfo } from 'types/process/ProcessorInfo';

const isValidatableDayComponent = (component: any): component is DayComponent => {
Expand Down Expand Up @@ -47,9 +53,9 @@ export const validateMaximumDaySync: RuleFnSync = (context: ValidationContext) =
} else {
maxDate.setHours(0, 0, 0, 0);
}
const error = new FieldError('maxDay', {
const error = new FieldError('maxDate', {
...context,
maxDate: String(maxDate),
maxDate: dayjs(maxDate).format(getDayFormat(component as DayComponent)),
setting: String(maxDate),
});
return date.isBefore(dayjs(maxDate)) || date.isSame(dayjs(maxDate)) ? null : error;
Expand Down
12 changes: 9 additions & 3 deletions src/process/validation/rules/validateMinimumDay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ProcessorError, FieldError } from 'error';
import { DayComponent, RuleFn, RuleFnSync, ValidationContext } from 'types';
import { dayjs, isPartialDay, getDateValidationFormat, getDateSetting } from 'utils/date';
import {
dayjs,
isPartialDay,
getDateValidationFormat,
getDateSetting,
getDayFormat,
} from 'utils/date';
import { ProcessorInfo } from 'types/process/ProcessorInfo';

const isValidatableDayComponent = (component: any): component is DayComponent => {
Expand Down Expand Up @@ -48,9 +54,9 @@ export const validateMinimumDaySync: RuleFnSync = (context: ValidationContext) =
} else {
minDate.setHours(0, 0, 0, 0);
}
const error = new FieldError('minDay', {
const error = new FieldError('minDate', {
...context,
minDate: String(minDate),
minDate: dayjs(minDate).format(getDayFormat(component as DayComponent)),
setting: String(minDate),
});
return date.isAfter(minDate) || date.isSame(minDate) ? null : error;
Expand Down
25 changes: 24 additions & 1 deletion src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import { isNaN, isNil } from 'lodash';
import { isNaN, isNil, get } from 'lodash';
import { Evaluator } from './Evaluator';
import { DayComponent } from 'types';

Expand Down Expand Up @@ -82,6 +82,29 @@ export function formatDate(value: ConfigType, format: string, timezone?: string)
return date.format(dayjsFormat);
}

export function getDayFormat(component: DayComponent) {
let format = '';
const showDay = !get(component, 'fields.day.hide', false);
const showMonth = !get(component, 'fields.month.hide', false);
const showYear = !get(component, 'fields.year.hide', false);
if (component.dayFirst && showDay) {
format += 'D/';
}
if (showMonth) {
format += 'M/';
}
if (!component.dayFirst && showDay) {
format += 'D/';
}
if (showYear) {
format += 'YYYY';
return format;
} else {
// Trim off the "/" from the end of the format string.
return format.length ? format.substring(0, format.length - 1) : format;
}
}

/**
* Return a translated date setting.
*
Expand Down

0 comments on commit e634ab4

Please sign in to comment.