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-9511: fixed day component min/max validation message #210

Merged
merged 1 commit into from
Jan 6, 2025
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
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
Loading