Skip to content

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

@0mcandal0

Description

@0mcandal0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions