-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathstripNull.test.js
110 lines (92 loc) · 2.82 KB
/
stripNull.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const test = require('tap').test
const build = require('..')
test('should handle null string values properly', (t) => {
t.plan(3)
const schema = {
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' }
},
required: ['firstName']
}
const stringify = build(schema, { stripNull: true })
const json1 = { firstName: 'Matteo', lastName: 'Collina' }
t.equal(stringify(json1), '{"firstName":"Matteo","lastName":"Collina"}')
const json2 = { firstName: 'Matteo', lastName: null }
t.equal(stringify(json2), '{"firstName":"Matteo"}')
const json3 = { firstName: null, lastName: 'Collina' }
t.throws(() => stringify(json3)) // throw error when required property is missing
})
test('should handle null int values properly', (t) => {
t.plan(3)
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' },
experience: { type: 'integer' }
}
}
const stringify = build(schema, { stripNull: true })
const json1 = { name: 'Matteo', age: 32, experience: 12 }
t.equal(stringify(json1), '{"name":"Matteo","age":32,"experience":12}')
const json2 = { name: 'Matteo', age: null, experience: null }
t.equal(stringify(json2), '{"name":"Matteo"}')
const json3 = { name: 'Matteo', age: 3, experience: null }
t.equal(stringify(json3), '{"name":"Matteo","age":3}')
})
test('should handle arrays properly', (t) => {
t.plan(1)
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
experience: {
type: 'array',
items: {
type: 'object',
properties: {
company: { type: 'string' },
years: { type: 'integer' }
}
}
}
}
}
const stringify = build(schema, { stripNull: true })
const json1 = {
name: 'Matteo',
experience: [
{ company: 'C1', years: 12 },
{ company: 'C2', years: null },
{ company: null, years: 6 },
{ company: null, years: null }
]
}
t.equal(
stringify(json1),
'{"name":"Matteo","experience":[{"company":"C1","years":12},{"company":"C2"},{"years":6},{}]}'
)
})
test('should handle objects and nested objects properly', (t) => {
t.plan(2)
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
address: {
type: 'object',
properties: {
city: { type: 'string' },
country: { type: 'string' }
}
}
}
}
const stringify = build(schema, { stripNull: true })
const json1 = { name: 'Matteo', address: { city: 'Mumbai', country: null } }
t.equal(stringify(json1), '{"name":"Matteo","address":{"city":"Mumbai"}}')
const json2 = { name: null, address: { city: null, country: 'India' } }
t.equal(stringify(json2), '{"address":{"country":"India"}}')
})