Skip to content

Commit 606dfcc

Browse files
authored
feat: simultaneously test example (#263)
* add simultaneously test example * fix feature
1 parent ed6da1a commit 606dfcc

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
11931193

11941194
if (type === 'string') {
11951195
code += `
1196-
${index === 0 ? 'if' : 'else if'}(obj${accessor} === null || typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp)
1196+
${index === 0 ? 'if' : 'else if'}(obj${accessor} === null || typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp || (typeof obj${accessor} === "object" && Object.hasOwnProperty.call(obj${accessor}, "toString")))
11971197
${nestedResult.code}
11981198
`
11991199
} else if (type === 'null') {

test/typesArray.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,55 @@ test('string type array can handle dates', (t) => {
365365
})
366366
t.is(value, '{"date":"2018-04-20T07:52:31.017Z","dateObject":"2018-04-21T07:52:31.017Z"}')
367367
})
368+
369+
test('object that is simultaneously a string and a json', (t) => {
370+
t.plan(2)
371+
const schema = {
372+
type: 'object',
373+
properties: {
374+
simultaneously: {
375+
type: ['string', 'object'],
376+
properties: {
377+
foo: { type: 'string' }
378+
}
379+
}
380+
}
381+
}
382+
383+
const likeObjectId = {
384+
toString () { return 'hello' }
385+
}
386+
387+
const stringify = build(schema)
388+
const valueStr = stringify({ simultaneously: likeObjectId })
389+
t.is(valueStr, '{"simultaneously":"hello"}')
390+
391+
const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
392+
t.is(valueObj, '{"simultaneously":{"foo":"hello"}}')
393+
})
394+
395+
test('object that is simultaneously a string and a json switched', (t) => {
396+
t.plan(2)
397+
const schema = {
398+
type: 'object',
399+
properties: {
400+
simultaneously: {
401+
type: ['object', 'string'],
402+
properties: {
403+
foo: { type: 'string' }
404+
}
405+
}
406+
}
407+
}
408+
409+
const likeObjectId = {
410+
toString () { return 'hello' }
411+
}
412+
413+
const stringify = build(schema)
414+
const valueStr = stringify({ simultaneously: likeObjectId })
415+
t.is(valueStr, '{"simultaneously":{}}')
416+
417+
const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
418+
t.is(valueObj, '{"simultaneously":{"foo":"hello"}}')
419+
})

0 commit comments

Comments
 (0)