@@ -127,7 +127,7 @@ test('not fail on null sub-object declared nullable', (t) => {
127127 t . equal ( '{"product":null}' , stringify ( object ) )
128128} )
129129
130- test ( 'throw an error on non nullable null sub-object' , ( t ) => {
130+ test ( 'on non nullable null sub-object it should coerce to {} ' , ( t ) => {
131131 t . plan ( 1 )
132132
133133 const stringify = build ( {
@@ -148,10 +148,12 @@ test('throw an error on non nullable null sub-object', (t) => {
148148 const object = {
149149 product : null
150150 }
151- t . throws ( ( ) => { stringify ( object ) } )
151+
152+ const result = stringify ( object )
153+ t . equal ( result , JSON . stringify ( { product : { } } ) )
152154} )
153155
154- test ( 'throw an error on non nullable null object' , ( t ) => {
156+ test ( 'on non nullable null object it should coerce to {} ' , ( t ) => {
155157 t . plan ( 1 )
156158
157159 const stringify = build ( {
@@ -170,5 +172,37 @@ test('throw an error on non nullable null object', (t) => {
170172 }
171173 }
172174 } )
173- t . throws ( ( ) => { stringify ( null ) } )
175+
176+ const result = stringify ( null )
177+ t . equal ( result , '{}' )
178+ } )
179+
180+ test ( 'on non-nullable null object with required fields it should throw complaining missing required fields' , ( t ) => {
181+ t . plan ( 1 )
182+
183+ const stringify = build ( {
184+ title : 'simple object' ,
185+ nullable : false ,
186+ type : 'object' ,
187+ properties : {
188+ product : {
189+ nullable : false ,
190+ type : 'object' ,
191+ properties : {
192+ name : {
193+ type : 'string'
194+ }
195+ }
196+ }
197+ } ,
198+ required : [ 'product' ]
199+ } )
200+
201+ try {
202+ stringify ( null )
203+ t . fail ( 'stringify should throw for missing required fields' )
204+ } catch ( err ) {
205+ const message = err . message
206+ t . equal ( message , '"product" is required!' )
207+ }
174208} )
0 commit comments