|
| 1 | +/* |
| 2 | + * Copyright 2019 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
1 | 12 | /* eslint-env mocha */
|
2 | 13 |
|
3 |
| -const assert = require("assert"); |
4 |
| -const { loader, pointer, filename, id, titles } = require("../lib/schemaProxy"); |
| 14 | +const assert = require('assert'); |
| 15 | +const { |
| 16 | + loader, pointer, filename, id, titles, resolve, |
| 17 | +} = require('../lib/schemaProxy'); |
5 | 18 |
|
6 | 19 | const example = {
|
7 |
| - "meta:license": [ |
8 |
| - "Copyright 2017 Adobe Systems Incorporated. All rights reserved.", |
| 20 | + 'meta:license': [ |
| 21 | + 'Copyright 2017 Adobe Systems Incorporated. All rights reserved.', |
9 | 22 | "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
|
10 |
| - "you may not use this file except in compliance with the License. You may obtain a copy", |
11 |
| - "of the License at http://www.apache.org/licenses/LICENSE-2.0" |
| 23 | + 'you may not use this file except in compliance with the License. You may obtain a copy', |
| 24 | + 'of the License at http://www.apache.org/licenses/LICENSE-2.0', |
12 | 25 | ],
|
13 |
| - $schema: "http://json-schema.org/draft-06/schema#", |
14 |
| - $id: "https://example.com/schemas/example", |
15 |
| - title: "Example", |
16 |
| - type: "object", |
| 26 | + $schema: 'http://json-schema.org/draft-06/schema#', |
| 27 | + $id: 'https://example.com/schemas/example', |
| 28 | + title: 'Example', |
| 29 | + type: 'object', |
17 | 30 | description:
|
18 |
| - "This is an example schema with examples. Too many examples? There can never be too many examples!", |
| 31 | + 'This is an example schema with examples. Too many examples? There can never be too many examples!', |
19 | 32 | properties: {
|
20 | 33 | foo: {
|
21 |
| - type: "string", |
22 |
| - description: "A simple string.", |
23 |
| - examples: ["bar"], |
24 |
| - version: "1.0.0", |
25 |
| - testProperty: "test" |
| 34 | + type: 'string', |
| 35 | + description: 'A simple string.', |
| 36 | + examples: ['bar'], |
| 37 | + version: '1.0.0', |
| 38 | + testProperty: 'test', |
26 | 39 | },
|
27 | 40 | bar: {
|
28 |
| - type: "string", |
29 |
| - description: "A simple string.", |
30 |
| - examples: ["bar", "baz"], |
31 |
| - version: "1.0.0", |
32 |
| - testProperty: "test" |
| 41 | + type: 'string', |
| 42 | + description: 'A simple string.', |
| 43 | + examples: ['bar', 'baz'], |
| 44 | + version: '1.0.0', |
| 45 | + testProperty: 'test', |
33 | 46 | },
|
34 | 47 | zip: {
|
35 |
| - type: "object", |
36 |
| - title: 'An object' |
| 48 | + type: 'object', |
| 49 | + title: 'An object', |
37 | 50 | },
|
38 | 51 | baz: {
|
39 |
| - "anyOf": [ |
40 |
| - { "$ref": "#/properties/foo"}, |
41 |
| - { "$ref": "#/properties/bar"} |
42 |
| - ] |
43 |
| - } |
44 |
| - } |
| 52 | + anyOf: [ |
| 53 | + { $ref: '#/properties/foo' }, |
| 54 | + { $ref: '#/properties/bar' }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + }, |
45 | 58 | };
|
46 | 59 |
|
47 | 60 | const referencing = {
|
48 |
| - $schema: "http://json-schema.org/draft-06/schema#", |
49 |
| - $id: "https://example.com/schemas/referencing", |
| 61 | + $schema: 'http://json-schema.org/draft-06/schema#', |
| 62 | + $id: 'https://example.com/schemas/referencing', |
50 | 63 | properties: {
|
51 |
| - $ref: 'https://example.com/schemas/referencing#/properties' |
52 |
| - } |
53 |
| -} |
54 |
| - |
55 |
| -describe("Testing Schema Proxy", () => { |
| 64 | + $ref: 'https://example.com/schemas/example#/properties', |
| 65 | + zap: { |
| 66 | + type: 'boolean', |
| 67 | + }, |
| 68 | + }, |
| 69 | +}; |
56 | 70 |
|
57 |
| - it("Schema Proxy creates a JSON schema", () => { |
| 71 | +describe('Testing Schema Proxy', () => { |
| 72 | + it('Schema Proxy creates a JSON schema', () => { |
58 | 73 | const proxied = loader()(example, 'example.schema.json');
|
59 | 74 |
|
60 |
| - assert.equal(proxied.title, "Example"); |
61 |
| - assert.equal(proxied.properties.foo.type, "string"); |
| 75 | + assert.equal(proxied.title, 'Example'); |
| 76 | + assert.equal(proxied.properties.foo.type, 'string'); |
62 | 77 | });
|
63 | 78 |
|
64 |
| - it("Schema Proxy loads multiple JSON schemas", () => { |
| 79 | + it('Schema Proxy loads multiple JSON schemas', () => { |
65 | 80 | const myloader = loader();
|
66 | 81 | const proxied1 = myloader(example, 'example.schema.json');
|
67 | 82 | const proxied2 = myloader(referencing, 'referencing.schema.json');
|
68 | 83 |
|
69 |
| - assert.equal(proxied1.title, "Example"); |
70 |
| - assert.equal(proxied2.$id, "https://example.com/schemas/referencing"); |
71 |
| - |
| 84 | + assert.equal(proxied1.title, 'Example'); |
| 85 | + assert.equal(proxied2.$id, 'https://example.com/schemas/referencing'); |
72 | 86 | });
|
73 | 87 |
|
74 |
| - it("Schema Proxy creates a JSON schema with Pointers", () => { |
| 88 | + it('Schema Proxy creates a JSON schema with Pointers', () => { |
75 | 89 | const proxied = loader()(example, 'example.schema.json');
|
76 | 90 |
|
77 |
| - assert.equal(proxied[pointer], ""); |
78 |
| - assert.equal(proxied.properties[pointer], "/properties"); |
79 |
| - assert.equal(proxied.properties.foo[pointer], "/properties/foo"); |
80 |
| - assert.equal(proxied['meta:license'][pointer], "/meta:license"); |
81 |
| - assert.equal(proxied.properties.baz.anyOf[0][pointer], "/properties/baz/anyOf/0"); |
| 91 | + assert.equal(proxied[pointer], ''); |
| 92 | + assert.equal(proxied.properties[pointer], '/properties'); |
| 93 | + assert.equal(proxied.properties.foo[pointer], '/properties/foo'); |
| 94 | + assert.equal(proxied['meta:license'][pointer], '/meta:license'); |
| 95 | + assert.equal(proxied.properties.baz.anyOf[0][pointer], '/properties/baz/anyOf/0'); |
82 | 96 | });
|
83 | 97 |
|
84 |
| - it("Schema Proxy creates a JSON schema with ID References", () => { |
| 98 | + it('Schema Proxy creates a JSON schema with ID References', () => { |
85 | 99 | const proxied = loader()(example, 'example.schema.json');
|
86 | 100 |
|
87 |
| - assert.equal(proxied[id], "https://example.com/schemas/example"); |
88 |
| - assert.equal(proxied.properties[pointer], "/properties"); |
| 101 | + assert.equal(proxied[id], 'https://example.com/schemas/example'); |
| 102 | + assert.equal(proxied.properties[pointer], '/properties'); |
89 | 103 |
|
90 |
| - assert.equal(proxied.properties[id], "https://example.com/schemas/example"); |
91 |
| - assert.equal(proxied.properties[pointer], "/properties"); |
| 104 | + assert.equal(proxied.properties[id], 'https://example.com/schemas/example'); |
| 105 | + assert.equal(proxied.properties[pointer], '/properties'); |
92 | 106 |
|
93 |
| - assert.equal(proxied.properties.foo[id], "https://example.com/schemas/example"); |
94 |
| - assert.equal(proxied['meta:license'][id], "https://example.com/schemas/example"); |
95 |
| - assert.equal(proxied.properties.baz.anyOf[0][id], "https://example.com/schemas/example"); |
| 107 | + assert.equal(proxied.properties.foo[id], 'https://example.com/schemas/example'); |
| 108 | + assert.equal(proxied['meta:license'][id], 'https://example.com/schemas/example'); |
| 109 | + assert.equal(proxied.properties.baz.anyOf[0][id], 'https://example.com/schemas/example'); |
96 | 110 | });
|
97 | 111 |
|
98 |
| - it("Schema Proxy creates a JSON schema with Filename References", () => { |
| 112 | + it('Schema Proxy creates a JSON schema with Filename References', () => { |
99 | 113 | const proxied = loader()(example, 'example.schema.json');
|
100 | 114 |
|
101 |
| - assert.equal(proxied[filename], "example.schema.json"); |
102 |
| - assert.equal(proxied.properties[filename], "example.schema.json"); |
103 |
| - assert.equal(proxied.properties.foo[filename], "example.schema.json"); |
104 |
| - assert.equal(proxied['meta:license'][filename], "example.schema.json"); |
105 |
| - assert.equal(proxied.properties.baz.anyOf[0][filename], "example.schema.json"); |
| 115 | + assert.equal(proxied[filename], 'example.schema.json'); |
| 116 | + assert.equal(proxied.properties[filename], 'example.schema.json'); |
| 117 | + assert.equal(proxied.properties.foo[filename], 'example.schema.json'); |
| 118 | + assert.equal(proxied['meta:license'][filename], 'example.schema.json'); |
| 119 | + assert.equal(proxied.properties.baz.anyOf[0][filename], 'example.schema.json'); |
106 | 120 | });
|
107 | 121 |
|
108 |
| - it("Schema Proxy creates a JSON schema with title References", () => { |
| 122 | + it('Schema Proxy creates a JSON schema with title References', () => { |
109 | 123 | const proxied = loader()(example, 'example.schema.json');
|
110 | 124 |
|
111 | 125 | assert.deepStrictEqual(proxied[titles], ['Example']);
|
112 | 126 | assert.deepStrictEqual(proxied.properties.zip[titles], ['Example', undefined, 'An object']);
|
113 | 127 | });
|
114 | 128 |
|
115 |
| - |
| 129 | + it('Schema proxy resolves JSON Pointers', () => { |
| 130 | + const myloader = loader(); |
| 131 | + const proxied1 = myloader(example, 'example.schema.json'); |
| 132 | + |
| 133 | + assert.deepStrictEqual(proxied1.properties, proxied1[resolve]('/properties')); |
| 134 | + assert.deepStrictEqual(proxied1.properties.foo, proxied1[resolve]('/properties/foo')); |
| 135 | + assert.deepStrictEqual(proxied1.properties.foo, proxied1.properties[resolve]('/foo')); |
| 136 | + }); |
| 137 | + |
| 138 | + it('Schema proxy resolves Reference Pointers', () => { |
| 139 | + const myloader = loader(); |
| 140 | + myloader(example, 'example.schema.json'); |
| 141 | + const proxied2 = myloader(referencing, 'referencing.schema.json'); |
| 142 | + |
| 143 | + assert.deepStrictEqual(new Set(Object.keys(proxied2.properties)), new Set([ |
| 144 | + '$ref', 'zap', // the two properties from the original declaration |
| 145 | + 'foo', 'bar', 'zip', 'baz', // plus all the referenced properties |
| 146 | + ])); |
| 147 | + }); |
116 | 148 | });
|
0 commit comments