Skip to content

Commit ebd32f8

Browse files
chrisguttandinmcollina
authored andcommitted
fix "isLong is not defined" error (#73)
* extract checks for isLong * install proxyquire as dev dependency * add integer tests without long * fix rendering of additional non long integers
1 parent 8124cfe commit ebd32f8

File tree

3 files changed

+122
-11
lines changed

3 files changed

+122
-11
lines changed

index.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,23 @@ function additionalProperty (schema, externalSchema, fullSchema) {
294294
`
295295
} else if (type === 'integer') {
296296
code += `
297-
${addComma}
298297
var t = Number(obj[keys[i]])
299-
if (isLong && isLong(obj[keys[i]]) || !isNaN(t)) {
300-
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
301-
}
302298
`
299+
if (isLong) {
300+
code += `
301+
if (isLong(obj[keys[i]]) || !isNaN(t)) {
302+
${addComma}
303+
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
304+
}
305+
`
306+
} else {
307+
code += `
308+
if (!isNaN(t)) {
309+
${addComma}
310+
json += $asString(keys[i]) + ':' + t
311+
}
312+
`
313+
}
303314
} else if (type === 'number') {
304315
code += `
305316
var t = Number(obj[keys[i]])
@@ -369,19 +380,33 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
369380
} else if (type === 'integer') {
370381
code += `
371382
var rendered = false
372-
if (isLong && isLong(obj['${key}'])) {
373-
${addComma}
374-
json += '${$asString(key)}:' + obj['${key}'].toString()
375-
rendered = true
376-
} else {
383+
`
384+
if (isLong) {
385+
code += `
386+
if (isLong(obj['${key}'])) {
387+
${addComma}
388+
json += '${$asString(key)}:' + obj['${key}'].toString()
389+
rendered = true
390+
} else {
391+
var t = Number(obj['${key}'])
392+
if (!isNaN(t)) {
393+
${addComma}
394+
json += '${$asString(key)}:' + t
395+
rendered = true
396+
}
397+
}
398+
`
399+
} else {
400+
code += `
377401
var t = Number(obj['${key}'])
378402
if (!isNaN(t)) {
379403
${addComma}
380404
json += '${$asString(key)}:' + t
381405
rendered = true
382406
}
383-
}
384-
407+
`
408+
}
409+
code += `
385410
if (rendered) {
386411
`
387412
} else {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"is-my-json-valid": "^2.17.1",
3030
"long": "^3.2.0",
3131
"pre-commit": "^1.2.2",
32+
"proxyquire": "^1.8.0",
3233
"standard": "^10.0.3",
3334
"tap": "^11.0.0",
3435
"uglify-es": "^3.2.2"

test/integer.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const validator = require('is-my-json-valid')
5+
const proxyquire = require('proxyquire')
6+
const build = proxyquire('..', { long: null })
7+
8+
test(`render an integer as JSON`, (t) => {
9+
t.plan(2)
10+
11+
const schema = {
12+
title: 'integer',
13+
type: 'integer'
14+
}
15+
16+
const validate = validator(schema)
17+
const stringify = build(schema)
18+
const output = stringify(1615)
19+
20+
t.equal(output, '1615')
21+
t.ok(validate(JSON.parse(output)), 'valid schema')
22+
})
23+
24+
test(`render an object with an integer as JSON`, (t) => {
25+
t.plan(2)
26+
27+
const schema = {
28+
title: 'object with integer',
29+
type: 'object',
30+
properties: {
31+
id: {
32+
type: 'integer'
33+
}
34+
}
35+
}
36+
37+
const validate = validator(schema)
38+
const stringify = build(schema)
39+
const output = stringify({
40+
id: 1615
41+
})
42+
43+
t.equal(output, '{"id":1615}')
44+
t.ok(validate(JSON.parse(output)), 'valid schema')
45+
})
46+
47+
test(`render an array with an integer as JSON`, (t) => {
48+
t.plan(2)
49+
50+
const schema = {
51+
title: 'array with integer',
52+
type: 'array',
53+
items: {
54+
type: 'integer'
55+
}
56+
}
57+
58+
const validate = validator(schema)
59+
const stringify = build(schema)
60+
const output = stringify([1615])
61+
62+
t.equal(output, '[1615]')
63+
t.ok(validate(JSON.parse(output)), 'valid schema')
64+
})
65+
66+
test(`render an object with an additionalProperty of type integer as JSON`, (t) => {
67+
t.plan(2)
68+
69+
const schema = {
70+
title: 'object with integer',
71+
type: 'object',
72+
additionalProperties: {
73+
type: 'integer'
74+
}
75+
}
76+
77+
const validate = validator(schema)
78+
const stringify = build(schema)
79+
const output = stringify({
80+
num: 1615
81+
})
82+
83+
t.equal(output, '{"num":1615}')
84+
t.ok(validate(JSON.parse(output)), 'valid schema')
85+
})

0 commit comments

Comments
 (0)