@@ -6,6 +6,7 @@ const semver = require('semver')
66const validator = require ( 'is-my-json-valid' )
77const proxyquire = require ( 'proxyquire' )
88const build = proxyquire ( '..' , { long : null } )
9+ const ROUNDING_TYPES = [ 'ceil' , 'floor' , 'round' ]
910
1011test ( 'render an integer as JSON' , ( t ) => {
1112 t . plan ( 2 )
@@ -40,6 +41,10 @@ test('render a float as an integer', (t) => {
4041 const cases = [
4142 { input : Math . PI , output : '3' } ,
4243 { input : 5.0 , output : '5' } ,
44+ { input : null , output : '0' } ,
45+ { input : 0 , output : '0' } ,
46+ { input : 0.0 , output : '0' } ,
47+ { input : 42 , output : '42' } ,
4348 { input : 1.99999 , output : '1' } ,
4449 { input : - 45.05 , output : '-45' } ,
4550 { input : 0.95 , output : '1' , rounding : 'ceil' } ,
@@ -138,7 +143,7 @@ if (semver.gt(process.versions.node, '10.3.0')) {
138143 t . end ( )
139144}
140145
141- test ( 'should round interger object parameter ' , t => {
146+ test ( 'should round integer object parameter' , t => {
142147 t . plan ( 2 )
143148
144149 const schema = { type : 'object' , properties : { magic : { type : 'integer' } } }
@@ -149,3 +154,29 @@ test('should round interger object parameter ', t => {
149154 t . equal ( output , '{"magic":5}' )
150155 t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
151156} )
157+
158+ test ( 'should not stringify a property if it does not exist' , t => {
159+ t . plan ( 2 )
160+
161+ const schema = { title : 'Example Schema' , type : 'object' , properties : { age : { type : 'integer' } } }
162+ const validate = validator ( schema )
163+ const stringify = build ( schema )
164+ const output = stringify ( { } )
165+
166+ t . equal ( output , '{}' )
167+ t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
168+ } )
169+
170+ ROUNDING_TYPES . forEach ( ( rounding ) => {
171+ test ( `should not stringify a property if it does not exist (rounding: ${ rounding } )` , t => {
172+ t . plan ( 2 )
173+
174+ const schema = { type : 'object' , properties : { magic : { type : 'integer' } } }
175+ const validate = validator ( schema )
176+ const stringify = build ( schema , { rounding } )
177+ const output = stringify ( { } )
178+
179+ t . equal ( output , '{}' )
180+ t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
181+ } )
182+ } )
0 commit comments