This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sanitize-test.ts
196 lines (185 loc) · 3.83 KB
/
sanitize-test.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import test from 'ava'
import stringify from 'json-stable-stringify'
import { getDefaults } from '../src/formats'
import { JsonSchema } from '../src/objects'
import { sanitize, sanitizeBySchema } from '../src/sanitize'
import { exampleFormats, schemas } from './example-schemas'
const schemaName = 'person'
const schemaVersion = '1.0.0'
const formatDefaults = getDefaults(exampleFormats)
test('example sanitize', t => {
const object = {
name: 'joe',
age: 21,
}
const result = sanitize(schemas)(schemaName, schemaVersion)(object)
t.snapshot(stringify(result, { space: ' ' }))
})
test('sanitize with default values', t => {
t.plan(1)
const object = {
name: 'joe',
age: 21,
}
const result = sanitize(schemas, formatDefaults)(schemaName, schemaVersion)(
object,
)
t.deepEqual(result, {
name: 'Buddy',
age: 21,
})
})
test('sanitize empty object using schema', t => {
const schema: JsonSchema = {
title: 'TestSchema',
type: 'object',
additionalProperties: false,
properties: {
createdAt: {
type: 'string',
format: 'date-time',
},
name: {
type: 'string',
},
hook: {
type: 'string',
format: 'hookId',
},
ids: {
type: 'array',
items: {
type: 'string',
format: 'uuid',
},
},
},
}
const o = {}
const result = sanitizeBySchema(schema, formatDefaults)(o)
t.deepEqual(result, {})
})
test('sanitize string array', t => {
t.plan(1)
const schema: JsonSchema = {
title: 'TestSchema',
type: 'object',
additionalProperties: false,
properties: {
names: {
type: 'array',
items: {
type: 'string',
format: 'name',
},
},
},
}
const o = {
names: ['Joe', 'Mary'],
}
const result = sanitizeBySchema(schema, formatDefaults)(o)
t.deepEqual(
result,
{ names: ['Buddy', 'Buddy'] },
'both names were replaced with default value for the format',
)
})
test('sanitize array', t => {
const schema: JsonSchema = {
title: 'TestSchema',
type: 'object',
additionalProperties: false,
properties: {
names: {
type: 'array',
items: {
// requires "title" in order to be considered a schema
title: 'Name',
type: 'object',
properties: {
name: {
type: 'string',
format: 'name',
},
},
},
},
},
}
const o = {
names: [
{
name: 'Joe',
},
{
name: 'Mary',
},
],
}
const result = sanitizeBySchema(schema, formatDefaults)(o)
t.deepEqual(
result,
{
names: [
{
name: 'Buddy',
},
{
name: 'Buddy',
},
],
},
'name in each object is sanitized',
)
})
test('sanitize array that can be null', t => {
const schema: JsonSchema = {
title: 'TestSchema',
type: 'object',
additionalProperties: false,
properties: {
names: {
// notice that names can be "null"
// https://github.com/cypress-io/schema-tools/issues/53
type: ['array', 'null'],
items: {
// requires "title" in order to be considered a schema
title: 'Name',
type: 'object',
properties: {
name: {
type: 'string',
format: 'name',
},
},
},
},
},
}
const o = {
names: [
{
name: 'Joe',
},
{
name: 'Mary',
},
],
}
const result = sanitizeBySchema(schema, formatDefaults)(o)
t.deepEqual(
result,
{
names: [
{
name: 'Buddy',
},
{
name: 'Buddy',
},
],
},
'name in each object is sanitized',
)
})