Skip to content

Commit ed0298c

Browse files
committed
Added boolean type.
1 parent 1629686 commit ed0298c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function build (schema) {
88
${$asString.toString()}
99
${$asNumber.toString()}
1010
${$asNull.toString()}
11+
${$asBoolean.toString()}
1112
`
1213
var main
1314

@@ -23,6 +24,9 @@ function build (schema) {
2324
case 'number':
2425
main = $asNumber.name
2526
break
27+
case 'boolean':
28+
main = $asBoolean.name
29+
break
2630
case 'null':
2731
main = $asNull.name
2832
break
@@ -44,7 +48,7 @@ function build (schema) {
4448
return (new Function(code))()
4549
}
4650

47-
function $asNull (i) {
51+
function $asNull () {
4852
return 'null'
4953
}
5054

@@ -57,6 +61,10 @@ function $asNumber (i) {
5761
}
5862
}
5963

64+
function $asBoolean (bool) {
65+
return bool && 'true' || 'false'
66+
}
67+
6068
function $asString (str) {
6169
if (str instanceof Date) {
6270
str = str.toISOString()
@@ -168,6 +176,11 @@ function nested (laterCode, name, key, schema) {
168176
json += $asNumber(obj${key})
169177
`
170178
break
179+
case 'boolean':
180+
code += `
181+
json += $asBoolean(obj${key})
182+
`
183+
break
171184
case 'object':
172185
funcName = (name + key).replace(/[-.\[\]]/g, '')
173186
laterCode = buildObject(schema, laterCode, funcName)

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ buildTest({
5050
type: 'string'
5151
}, 'hello world')
5252

53+
buildTest({
54+
title: 'boolean true',
55+
type: 'boolean'
56+
}, true)
57+
58+
buildTest({
59+
title: 'boolean false',
60+
type: 'boolean'
61+
}, false)
62+
5363
buildTest({
5464
title: 'an integer',
5565
type: 'integer'
@@ -195,3 +205,15 @@ test('render a a date in a string as JSON', (t) => {
195205
t.equal(output, JSON.stringify(toStringify))
196206
t.ok(validate(JSON.parse(output)), 'valid schema')
197207
})
208+
209+
buildTest({
210+
'title': 'object with boolean',
211+
'type': 'object',
212+
'properties': {
213+
'readonly': {
214+
'type': 'boolean'
215+
}
216+
}
217+
}, {
218+
readonly: true
219+
})

0 commit comments

Comments
 (0)