Skip to content

FIO-10011: fixed an issue where requared validation is failed when DateTime component value is set as a Date (moment js object) #249

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

Merged
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
18 changes: 18 additions & 0 deletions src/process/validation/rules/__tests__/validateRequired.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import moment from 'moment';

import { FieldError } from 'error';
import { validateRequired } from '../validateRequired';
Expand All @@ -11,6 +12,7 @@ import {
simpleRadioField,
simpleCheckBoxField,
requiredAddressManualMode,
simpleDateTimeField,
} from './fixtures/components';
import { processOne } from 'processes/processOne';
import { generateProcessorContext } from './fixtures/util';
Expand Down Expand Up @@ -253,4 +255,20 @@ describe('validateRequired', function () {
const result = await validateRequired(context);
expect(result).to.equal(null);
});

it('Validating a required component with moment.js object', async function () {
const component = { ...simpleDateTimeField, validate: { required: true } };
const data = { component: moment() };
const context = generateProcessorContext(component, data);
const result = await validateRequired(context);
expect(result).to.equal(null);
});

it('Validating a required component with Date object', async function () {
const component = { ...simpleDateTimeField, validate: { required: true } };
const data = { component: new Date() };
const context = generateProcessorContext(component, data);
const result = await validateRequired(context);
expect(result).to.equal(null);
});
});
7 changes: 7 additions & 0 deletions src/process/validation/rules/validateRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ const valueIsPresent = (
else if (Array.isArray(value) && value.length === 0) {
return false;
}
// Check for moment.js object or Date object
else if (
typeof value === 'object' &&
(value._isAMomentObject === true || value instanceof Date)
) {
return true;
}
// Recursively evaluate
else if (typeof value === 'object' && !isNestedDatatype) {
return Object.values(value).some((val) =>
Expand Down
Loading