diff --git a/index.js b/index.js index 5958c934..d2b8b725 100644 --- a/index.js +++ b/index.js @@ -680,7 +680,15 @@ function buildArrayTypeCondition (type, accessor) { condition = `obj${accessor} === null` break case 'string': - condition = `typeof obj${accessor} === 'string'` + condition = `typeof obj${accessor} === 'string' || + obj${accessor} === null || + obj${accessor} instanceof Date || + obj${accessor} instanceof RegExp || + ( + typeof obj${accessor} === "object" && + typeof obj${accessor}.toString === "function" && + obj${accessor}.toString !== Object.prototype.toString + )` break case 'integer': condition = `Number.isInteger(obj${accessor})` @@ -740,8 +748,7 @@ function buildMultiTypeSerializer (context, location, input) { ( typeof ${input} === "object" && typeof ${input}.toString === "function" && - ${input}.toString !== Object.prototype.toString && - !(${input} instanceof Date) + ${input}.toString !== Object.prototype.toString ) ) ${nestedResult} diff --git a/test/array.test.js b/test/array.test.js index 6aa2f83a..fbffe02f 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -31,12 +31,36 @@ function buildTest (schema, toStringify, options) { const stringify = build(schema, options) const output = stringify(toStringify) - t.same(JSON.parse(output), toStringify) + t.same(JSON.parse(output), JSON.parse(JSON.stringify(toStringify))) t.equal(output, JSON.stringify(toStringify)) t.ok(validate(JSON.parse(output)), 'valid schema') }) } +buildTest({ + title: 'dates tuple', + type: 'object', + properties: { + dates: { + type: 'array', + minItems: 2, + maxItems: 2, + items: [ + { + type: 'string', + format: 'date-time' + }, + { + type: 'string', + format: 'date-time' + } + ] + } + } +}, { + dates: [new Date(1), new Date(2)] +}) + buildTest({ title: 'string array', type: 'object',