Skip to content

Commit c300641

Browse files
authored
fix: consistently handle string in array (#666)
1 parent 5bb8e1e commit c300641

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,15 @@ function buildArrayTypeCondition (type, accessor) {
681681
condition = `obj${accessor} === null`
682682
break
683683
case 'string':
684-
condition = `typeof obj${accessor} === 'string'`
684+
condition = `typeof obj${accessor} === 'string' ||
685+
obj${accessor} === null ||
686+
obj${accessor} instanceof Date ||
687+
obj${accessor} instanceof RegExp ||
688+
(
689+
typeof obj${accessor} === "object" &&
690+
typeof obj${accessor}.toString === "function" &&
691+
obj${accessor}.toString !== Object.prototype.toString
692+
)`
685693
break
686694
case 'integer':
687695
condition = `Number.isInteger(obj${accessor})`
@@ -741,8 +749,7 @@ function buildMultiTypeSerializer (context, location, input) {
741749
(
742750
typeof ${input} === "object" &&
743751
typeof ${input}.toString === "function" &&
744-
${input}.toString !== Object.prototype.toString &&
745-
!(${input} instanceof Date)
752+
${input}.toString !== Object.prototype.toString
746753
)
747754
)
748755
${nestedResult}

test/array.test.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,36 @@ function buildTest (schema, toStringify, options) {
3131
const stringify = build(schema, options)
3232
const output = stringify(toStringify)
3333

34-
t.same(JSON.parse(output), toStringify)
34+
t.same(JSON.parse(output), JSON.parse(JSON.stringify(toStringify)))
3535
t.equal(output, JSON.stringify(toStringify))
3636
t.ok(validate(JSON.parse(output)), 'valid schema')
3737
})
3838
}
3939

40+
buildTest({
41+
title: 'dates tuple',
42+
type: 'object',
43+
properties: {
44+
dates: {
45+
type: 'array',
46+
minItems: 2,
47+
maxItems: 2,
48+
items: [
49+
{
50+
type: 'string',
51+
format: 'date-time'
52+
},
53+
{
54+
type: 'string',
55+
format: 'date-time'
56+
}
57+
]
58+
}
59+
}
60+
}, {
61+
dates: [new Date(1), new Date(2)]
62+
})
63+
4064
buildTest({
4165
title: 'string array',
4266
type: 'object',

0 commit comments

Comments
 (0)