-
Notifications
You must be signed in to change notification settings - Fork 22
/
json-schema-test.coffee
384 lines (295 loc) · 12.1 KB
/
json-schema-test.coffee
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
{assert} = require 'chai'
fixtures = require '../../fixtures'
{JsonSchema} = require '../../../src/validators/json-schema'
{ValidationErrors} = require '../../../src/validators/validation-errors'
sinon = require 'sinon'
shared = require '../support/amanda-to-gavel-shared'
describe 'JsonSchema', ->
validator = null
dataForTypes =
string:
real: fixtures.sampleJsonComplexKeyMissing
schema: fixtures.sampleJsonSchemaNonStrict
object:
real: JSON.parse fixtures.sampleJsonComplexKeyMissing
schema: JSON.parse fixtures.sampleJsonSchemaNonStrict
types = Object.keys dataForTypes
types.forEach (type) ->
data = dataForTypes[type]
describe 'when i create new instance of validator with "' + type + '" type arguments', ->
validator = null
beforeEach () ->
validator = new JsonSchema data['real'], data['schema']
it 'should not throw an exception', ->
fn = () ->
new JsonSchema data['real'], data['schema']
assert.doesNotThrow fn
it 'should set data to object', ->
assert.equal typeof validator.data, 'object'
it 'should parse data to object which is json parsable', ->
assert.doesNotThrow () -> JSON.stringify validator.data
it 'should parse schema to object', ->
assert.equal typeof validator.schema, 'object'
it 'should parse schema to object which is json parsable', ->
assert.doesNotThrow () -> JSON.stringify validator.schema
describe 'when I run validate()', ->
validatorReturn = null
validatorReturnAgain = null
validatorReturnAfterDataChanged = null
beforeEach ->
validatorReturn = validator.validate()
it 'should set @errors', ->
assert.isTrue validator.errors instanceof ValidationErrors
it 'should return some errors', ->
assert.notEqual validatorReturn.length , 0
describe 'and run validate again', ->
before ->
validatorReturnAgain = validator.validate()
it 'errors should not change', ->
assert.deepEqual JSON.parse(JSON.stringify(validatorReturnAgain)), JSON.parse(JSON.stringify(validatorReturn))
describe 'when i change data', ->
before ->
validator.data = JSON.parse fixtures.sampleJson
describe 'and run validate again', ->
before ->
validatorReturnAfterDataChanged = validator.validate()
it 'errors should change', ->
assert.equal validatorReturnAfterDataChanged.length, 0
describe 'when i change schema', ->
before ->
validator.schema = JSON.parse fixtures.sampleJsonSchemaNonStrict2
describe 'and run validate again', ->
validatorReturnAfterDataChanged2 = null
before ->
validatorReturnAfterDataChanged2 = validator.validate()
it 'errors should change', ->
assert.notDeepEqual JSON.parse(JSON.stringify(validatorReturnAfterDataChanged2)), JSON.parse(JSON.stringify(validatorReturnAfterDataChanged))
shared.shouldBehaveLikeAmandaToGavel(new JsonSchema '{}','{}')
describe 'when validation performed on real empty object', () ->
it 'should return some errors', ->
validator = new JsonSchema {}, JSON.parse fixtures.sampleJsonSchemaNonStrict
result = validator.validate()
assert.notEqual validator.validate().length, 0
it 'should have validateSchema method', () ->
validator = new JsonSchema {},{}
assert.isDefined validator.validateSchema
describe 'when I create new instance', () ->
it 'should call validateSchema', () ->
sinon.spy JsonSchema.prototype, 'validateSchema'
validator = new JsonSchema {}, {}
assert.isTrue validator.validateSchema.called
describe 'when invalid JSON-stringified-data are provided', ->
invalidStringifiedSchema = null
before ->
invalidStringifiedSchema = require '../../fixtures/invalid-stringified-schema'
it 'should throw an error for "data"', () ->
fn = () ->
validator = new JsonSchema invalidStringifiedSchema
assert.throw fn
it 'should throw an error for "schema"', () ->
invalidStringifiedSchema = require '../../fixtures/invalid-stringified-schema'
fn = () ->
validator = new JsonSchema {}, invalidStringifiedSchema
assert.throw fn
describe 'validate an object to check json_schema_options passed to Amanda', ->
results = null
error = null
messagesLength = null
before ->
messagesLength = Object.keys(fixtures.sampleJsonBodyTestingAmandaMessages).length
validator = new JsonSchema fixtures.sampleJsonBodyTestingAmandaMessages, fixtures.sampleJsonSchemaTestingAmandaMessages
results = validator.validate()
it "contains all those schema defined messages", ->
assert.isNull error
assert.isObject results
assert.lengthOf Object.keys(fixtures.sampleJsonSchemaTestingAmandaMessages.properties), messagesLength
assert.propertyVal results, 'length', messagesLength
assert.lengthOf results, messagesLength
describe 'validateSchema', () ->
describe 'with schema v3', () ->
describe 'when invalid schema provided', () ->
fn = null
before () ->
invalidSchema = require '../../fixtures/invalid-schema-v3'
fn = () ->
validator = new JsonSchema {}, invalidSchema
it 'should throw an error', () ->
assert.throw fn
it 'should mention schema v3 in the message', () ->
try
fn()
catch e
assert.include e.message, 'v3'
describe 'when valid v3 schema provided', () ->
fn = null
before () ->
validSchema = require '../../fixtures/valid-schema-v3'
fn = () ->
validator = new JsonSchema {}, validSchema
it 'should not throw any error', () ->
assert.doesNotThrow fn
it 'should set @jsonSchemaVersion to v3', () ->
fn()
assert.equal validator.jsonSchemaVersion, 'v3'
describe 'with schema v4', () ->
describe 'when invalid v4 schema provided', () ->
fn = null
before () ->
invalidSchema = require '../../fixtures/invalid-schema-v4'
fn = () ->
validator = new JsonSchema {}, invalidSchema
it 'should throw an error', () ->
assert.throw fn
it 'should mention v4 in the error message', () ->
try
fn()
catch e
assert.include e.message, 'v4'
describe 'when valid v4 schema provided', () ->
fn = null
before () ->
validSchema = require '../../fixtures/valid-schema-v4'
fn = () ->
validator = new JsonSchema {}, validSchema
it 'should not throw any error', () ->
assert.doesNotThrow fn
it 'should set @jsonSchemaVersion to v4', () ->
fn()
assert.equal validator.jsonSchemaVersion, 'v4'
describe 'with not identified version of schema', () ->
describe 'valid against v3 metaschema', () ->
fn = null
before () ->
validSchema = require '../../fixtures/valid-schema-v3'
delete validSchema['$schema']
fn = () ->
validator = new JsonSchema {}, validSchema
it 'should not throw any error', () ->
assert.doesNotThrow fn
it 'should set @jsonSchemaVersion to v3', () ->
fn()
assert.equal validator.jsonSchemaVersion, 'v3'
describe 'valid against v4 metaschema', () ->
fn = null
before () ->
validSchema = require '../../fixtures/valid-schema-v4'
delete validSchema['$schema']
fn = () ->
validator = new JsonSchema {}, validSchema
it 'should not throw any error', () ->
assert.doesNotThrow fn
it 'should set @jsonSchemaVersion to v4', () ->
fn()
assert.equal validator.jsonSchemaVersion, 'v4'
describe 'not valid against any metaschema', () ->
fn = null
before () ->
validSchema = require '../../fixtures/invalid-schema-v3-v4'
delete validSchema['$schema']
fn = () ->
validator = new JsonSchema {}, validSchema
it 'should throw an error', () ->
assert.throw fn
it 'should metion both v3 and v4 in the error message', () ->
try
fn()
catch e
assert.include e.message, 'v3'
assert.include e.message, 'v4'
describe 'validatePrivate()', () ->
describe 'when @jsonSchemaVersion is set to v3', () ->
validator = null
before () ->
validSchema = require '../../fixtures/valid-schema-v3'
delete validSchema['$schema']
validator = new JsonSchema {}, validSchema
sinon.stub validator, 'validateSchemaV3'
sinon.stub validator, 'validateSchemaV4'
validator.jsonSchemaVersion = 'v3'
validator.validatePrivate()
after () ->
validator.validateSchemaV3.restore()
validator.validateSchemaV4.restore()
it 'should validate using Amanda', () ->
assert.ok validator.validateSchemaV3.called
it 'should not valiate using tv4', () ->
assert.notOk validator.validateSchemaV4.called
describe 'when @jsonSchemaVersion is set to v4', () ->
validator = null
before () ->
validSchema = require '../../fixtures/valid-schema-v4'
delete validSchema['$schema']
validator = new JsonSchema {}, validSchema
sinon.stub validator, 'validateSchemaV3'
sinon.stub validator, 'validateSchemaV4'
validator.jsonSchemaVersion = 'v4'
validator.validatePrivate()
after () ->
validator.validateSchemaV3.restore()
validator.validateSchemaV4.restore()
it 'should validate using tv4', () ->
assert.ok validator.validateSchemaV4.called
it 'should not validate using amanda', () ->
assert.notOk validator.validateSchemaV3.called
describe 'when valid v4 $ref schema provided with valid data', () ->
fn = null
validator = null
before () ->
validSchema = require '../../fixtures/valid-schema-v4-with-refs'
real = JSON.parse '''
{ "foo": "bar" }
'''
fn = () ->
validator = new JsonSchema real, validSchema
validator.validatePrivate()
it 'should not return a validation error', () ->
assert.lengthOf fn(), 0
describe 'when valid v4 $ref schema provided with invalid data', () ->
fn = null
validator = null
before () ->
validSchema = require '../../fixtures/valid-schema-v4-with-refs'
real = JSON.parse '''
{ "foo": 1 }
'''
fn = () ->
validator = new JsonSchema real, validSchema
validator.validatePrivate()
it 'should return a validation error', () ->
assert.lengthOf fn(), 1
it 'should mention an invalid type error', () ->
e = fn()[0]
assert.include e.message, "Invalid type"
describe 'when invalid v4 $ref schema provided with valid data', () ->
fn = null
validator = null
before () ->
invalidSchema = require '../../fixtures/invalid-schema-v4-with-refs'
real = JSON.parse '''
{ "foo": "bar" }
'''
fn = () ->
validator = new JsonSchema real, invalidSchema
validator.validatePrivate()
it 'should return a validation error', () ->
assert.lengthOf fn(), 1
it 'should mention a missing schema error', () ->
e = fn()[0]
assert.include e.message, "Missing schema"
describe 'when @jsonSchemaVersion is null', () ->
fn = null
validator = null
beforeEach () ->
validSchema = require '../../fixtures/valid-schema-v3'
delete validSchema['$schema']
fn = () ->
validator = new JsonSchema {}, validSchema
validator.jsonSchemaVersion = null
validator.validatePrivate()
it 'shuold throw an error', () ->
assert.throw fn
it 'should let know in the error message that it should not happen', () ->
try
fn()
catch e
assert.include e.message, "JSON schema version not identified, can't validate!"