Skip to content

Commit

Permalink
fix: validate required with Date as value
Browse files Browse the repository at this point in the history
When the value of an input is a Date object, the required validator
returns an error.

Added check if value is a Date when validating object in required check.

Closes #41
  • Loading branch information
Gido Manders committed Jun 28, 2022
1 parent 916aa24 commit 348f27b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NumberError,
NumberFractionError
} from './errors';
import { isDate } from 'lodash';

/* eslint-disable @typescript-eslint/no-explicit-any */

Expand All @@ -21,7 +22,9 @@ export function makeRequired(label: string): FieldValidator<any> {
value === '' ||
(typeof value === 'string' && value.trim() === '') ||
(Array.isArray(value) && value.length === 0) ||
(typeof value === 'object' && Object.keys(value).length === 0) ||
(typeof value === 'object' &&
!isDate(value) &&
Object.keys(value).length === 0) ||
// Prevent users from validating boolean values by always
// considering it an error, they should use `makeBooleanRequired`
// instead.
Expand Down
3 changes: 2 additions & 1 deletion tests/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function makeValidatorChecker(
}

test('required', async () => {
expect.assertions(15);
expect.assertions(16);

const validator = makeRequired('Name');

Expand Down Expand Up @@ -138,6 +138,7 @@ test('required', async () => {
await checkValidator({ value: 'henkie', expected: undefined });
await checkValidator({ value: { test: true }, expected: undefined });
await checkValidator({ value: ['test'], expected: undefined });
await checkValidator({ value: new Date(), expected: undefined });
});

test('booleanRequired', async () => {
Expand Down

0 comments on commit 348f27b

Please # to comment.