@@ -6,6 +6,7 @@ const semver = require('semver')
6
6
const validator = require ( 'is-my-json-valid' )
7
7
const proxyquire = require ( 'proxyquire' )
8
8
const build = proxyquire ( '..' , { long : null } )
9
+ const ROUNDING_TYPES = [ 'ceil' , 'floor' , 'round' ]
9
10
10
11
test ( 'render an integer as JSON' , ( t ) => {
11
12
t . plan ( 2 )
@@ -40,6 +41,10 @@ test('render a float as an integer', (t) => {
40
41
const cases = [
41
42
{ input : Math . PI , output : '3' } ,
42
43
{ 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' } ,
43
48
{ input : 1.99999 , output : '1' } ,
44
49
{ input : - 45.05 , output : '-45' } ,
45
50
{ input : 0.95 , output : '1' , rounding : 'ceil' } ,
@@ -138,7 +143,7 @@ if (semver.gt(process.versions.node, '10.3.0')) {
138
143
t . end ( )
139
144
}
140
145
141
- test ( 'should round interger object parameter ' , t => {
146
+ test ( 'should round integer object parameter' , t => {
142
147
t . plan ( 2 )
143
148
144
149
const schema = { type : 'object' , properties : { magic : { type : 'integer' } } }
@@ -149,3 +154,29 @@ test('should round interger object parameter ', t => {
149
154
t . equal ( output , '{"magic":5}' )
150
155
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
151
156
} )
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