Skip to content

Commit eda6a31

Browse files
committed
Fix requested changes
1 parent 7e80246 commit eda6a31

File tree

2 files changed

+72
-33
lines changed

2 files changed

+72
-33
lines changed

index.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function build (schema) {
1212
${$asNull.toString()}
1313
${$asBoolean.toString()}
1414
${$asRegExp.toString()}
15-
${$coerce.toString()}
1615
`
1716
var main
1817

@@ -59,7 +58,7 @@ function $asNumber (i) {
5958
if (isNaN(num)) {
6059
return 'null'
6160
} else {
62-
return '' + i
61+
return '' + num
6362
}
6463
}
6564

@@ -132,22 +131,6 @@ function $asRegExp (reg) {
132131
return '"' + reg + '"'
133132
}
134133

135-
function $coerce (value, type) {
136-
if (type === 'string') {
137-
return String(value)
138-
} else if (type === 'number') {
139-
return Number(value)
140-
} else if (type === 'boolean') {
141-
return Boolean(value)
142-
} else if (type === 'object') {
143-
return {}
144-
} else if (type === 'array') {
145-
return []
146-
} else {
147-
throw new Error('Cannot coerce ' + value + ' to ' + type)
148-
}
149-
}
150-
151134
function addPatternProperties (pp) {
152135
let code = `
153136
var keys = Object.keys(obj)
@@ -168,9 +151,25 @@ function addPatternProperties (pp) {
168151
code += `
169152
json += $asString(keys[i]) + ':[],'
170153
`
154+
} else if (type === 'null') {
155+
code += `
156+
json += $asString(keys[i]) +':null,'
157+
`
158+
} else if (type === 'string') {
159+
code += `
160+
json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
161+
`
162+
} else if (type === 'number' || type === 'integer') {
163+
code += `
164+
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
165+
`
166+
} else if (type === 'boolean') {
167+
code += `
168+
json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]]) + ','
169+
`
171170
} else {
172171
code += `
173-
json += $asString(keys[i]) + ':' + $as${type[0].toUpperCase() + type.slice(1)}($coerce(obj[keys[i]], '${type}')) + ','
172+
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${type}')
174173
`
175174
}
176175
code += `

test.js

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ test('missing values', (t) => {
318318
})
319319

320320
test('patternProperties', (t) => {
321-
t.plan(7)
322-
let stringify = build({
321+
t.plan(1)
322+
const stringify = build({
323323
title: 'patternProperties',
324324
type: 'object',
325325
properties: {
@@ -336,8 +336,11 @@ test('patternProperties', (t) => {
336336

337337
let obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: {a: true}, notMe: false }
338338
t.equal('{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}', stringify(obj))
339+
})
339340

340-
stringify = build({
341+
test('patternProperties should not change properties', (t) => {
342+
t.plan(1)
343+
const stringify = build({
341344
title: 'patternProperties should not change properties',
342345
type: 'object',
343346
properties: {
@@ -352,10 +355,13 @@ test('patternProperties', (t) => {
352355
}
353356
})
354357

355-
obj = { foo: '42', ofoo: 42 }
358+
const obj = { foo: '42', ofoo: 42 }
356359
t.equal('{"ofoo":42,"foo":"42"}', stringify(obj))
360+
})
357361

358-
stringify = build({
362+
test('patternProperties - string coerce', (t) => {
363+
t.plan(1)
364+
const stringify = build({
359365
title: 'check string coerce',
360366
type: 'object',
361367
properties: {},
@@ -366,10 +372,13 @@ test('patternProperties', (t) => {
366372
}
367373
})
368374

369-
obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } }
375+
const obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } }
370376
t.equal('{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}', stringify(obj))
377+
})
371378

372-
stringify = build({
379+
test('patternProperties - number coerce', (t) => {
380+
t.plan(1)
381+
const stringify = build({
373382
title: 'check number coerce',
374383
type: 'object',
375384
properties: {},
@@ -380,10 +389,13 @@ test('patternProperties', (t) => {
380389
}
381390
})
382391

383-
obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
392+
const obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
384393
t.equal('{"foo":1,"ofoo":42,"xfoo":null,"arrfoo":null,"objfoo":null}', stringify(obj))
394+
})
385395

386-
stringify = build({
396+
test('patternProperties - boolean coerce', (t) => {
397+
t.plan(1)
398+
const stringify = build({
387399
title: 'check boolean coerce',
388400
type: 'object',
389401
properties: {},
@@ -394,10 +406,13 @@ test('patternProperties', (t) => {
394406
}
395407
})
396408

397-
obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } }
409+
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } }
398410
t.equal('{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}', stringify(obj))
411+
})
399412

400-
stringify = build({
413+
test('patternProperties - object coerce', (t) => {
414+
t.plan(1)
415+
const stringify = build({
401416
title: 'check object coerce',
402417
type: 'object',
403418
properties: {},
@@ -408,10 +423,13 @@ test('patternProperties', (t) => {
408423
}
409424
})
410425

411-
obj = { foo: true, ofoo: '42', arrfoo: [1, 2], objfoo: { answer: 42 } }
426+
const obj = { foo: true, ofoo: '42', arrfoo: [1, 2], objfoo: { answer: 42 } }
412427
t.equal('{"foo":{},"ofoo":{},"arrfoo":{},"objfoo":{}}', stringify(obj))
428+
})
413429

414-
stringify = build({
430+
test('patternProperties - array coerce', (t) => {
431+
t.plan(1)
432+
const stringify = build({
415433
title: 'check array coerce',
416434
type: 'object',
417435
properties: {},
@@ -422,6 +440,28 @@ test('patternProperties', (t) => {
422440
}
423441
})
424442

425-
obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
443+
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
426444
t.equal('{"foo":[],"ofoo":[],"arrfoo":[],"objfoo":[]}', stringify(obj))
427445
})
446+
447+
test('patternProperties - throw on unknown type', (t) => {
448+
t.plan(1)
449+
const stringify = build({
450+
title: 'check array coerce',
451+
type: 'object',
452+
properties: {},
453+
patternProperties: {
454+
foo: {
455+
type: 'strangetype'
456+
}
457+
}
458+
})
459+
460+
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
461+
try {
462+
stringify(obj)
463+
t.fail()
464+
} catch (e) {
465+
t.pass()
466+
}
467+
})

0 commit comments

Comments
 (0)