Skip to content

Commit 7d676d0

Browse files
committed
Added null type.
1 parent 395923d commit 7d676d0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function build (schema) {
77
88
${$asString.toString()}
99
${$asNumber.toString()}
10+
${$asNull.toString()}
1011
`
1112
var main
1213

@@ -22,6 +23,9 @@ function build (schema) {
2223
case 'number':
2324
main = $asNumber.name
2425
break
26+
case 'null':
27+
main = $asNull.name
28+
break
2529
default:
2630
throw new Error(`${schema.type} unsupported`)
2731
}
@@ -34,6 +38,10 @@ function build (schema) {
3438
return (new Function(code))()
3539
}
3640

41+
function $asNull (i) {
42+
return 'null'
43+
}
44+
3745
function $asNumber (i) {
3846
var num = Number(i)
3947
if (isNaN(num)) {
@@ -78,6 +86,11 @@ function buildObject (schema, code, name) {
7886
`
7987

8088
switch (type) {
89+
case 'null':
90+
code += `
91+
json += $asNull()
92+
`
93+
break
8194
case 'string':
8295
code += `
8396
json += $asString(obj.${key})

test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,20 @@ buildTest({
8484
something: 'else'
8585
}
8686
})
87+
88+
buildTest({
89+
'title': 'null',
90+
'type': 'null'
91+
}, null)
92+
93+
buildTest({
94+
'title': 'with null',
95+
'type': 'object',
96+
'properties': {
97+
'firstName': {
98+
'type': 'null'
99+
}
100+
}
101+
}, {
102+
firstName: null
103+
})

0 commit comments

Comments
 (0)