1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const assert = require('assert');
|
15 | const fs = require('fs-extra');
|
16 | const path = require('path');
|
17 | const {
|
18 | loader, filename,
|
19 | } = require('../lib/schemaProxy');
|
20 |
|
21 | const traverse = require('../lib/traverseSchema');
|
22 |
|
23 | const example = {
|
24 | 'meta:license': [
|
25 | 'Copyright 2017 Adobe Systems Incorporated. All rights reserved.',
|
26 | "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
|
27 | 'you may not use this file except in compliance with the License. You may obtain a copy',
|
28 | 'of the License at http://www.apache.org/licenses/LICENSE-2.0',
|
29 | ],
|
30 | $schema: 'http://json-schema.org/draft-06/schema#',
|
31 | $id: 'https://example.com/schemas/example',
|
32 | title: 'Example',
|
33 | type: 'object',
|
34 | description:
|
35 | 'This is an example schema with examples. Too many examples? There can never be too many examples!',
|
36 | properties: {
|
37 | foo: {
|
38 | type: 'string',
|
39 | description: 'A simple string.',
|
40 | examples: ['bar'],
|
41 | version: '1.0.0',
|
42 | testProperty: 'test',
|
43 | },
|
44 | bar: {
|
45 | type: 'string',
|
46 | description: 'A simple string.',
|
47 | examples: ['bar', 'baz'],
|
48 | version: '1.0.0',
|
49 | testProperty: 'test',
|
50 | },
|
51 | zip: {
|
52 | type: 'object',
|
53 | title: 'An object',
|
54 | },
|
55 | zup: {
|
56 | type: 'object',
|
57 | title: 'An object',
|
58 | },
|
59 | baz: {
|
60 | anyOf: [
|
61 | { $ref: '#/properties/foo' },
|
62 | { $ref: '#/properties/bar' },
|
63 | ],
|
64 | },
|
65 | },
|
66 | };
|
67 |
|
68 | describe('Testing Schema Traversal', () => {
|
69 | it('Schema Traversal generates a list', () => {
|
70 | const proxied = loader()(example, 'example.schema.json');
|
71 | const schemas = traverse([proxied]);
|
72 |
|
73 | assert.equal(schemas.length, 8);
|
74 | assert.equal(schemas[7][filename], 'example.schema.json');
|
75 | });
|
76 |
|
77 | it('Cyclic Schema Traversal generates a list', async () => {
|
78 | const one = await fs.readJson(path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'));
|
79 | const two = await fs.readJson(path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'));
|
80 | const three = await fs.readJson(path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'));
|
81 |
|
82 | const myloader = loader();
|
83 | const proxiedone = myloader(one, path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'));
|
84 | const proxiedtwo = myloader(two, path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'));
|
85 | const proxiedthree = myloader(three, path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'));
|
86 |
|
87 | const schemas = traverse([proxiedone, proxiedtwo, proxiedthree]);
|
88 |
|
89 | assert.equal(schemas[0].$id, 'http://example.com/schemas/one');
|
90 | assert.equal(schemas[3].$id, 'http://example.com/schemas/three');
|
91 | assert.equal(schemas[6].$id, 'http://example.com/schemas/two');
|
92 | });
|
93 | });
|