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

validateRequired test fails when validating required dateTime field with a valid Date object #214

Open
0mcandal0 opened this issue Jan 17, 2025 · 0 comments

Comments

@0mcandal0
Copy link

Describe the bug
The validateRequired test fails when validating a required dateTime field that is present in the data with a valid Date object.

Version/Branch
master

To Reproduce
Steps to reproduce the behavior:

  1. Add the following test to the code:

    it('Validating a dateTime that is required and present in the data with a valid date object will return null', 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);
    });
  2. Run the test.

  3. Observe that it fails.

Expected behavior
The test should return null when validating a valid Date object for a required dateTime field.

Additional context
After reviewing the code, the issue is resolved by modifying the valueIsPresent function to correctly handle Date values. The proposed modification is as follows:

const valueIsPresent = (
  value: any,
  considerFalseTruthy: boolean,
  isNestedDatatype?: boolean,
): boolean => {
  if (
    value === null ||
    value === undefined ||
    value === '' ||
    (!considerFalseTruthy && value === false)
  ) {
    return false;
  } else if (isEmptyObject(value)) {
    return false;
  } else if (Array.isArray(value) && value.length === 0) {
    return false;
  } else if (value instanceof Date) {
    return !isNaN(value.getTime());
  } else if (typeof value === 'object' && !isNestedDatatype) {
    return Object.values(value).some((val) =>
      valueIsPresent(val, considerFalseTruthy, isNestedDatatype),
    );
  } else if (Array.isArray(value) && value.length) {
    return doesArrayDataHaveValue(value);
  }
  return true;
};

The modification was made in the file validateRequired.ts.

With this modification, the test passes correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant