Skip to content

Commit

Permalink
fix to throw out invalid date objects (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianstormtaylor committed Sep 13, 2018
1 parent 47b8543 commit a0f644d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/types.js
Expand Up @@ -11,7 +11,6 @@ const TYPES = [
'array',
'boolean',
'buffer',
'date',
'error',
'float32array',
'float64array',
Expand Down Expand Up @@ -52,6 +51,15 @@ TYPES.forEach(type => {
Types[type] = value => kindOf(value) === type
})

/**
* Handle the 'date' case specially, to throw out invalid `Date` objects.
*
* @param {Mixed} value
* @return {Boolean}
*/

Types.date = value => kindOf(value) === 'date' && !isNaN(value)

/**
* Export.
*
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/built-ins/date-invalid-date.js
@@ -0,0 +1,14 @@
import { struct } from '../../..'

const invalid = new Date('invalid')

export const Struct = struct('date')

export const data = invalid

export const error = {
path: [],
value: invalid,
type: 'date',
reason: null,
}
12 changes: 12 additions & 0 deletions test/fixtures/built-ins/date-invalid.js
@@ -0,0 +1,12 @@
import { struct } from '../../..'

export const Struct = struct('date')

export const data = 'invalid'

export const error = {
path: [],
value: 'invalid',
type: 'date',
reason: null,
}
7 changes: 7 additions & 0 deletions test/fixtures/built-ins/date-valid.js
@@ -0,0 +1,7 @@
import { struct } from '../../..'

export const Struct = struct('date')

export const data = new Date(0)

export const output = new Date(0)
12 changes: 12 additions & 0 deletions test/fixtures/built-ins/number-invalid.js
@@ -0,0 +1,12 @@
import { struct } from '../../..'

export const Struct = struct('number')

export const data = 'invalid'

export const error = {
path: [],
value: 'invalid',
type: 'number',
reason: null,
}
7 changes: 7 additions & 0 deletions test/fixtures/built-ins/number-valid.js
@@ -0,0 +1,7 @@
import { struct } from '../../..'

export const Struct = struct('number')

export const data = 42

export const output = 42
12 changes: 12 additions & 0 deletions test/fixtures/built-ins/string-invalid.js
@@ -0,0 +1,12 @@
import { struct } from '../../..'

export const Struct = struct('string')

export const data = 42

export const error = {
path: [],
value: 42,
type: 'string',
reason: null,
}
7 changes: 7 additions & 0 deletions test/fixtures/built-ins/string-valid.js
@@ -0,0 +1,7 @@
import { struct } from '../../..'

export const Struct = struct('string')

export const data = 'valid'

export const output = 'valid'

0 comments on commit a0f644d

Please sign in to comment.