Skip to content

Commit e1e06a4

Browse files
JimmyHurrahmcollina
authored andcommitted
fix, string type arrays handling dates (#84)
1 parent 736e601 commit e1e06a4

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,12 +670,22 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
670670
type.forEach((type, index) => {
671671
var tempSchema = {type: type}
672672
var nestedResult = nested(laterCode, name, key, tempSchema, externalSchema, fullSchema, subKey)
673-
code += `
674-
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(tempSchema, {depth: null})}, obj${accessor}))
673+
if (type === 'string') {
674+
code += `
675+
${index === 0 ? 'if' : 'else if'}(obj${accessor} instanceof Date || ajv.validate(${require('util').inspect(tempSchema, {depth: null})}, obj${accessor}))
676+
${nestedResult.code}
677+
`
678+
} else {
679+
code += `
680+
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(tempSchema, {depth: null})}, obj${accessor}))
675681
${nestedResult.code}
676-
`
682+
`
683+
}
677684
laterCode = nestedResult.laterCode
678685
})
686+
code += `
687+
else json+= null
688+
`
679689
} else {
680690
throw new Error(`${type} unsupported`)
681691
}

test/typesArray.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,22 @@ test('object with anyOf and multiple types', (t) => {
182182
t.fail()
183183
}
184184
})
185+
186+
test('string type array can handle dates', (t) => {
187+
t.plan(1)
188+
const schema = {
189+
type: 'object',
190+
properties: {
191+
date: { type: ['string'] }
192+
}
193+
}
194+
const stringify = build(schema)
195+
try {
196+
const value = stringify({
197+
date: new Date('2018-04-20T07:52:31.017Z')
198+
})
199+
t.is(value, '{"date":"2018-04-20T07:52:31.017Z"}')
200+
} catch (e) {
201+
t.fail()
202+
}
203+
})

0 commit comments

Comments
 (0)