Skip to content

serializing null value crash fix #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function $asBooleanNullable (bool) {
function $asDatetime (date) {
if (date instanceof Date) {
return '"' + date.toISOString() + '"'
} else if (typeof date.toISOString === 'function') {
} else if (date && typeof date.toISOString === 'function') {
return '"' + date.toISOString() + '"'
} else {
return $asString(date)
Expand All @@ -292,7 +292,7 @@ function $asDate (date) {
var month = new Intl.DateTimeFormat('en', { month: '2-digit' }).format(date)
var day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date)
return '"' + year + '-' + month + '-' + day + '"'
} else if (typeof date.format === 'function') {
} else if (date && typeof date.format === 'function') {
return '"' + date.format('YYYY-MM-DD') + '"'
} else {
return $asString(date)
Expand All @@ -305,7 +305,7 @@ function $asTime (date) {
var minute = new Intl.DateTimeFormat('en', { minute: 'numeric' }).format(date)
var second = new Intl.DateTimeFormat('en', { second: 'numeric' }).format(date)
return '"' + $pad2Zeros(hour) + ':' + $pad2Zeros(minute) + ':' + $pad2Zeros(second) + '"'
} else if (typeof date.format === 'function') {
} else if (date && typeof date.format === 'function') {
return '"' + date.format('HH:mm:ss') + '"'
} else {
return $asString(date)
Expand Down Expand Up @@ -1077,9 +1077,10 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
sortedTypes.forEach((type, index) => {
var tempSchema = Object.assign({}, schema, { type })
var nestedResult = nested(laterCode, name, key, tempSchema, externalSchema, fullSchema, subKey)

if (type === 'string') {
code += `
${index === 0 ? 'if' : 'else if'}(typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp)
${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)
${nestedResult.code}
`
} else if (type === 'null') {
Expand Down
208 changes: 208 additions & 0 deletions test/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,211 @@ test('render a nested object in a string when type is date-format as ISOString',
t.equal(output, JSON.stringify(toStringify))
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('serializing null value', t => {
const input = { updatedAt: null }

function createSchema (properties) {
return {
title: 'an object in a string',
type: 'object',
properties
}
}

function serialize (schema, input) {
const validate = validator(schema)
const stringify = build(schema)
const output = stringify(input)

return {
validate,
output
}
}

t.plan(3)

t.test('type::string', t => {
t.plan(3)

t.test('format::date-time', t => {
t.plan(2)

const prop = {
updatedAt: {
type: 'string',
format: 'date-time'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":""}')
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date-time format')
})

t.test('format::date', t => {
t.plan(2)

const prop = {
updatedAt: {
type: 'string',
format: 'date'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":""}')
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date format')
})

t.test('format::time', t => {
t.plan(2)

const prop = {
updatedAt: {
type: 'string',
format: 'time'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":""}')
t.notOk(validate(JSON.parse(output)), 'an empty string is not a time format')
})
})

t.test('type::array', t => {
t.plan(3)

t.test('format::date-time', t => {
t.plan(2)

const prop = {
updatedAt: {
type: ['string'],
format: 'date-time'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":""}')
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date-time format')
})

t.test('format::date', t => {
t.plan(2)

const prop = {
updatedAt: {
type: ['string'],
format: 'date'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":""}')
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date format')
})

t.test('format::time', t => {
t.plan(2)

const prop = {
updatedAt: {
type: ['string'],
format: 'time'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":""}')
t.notOk(validate(JSON.parse(output)), 'an empty string is not a time format')
})
})

t.test('type::array::nullable', t => {
t.plan(3)

t.test('format::date-time', t => {
t.plan(2)

const prop = {
updatedAt: {
type: ['string', 'null'],
format: 'date-time'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":null}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

t.test('format::date', t => {
t.plan(2)

const prop = {
updatedAt: {
type: ['string', 'null'],
format: 'date'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":null}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

t.test('format::time', t => {
t.plan(2)

const prop = {
updatedAt: {
type: ['string', 'null'],
format: 'time'
}
}

const {
output,
validate
} = serialize(createSchema(prop), input)

t.equal(output, '{"updatedAt":null}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})
})
})