Skip to content
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
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const Serializer = require('./lib/serializer')
const Validator = require('./lib/validator')
const Location = require('./lib/location')

const SINGLE_TICK = /'/g

let largeArraySize = 2e4
let largeArrayMechanism = 'default'

Expand Down Expand Up @@ -834,7 +836,7 @@ function buildConstSerializer (location, input) {
`
}

code += `json += '${JSON.stringify(schema.const)}'`
code += `json += '${JSON.stringify(schema.const).replace(SINGLE_TICK, "\\'")}'`

if (hasNullType) {
code += `
Expand Down
20 changes: 20 additions & 0 deletions test/const.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ test('schema with const string and no input', (t) => {
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('schema with const string that contains \'', (t) => {
t.plan(2)

const schema = {
type: 'object',
properties: {
foo: { const: "'bar'" }
}
}

const validate = validator(schema)
const stringify = build(schema)
const output = stringify({
foo: "'bar'"
})

t.equal(output, '{"foo":"\'bar\'"}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('schema with const number', (t) => {
t.plan(2)

Expand Down