Skip to content

Commit b4d8301

Browse files
authored
Merge pull request #34 from SimenB/weird-keys
Allow non alpha keys
2 parents cf349f2 + 42d752a commit b4d8301

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,18 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
317317
var laterCode = ''
318318

319319
Object.keys(schema.properties || {}).forEach((key, i, a) => {
320-
// Using obj.key !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
320+
// Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
321321
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
322322
code += `
323-
if (obj.${key} !== undefined) {
323+
if (obj['${key}'] !== undefined) {
324324
json += '${$asString(key)}:'
325325
`
326326

327327
if (schema.properties[key]['$ref']) {
328328
schema.properties[key] = refFinder(schema.properties[key]['$ref'], fullSchema, externalSchema)
329329
}
330330

331-
const result = nested(laterCode, name, '.' + key, schema.properties[key], externalSchema, fullSchema)
331+
const result = nested(laterCode, name, key, schema.properties[key], externalSchema, fullSchema)
332332

333333
code += result.code
334334
laterCode = result.laterCode
@@ -406,6 +406,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
406406
var code = ''
407407
var funcName
408408
const type = schema.type
409+
const accessor = key.indexOf('[') === 0 ? key : `['${key}']`
409410
switch (type) {
410411
case 'null':
411412
code += `
@@ -414,36 +415,36 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
414415
break
415416
case 'string':
416417
code += `
417-
json += $asString(obj${key})
418+
json += $asString(obj${accessor})
418419
`
419420
break
420421
case 'integer':
421422
code += `
422-
json += $asInteger(obj${key})
423+
json += $asInteger(obj${accessor})
423424
`
424425
break
425426
case 'number':
426427
code += `
427-
json += $asNumber(obj${key})
428+
json += $asNumber(obj${accessor})
428429
`
429430
break
430431
case 'boolean':
431432
code += `
432-
json += $asBoolean(obj${key})
433+
json += $asBoolean(obj${accessor})
433434
`
434435
break
435436
case 'object':
436437
funcName = (name + key).replace(/[-.\[\]]/g, '') // eslint-disable-line
437438
laterCode = buildObject(schema, laterCode, funcName, externalSchema, fullSchema)
438439
code += `
439-
json += ${funcName}(obj${key})
440+
json += ${funcName}(obj${accessor})
440441
`
441442
break
442443
case 'array':
443444
funcName = (name + key).replace(/[-.\[\]]/g, '') // eslint-disable-line
444445
laterCode = buildArray(schema, laterCode, funcName, externalSchema, fullSchema)
445446
code += `
446-
json += ${funcName}(obj${key})
447+
json += ${funcName}(obj${accessor})
447448
`
448449
break
449450
default:

test/basic.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ buildTest({
113113
'type': 'null'
114114
}, null)
115115

116+
buildTest({
117+
'title': 'deep object with weird keys',
118+
'type': 'object',
119+
'properties': {
120+
'@version': {
121+
'type': 'integer'
122+
}
123+
}
124+
}, {
125+
'@version': 1
126+
})
127+
116128
buildTest({
117129
'title': 'with null',
118130
'type': 'object',

0 commit comments

Comments
 (0)