@@ -44,10 +44,14 @@ function build (schema, options) {
44
44
45
45
code += `
46
46
${ $asString . toString ( ) }
47
+ ${ $asStringNullable . toString ( ) }
47
48
${ $asStringSmall . toString ( ) }
48
49
${ $asNumber . toString ( ) }
50
+ ${ $asNumberNullable . toString ( ) }
51
+ ${ $asIntegerNullable . toString ( ) }
49
52
${ $asNull . toString ( ) }
50
53
${ $asBoolean . toString ( ) }
54
+ ${ $asBooleanNullable . toString ( ) }
51
55
`
52
56
53
57
// only handle longs if the module is used
@@ -76,16 +80,16 @@ function build (schema, options) {
76
80
code = buildObject ( schema , code , main , options . schema , schema )
77
81
break
78
82
case 'string' :
79
- main = $asString . name
83
+ main = schema . nullable ? $asStringNullable . name : $asString . name
80
84
break
81
85
case 'integer' :
82
- main = $asInteger . name
86
+ main = schema . nullable ? $asIntegerNullable . name : $asInteger . name
83
87
break
84
88
case 'number' :
85
- main = $asNumber . name
89
+ main = schema . nullable ? $asNumberNullable . name : $asNumber . name
86
90
break
87
91
case 'boolean' :
88
- main = $asBoolean . name
92
+ main = schema . nullable ? $asBooleanNullable . name : $asBoolean . name
89
93
break
90
94
case 'null' :
91
95
main = $asNull . name
@@ -100,7 +104,7 @@ function build (schema, options) {
100
104
101
105
code += `
102
106
;
103
- return ${ main }
107
+ return ${ main }
104
108
`
105
109
106
110
if ( options . uglify ) {
@@ -202,6 +206,10 @@ function $asInteger (i) {
202
206
}
203
207
}
204
208
209
+ function $asIntegerNullable ( i ) {
210
+ return i === null ? null : $asInteger ( i )
211
+ }
212
+
205
213
function $asNumber ( i ) {
206
214
var num = Number ( i )
207
215
if ( isNaN ( num ) ) {
@@ -211,10 +219,18 @@ function $asNumber (i) {
211
219
}
212
220
}
213
221
222
+ function $asNumberNullable ( i ) {
223
+ return i === null ? null : $asNumber ( i )
224
+ }
225
+
214
226
function $asBoolean ( bool ) {
215
227
return bool && 'true' || 'false' // eslint-disable-line
216
228
}
217
229
230
+ function $asBooleanNullable ( bool ) {
231
+ return bool === null ? null : $asBoolean ( bool )
232
+ }
233
+
218
234
function $asString ( str ) {
219
235
if ( str instanceof Date ) {
220
236
return '"' + str . toISOString ( ) + '"'
@@ -233,6 +249,10 @@ function $asString (str) {
233
249
}
234
250
}
235
251
252
+ function $asStringNullable ( str ) {
253
+ return str === null ? null : $asString ( str )
254
+ }
255
+
236
256
// magically escape strings for json
237
257
// relying on their charCodeAt
238
258
// everything below 32 needs JSON.stringify()
@@ -481,6 +501,18 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
481
501
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
482
502
483
503
var type = schema . properties [ key ] . type
504
+ var nullable = schema . properties [ key ] . nullable
505
+
506
+ if ( nullable ) {
507
+ code += `
508
+ if (obj['${ key } '] === null) {
509
+ ${ addComma }
510
+ json += '${ $asString ( key ) } :null'
511
+ var rendered = true
512
+ } else {
513
+ `
514
+ }
515
+
484
516
if ( type === 'number' ) {
485
517
code += `
486
518
var t = Number(obj['${ key } '])
@@ -549,8 +581,13 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
549
581
code += `
550
582
}
551
583
`
552
- } )
553
584
585
+ if ( nullable ) {
586
+ code += `
587
+ }
588
+ `
589
+ }
590
+ } )
554
591
return { code : code , laterCode : laterCode }
555
592
}
556
593
@@ -692,9 +729,17 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
692
729
function buildArray ( schema , code , name , externalSchema , fullSchema ) {
693
730
code += `
694
731
function ${ name } (obj) {
732
+ `
733
+ if ( schema . nullable ) {
734
+ code += `
735
+ if(obj === null) {
736
+ return '${ $asNull ( ) } ';
737
+ }
738
+ `
739
+ }
740
+ code += `
695
741
var json = '['
696
742
`
697
-
698
743
var laterCode = ''
699
744
700
745
if ( schema . items [ '$ref' ] ) {
@@ -798,6 +843,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
798
843
}
799
844
800
845
var type = schema . type
846
+ var nullable = schema . nullable === true
801
847
802
848
var accessor = key . indexOf ( '[' ) === 0 ? key : `['${ key } ']`
803
849
switch ( type ) {
@@ -807,24 +853,16 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
807
853
`
808
854
break
809
855
case 'string' :
810
- code += `
811
- json += $asString(obj${ accessor } )
812
- `
856
+ code += nullable ? `json += obj${ accessor } === null ? null : $asString(obj${ accessor } )` : `json += $asString(obj${ accessor } )`
813
857
break
814
858
case 'integer' :
815
- code += `
816
- json += $asInteger(obj${ accessor } )
817
- `
859
+ code += nullable ? `json += obj${ accessor } === null ? null : $asInteger(obj${ accessor } )` : `json += $asInteger(obj${ accessor } )`
818
860
break
819
861
case 'number' :
820
- code += `
821
- json += $asNumber(obj${ accessor } )
822
- `
862
+ code += nullable ? `json += obj${ accessor } === null ? null : $asNumber(obj${ accessor } )` : `json += $asNumber(obj${ accessor } )`
823
863
break
824
864
case 'boolean' :
825
- code += `
826
- json += $asBoolean(obj${ accessor } )
827
- `
865
+ code += nullable ? `json += obj${ accessor } === null ? null : $asBoolean(obj${ accessor } )` : `json += $asBoolean(obj${ accessor } )`
828
866
break
829
867
case 'object' :
830
868
funcName = ( name + key + subKey ) . replace ( / [ - . \[ \] ] / g, '' ) // eslint-disable-line
0 commit comments