Skip to content

Commit

Permalink
fix "isLong is not defined" error (#73)
Browse files Browse the repository at this point in the history
* extract checks for isLong

* install proxyquire as dev dependency

* add integer tests without long

* fix rendering of additional non long integers
  • Loading branch information
chrisguttandin authored and mcollina committed Feb 8, 2018
1 parent 8124cfe commit ebd32f8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 11 deletions.
47 changes: 36 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,23 @@ function additionalProperty (schema, externalSchema, fullSchema) {
`
} else if (type === 'integer') {
code += `
${addComma}
var t = Number(obj[keys[i]])
if (isLong && isLong(obj[keys[i]]) || !isNaN(t)) {
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
}
`
if (isLong) {
code += `
if (isLong(obj[keys[i]]) || !isNaN(t)) {
${addComma}
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
}
`
} else {
code += `
if (!isNaN(t)) {
${addComma}
json += $asString(keys[i]) + ':' + t
}
`
}
} else if (type === 'number') {
code += `
var t = Number(obj[keys[i]])
Expand Down Expand Up @@ -369,19 +380,33 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
} else if (type === 'integer') {
code += `
var rendered = false
if (isLong && isLong(obj['${key}'])) {
${addComma}
json += '${$asString(key)}:' + obj['${key}'].toString()
rendered = true
} else {
`
if (isLong) {
code += `
if (isLong(obj['${key}'])) {
${addComma}
json += '${$asString(key)}:' + obj['${key}'].toString()
rendered = true
} else {
var t = Number(obj['${key}'])
if (!isNaN(t)) {
${addComma}
json += '${$asString(key)}:' + t
rendered = true
}
}
`
} else {
code += `
var t = Number(obj['${key}'])
if (!isNaN(t)) {
${addComma}
json += '${$asString(key)}:' + t
rendered = true
}
}
`
}
code += `
if (rendered) {
`
} else {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"is-my-json-valid": "^2.17.1",
"long": "^3.2.0",
"pre-commit": "^1.2.2",
"proxyquire": "^1.8.0",
"standard": "^10.0.3",
"tap": "^11.0.0",
"uglify-es": "^3.2.2"
Expand Down
85 changes: 85 additions & 0 deletions test/integer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'

const test = require('tap').test
const validator = require('is-my-json-valid')
const proxyquire = require('proxyquire')
const build = proxyquire('..', { long: null })

test(`render an integer as JSON`, (t) => {
t.plan(2)

const schema = {
title: 'integer',
type: 'integer'
}

const validate = validator(schema)
const stringify = build(schema)
const output = stringify(1615)

t.equal(output, '1615')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test(`render an object with an integer as JSON`, (t) => {
t.plan(2)

const schema = {
title: 'object with integer',
type: 'object',
properties: {
id: {
type: 'integer'
}
}
}

const validate = validator(schema)
const stringify = build(schema)
const output = stringify({
id: 1615
})

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

test(`render an array with an integer as JSON`, (t) => {
t.plan(2)

const schema = {
title: 'array with integer',
type: 'array',
items: {
type: 'integer'
}
}

const validate = validator(schema)
const stringify = build(schema)
const output = stringify([1615])

t.equal(output, '[1615]')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test(`render an object with an additionalProperty of type integer as JSON`, (t) => {
t.plan(2)

const schema = {
title: 'object with integer',
type: 'object',
additionalProperties: {
type: 'integer'
}
}

const validate = validator(schema)
const stringify = build(schema)
const output = stringify({
num: 1615
})

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

0 comments on commit ebd32f8

Please sign in to comment.