]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/test/misc.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / node_modules / har-validator / node_modules / is-my-json-valid / test / misc.js
1 var tape = require('tape')
2 var cosmic = require('./fixtures/cosmic')
3 var validator = require('../')
4 var validatorRequire = require('../require')
5
6 tape('simple', function(t) {
7   var schema = {
8     required: true,
9     type: 'object',
10     properties: {
11       hello: {type:'string', required:true}
12     }
13   }
14
15   var validate = validator(schema)
16
17   t.ok(validate({hello: 'world'}), 'should be valid')
18   t.notOk(validate(), 'should be invalid')
19   t.notOk(validate({}), 'should be invalid')
20   t.end()
21 })
22
23 tape('advanced', function(t) {
24   var validate = validator(cosmic.schema)
25
26   t.ok(validate(cosmic.valid), 'should be valid')
27   t.notOk(validate(cosmic.invalid), 'should be invalid')
28   t.end()
29 })
30
31 tape('greedy/false', function(t) {
32   var validate = validator({
33     type: 'object',
34     properties: {
35       x: {
36         type: 'number'
37       }
38     },
39     required: ['x', 'y']
40   });
41   t.notOk(validate({}), 'should be invalid')
42   t.strictEqual(validate.errors.length, 2);
43   t.strictEqual(validate.errors[0].field, 'data.x')
44   t.strictEqual(validate.errors[0].message, 'is required')
45   t.strictEqual(validate.errors[1].field, 'data.y')
46   t.strictEqual(validate.errors[1].message, 'is required')
47   t.notOk(validate({x: 'string'}), 'should be invalid')
48   t.strictEqual(validate.errors.length, 1);
49   t.strictEqual(validate.errors[0].field, 'data.y')
50   t.strictEqual(validate.errors[0].message, 'is required')
51   t.notOk(validate({x: 'string', y: 'value'}), 'should be invalid')
52   t.strictEqual(validate.errors.length, 1);
53   t.strictEqual(validate.errors[0].field, 'data.x')
54   t.strictEqual(validate.errors[0].message, 'is the wrong type')
55   t.end();
56 });
57
58 tape('greedy/true', function(t) {
59   var validate = validator({
60     type: 'object',
61     properties: {
62       x: {
63         type: 'number'
64       }
65     },
66     required: ['x', 'y']
67   }, {
68     greedy: true
69   });
70   t.notOk(validate({}), 'should be invalid')
71   t.strictEqual(validate.errors.length, 2);
72   t.strictEqual(validate.errors[0].field, 'data.x')
73   t.strictEqual(validate.errors[0].message, 'is required')
74   t.strictEqual(validate.errors[1].field, 'data.y')
75   t.strictEqual(validate.errors[1].message, 'is required')
76   t.notOk(validate({x: 'string'}), 'should be invalid')
77   t.strictEqual(validate.errors.length, 2);
78   t.strictEqual(validate.errors[0].field, 'data.y')
79   t.strictEqual(validate.errors[0].message, 'is required')
80   t.strictEqual(validate.errors[1].field, 'data.x')
81   t.strictEqual(validate.errors[1].message, 'is the wrong type')
82   t.notOk(validate({x: 'string', y: 'value'}), 'should be invalid')
83   t.strictEqual(validate.errors.length, 1);
84   t.strictEqual(validate.errors[0].field, 'data.x')
85   t.strictEqual(validate.errors[0].message, 'is the wrong type')
86   t.ok(validate({x: 1, y: 'value'}), 'should be invalid')
87   t.end();
88 });
89
90 tape('additional props', function(t) {
91   var validate = validator({
92     type: 'object',
93     additionalProperties: false
94   }, {
95     verbose: true
96   })
97
98   t.ok(validate({}))
99   t.notOk(validate({foo:'bar'}))
100   t.ok(validate.errors[0].value === 'data.foo', 'should output the property not allowed in verbose mode')
101   t.strictEqual(validate.errors[0].type, 'object', 'error object should contain the type')
102   t.end()
103 })
104
105 tape('array', function(t) {
106   var validate = validator({
107     type: 'array',
108     required: true,
109     items: {
110       type: 'string'
111     }
112   })
113
114   t.notOk(validate({}), 'wrong type')
115   t.notOk(validate(), 'is required')
116   t.ok(validate(['test']))
117   t.end()
118 })
119
120 tape('nested array', function(t) {
121   var validate = validator({
122     type: 'object',
123     properties: {
124       list: {
125         type: 'array',
126         required: true,
127         items: {
128           type: 'string'
129         }
130       }
131     }
132   })
133
134   t.notOk(validate({}), 'is required')
135   t.ok(validate({list:['test']}))
136   t.notOk(validate({list:[1]}))
137   t.end()
138 })
139
140 tape('enum', function(t) {
141   var validate = validator({
142     type: 'object',
143     properties: {
144       foo: {
145         type: 'number',
146         required: true,
147         enum: [42]
148       }
149     }
150   })
151
152   t.notOk(validate({}), 'is required')
153   t.ok(validate({foo:42}))
154   t.notOk(validate({foo:43}))
155   t.end()
156 })
157
158 tape('minimum/maximum', function(t) {
159   var validate = validator({
160     type: 'object',
161     properties: {
162       foo: {
163         type: 'number',
164         minimum: 0,
165         maximum: 0
166       }
167     }
168   })
169
170   t.notOk(validate({foo:-42}))
171   t.ok(validate({foo:0}))
172   t.notOk(validate({foo:42}))
173   t.end()
174 })
175
176 tape('exclusiveMinimum/exclusiveMaximum', function(t) {
177   var validate = validator({
178     type: 'object',
179     properties: {
180       foo: {
181         type: 'number',
182         minimum: 10,
183         maximum: 20,
184         exclusiveMinimum: true,
185         exclusiveMaximum: true
186       }
187     }
188   })
189
190   t.notOk(validate({foo:10}))
191   t.ok(validate({foo:11}))
192   t.notOk(validate({foo:20}))
193   t.ok(validate({foo:19}))
194   t.end()
195 })
196
197 tape('custom format', function(t) {
198   var validate = validator({
199     type: 'object',
200     properties: {
201       foo: {
202         type: 'string',
203         format: 'as'
204       }
205     }
206   }, {formats: {as:/^a+$/}})
207
208   t.notOk(validate({foo:''}), 'not as')
209   t.notOk(validate({foo:'b'}), 'not as')
210   t.notOk(validate({foo:'aaab'}), 'not as')
211   t.ok(validate({foo:'a'}), 'as')
212   t.ok(validate({foo:'aaaaaa'}), 'as')
213   t.end()
214 })
215
216 tape('custom format function', function(t) {
217   var validate = validator({
218     type: 'object',
219     properties: {
220       foo: {
221         type: 'string',
222         format: 'as'
223       }
224     }
225   }, {formats: {as:function(s) { return /^a+$/.test(s) } }})
226
227   t.notOk(validate({foo:''}), 'not as')
228   t.notOk(validate({foo:'b'}), 'not as')
229   t.notOk(validate({foo:'aaab'}), 'not as')
230   t.ok(validate({foo:'a'}), 'as')
231   t.ok(validate({foo:'aaaaaa'}), 'as')
232   t.end()
233 })
234
235 tape('do not mutate schema', function(t) {
236   var sch = {
237     items: [
238       {}
239     ],
240     additionalItems: {
241       type: 'integer'
242     }
243   }
244
245   var copy = JSON.parse(JSON.stringify(sch))
246
247   validator(sch)
248
249   t.same(sch, copy, 'did not mutate')
250   t.end()
251 })
252
253 tape('#toJSON()', function(t) {
254   var schema = {
255     required: true,
256     type: 'object',
257     properties: {
258       hello: {type:'string', required:true}
259     }
260   }
261
262   var validate = validator(schema)
263
264   t.deepEqual(validate.toJSON(), schema, 'should return original schema')
265   t.end()
266 })
267
268 tape('external schemas', function(t) {
269   var ext = {type: 'string'}
270   var schema = {
271     required: true,
272     $ref: '#ext'
273   }
274
275   var validate = validator(schema, {schemas: {ext:ext}})
276
277   t.ok(validate('hello string'), 'is a string')
278   t.notOk(validate(42), 'not a string')
279   t.end()
280 })
281
282 tape('external schema URIs', function(t) {
283   var ext = {type: 'string'}
284   var schema = {
285     required: true,
286     $ref: 'http://example.com/schemas/schemaURIs'
287   }
288
289   var opts = {schemas:{}};
290   opts.schemas['http://example.com/schemas/schemaURIs'] = ext;
291   var validate = validator(schema, opts)
292
293   t.ok(validate('hello string'), 'is a string')
294   t.notOk(validate(42), 'not a string')
295   t.end()
296 })
297
298 tape('top-level external schema', function(t) {
299   var defs = {
300     "string": {
301       type: "string"
302     },
303     "sex": {
304       type: "string",
305       enum: ["male", "female", "other"]
306     }
307   }
308   var schema = {
309     type: "object",
310     properties: {
311       "name": { $ref: "definitions.json#/string" },
312       "sex": { $ref: "definitions.json#/sex" }
313     },
314     required: ["name", "sex"]
315   }
316
317   var validate = validator(schema, {
318     schemas: {
319       "definitions.json": defs
320     }
321   })
322   t.ok(validate({name:"alice", sex:"female"}), 'is an object')
323   t.notOk(validate({name:"alice", sex: "bob"}), 'recognizes external schema')
324   t.notOk(validate({name:2, sex: "female"}), 'recognizes external schema')
325   t.end()
326 })
327
328 tape('nested required array decl', function(t) {
329   var schema = {
330     properties: {
331       x: {
332         type: 'object',
333         properties: {
334           y: {
335             type: 'object',
336             properties: {
337               z: {
338                 type: 'string'
339               }
340             },
341             required: ['z']
342           }
343         }
344       }
345     },
346     required: ['x']
347   }
348
349   var validate = validator(schema)
350
351   t.ok(validate({x: {}}), 'should be valid')
352   t.notOk(validate({}), 'should not be valid')
353   t.strictEqual(validate.errors[0].field, 'data.x', 'should output the missing field')
354   t.end()
355 })
356
357 tape('verbose mode', function(t) {
358   var schema = {
359     required: true,
360     type: 'object',
361     properties: {
362       hello: {
363         required: true,
364         type: 'string'
365       }
366     }
367   };
368
369   var validate = validator(schema, {verbose: true})
370
371   t.ok(validate({hello: 'string'}), 'should be valid')
372   t.notOk(validate({hello: 100}), 'should not be valid')
373   t.strictEqual(validate.errors[0].value, 100, 'error object should contain the invalid value')
374   t.strictEqual(validate.errors[0].type, 'string', 'error object should contain the type')
375   t.end()
376 })
377
378 tape('additional props in verbose mode', function(t) {
379   var schema = {
380     type: 'object',
381     required: true,
382     additionalProperties: false,
383     properties: {
384       foo: {
385         type: 'string'
386       },
387       'hello world': {
388         type: 'object',
389         required: true,
390         additionalProperties: false,
391         properties: {
392           foo: {
393             type: 'string'
394           }
395         }
396       }
397     }
398   };
399
400   var validate = validator(schema, {verbose: true})
401
402   validate({'hello world': {bar: 'string'}});
403
404   t.strictEqual(validate.errors[0].value, 'data["hello world"].bar', 'should output the path to the additional prop in the error')
405   t.end()
406 })
407
408 tape('Date.now() is an integer', function(t) {
409   var schema = {type: 'integer'}
410   var validate = validator(schema)
411
412   t.ok(validate(Date.now()), 'is integer')
413   t.end()
414 })
415
416 tape('field shows item index in arrays', function(t) {
417   var schema = {
418     type: 'array',
419     items: {
420       type: 'array',
421       items: {
422         properties: {
423           foo: {
424             type: 'string',
425             required: true
426           }
427         }
428       }
429     }
430   }
431
432   var validate = validator(schema)
433
434   validate([
435     [
436       { foo: 'test' },
437       { foo: 'test' }
438     ],
439     [
440       { foo: 'test' },
441       { baz: 'test' }
442     ]
443   ])
444
445   t.strictEqual(validate.errors[0].field, 'data.1.1.foo', 'should output the field with specific index of failing item in the error')
446   t.end()
447 })