1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const assert = require('assert');
|
15 | const path = require('path');
|
16 | const {
|
17 | loader, pointer, filename, id, titles, resolve, slug, meta,
|
18 | } = require('../lib/schemaProxy');
|
19 |
|
20 | const example = {
|
21 | 'meta:license': [
|
22 | 'Copyright 2017 Adobe Systems Incorporated. All rights reserved.',
|
23 | "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
|
24 | 'you may not use this file except in compliance with the License. You may obtain a copy',
|
25 | 'of the License at http://www.apache.org/licenses/LICENSE-2.0',
|
26 | ],
|
27 | $schema: 'http://json-schema.org/draft-06/schema#',
|
28 | $id: 'https://example.com/schemas/example',
|
29 | title: 'Example',
|
30 | type: 'object',
|
31 | description:
|
32 | 'This is an example schema with examples. Too many examples? There can never be too many examples!',
|
33 | properties: {
|
34 | foo: {
|
35 | type: 'string',
|
36 | description: 'A simple string.',
|
37 | examples: ['bar'],
|
38 | version: '1.0.0',
|
39 | testProperty: 'test',
|
40 | },
|
41 | bar: {
|
42 | type: 'string',
|
43 | description: 'A simple string.',
|
44 | examples: ['bar', 'baz'],
|
45 | version: '1.0.0',
|
46 | testProperty: 'test',
|
47 | },
|
48 | zip: {
|
49 | type: 'object',
|
50 | title: 'An object',
|
51 | },
|
52 | zup: {
|
53 | type: 'object',
|
54 | title: 'An object',
|
55 | },
|
56 | baz: {
|
57 | anyOf: [
|
58 | { $ref: '#/properties/foo' },
|
59 | { $ref: '#/properties/bar' },
|
60 | ],
|
61 | },
|
62 | },
|
63 | };
|
64 |
|
65 | const referencing = {
|
66 | $schema: 'http://json-schema.org/draft-06/schema#',
|
67 | $id: 'https://example.com/schemas/referencing',
|
68 | title: 'Referencing',
|
69 | properties: {
|
70 | $ref: 'https://example.com/schemas/example#/properties',
|
71 | zap: {
|
72 | type: 'boolean',
|
73 | },
|
74 | },
|
75 | };
|
76 |
|
77 | describe('Testing Schema Proxy', () => {
|
78 | it('Schema Proxy creates a JSON schema', () => {
|
79 | const proxied = loader()(example, 'example.schema.json');
|
80 |
|
81 | assert.equal(proxied.title, 'Example');
|
82 | assert.equal(proxied.properties.foo.type, 'string');
|
83 | });
|
84 |
|
85 | it('Schema Proxy loads multiple JSON schemas', () => {
|
86 | const myloader = loader();
|
87 | const proxied1 = myloader(example, 'example.schema.json');
|
88 | const proxied2 = myloader(referencing, 'referencing.schema.json');
|
89 |
|
90 | assert.equal(proxied1.title, 'Example');
|
91 | assert.equal(proxied2.$id, 'https://example.com/schemas/referencing');
|
92 | });
|
93 |
|
94 | it('Schema Proxy creates a JSON schema with Pointers', () => {
|
95 | const proxied = loader()(example, 'example.schema.json');
|
96 |
|
97 | assert.equal(proxied[pointer], '');
|
98 | assert.equal(proxied.properties[pointer], '/properties');
|
99 | assert.equal(proxied.properties.foo[pointer], '/properties/foo');
|
100 | assert.equal(proxied['meta:license'][pointer], '/meta:license');
|
101 | assert.equal(proxied.properties.baz.anyOf[0][pointer], '/properties/baz/anyOf/0');
|
102 | });
|
103 |
|
104 | it('Schema Proxy creates a JSON schema with ID References', () => {
|
105 | const proxied = loader()(example, 'example.schema.json');
|
106 |
|
107 | assert.equal(proxied[id], 'https://example.com/schemas/example');
|
108 | assert.equal(proxied.properties[pointer], '/properties');
|
109 |
|
110 | assert.equal(proxied.properties[id], 'https://example.com/schemas/example');
|
111 | assert.equal(proxied.properties[pointer], '/properties');
|
112 |
|
113 | assert.equal(proxied.properties.foo[id], 'https://example.com/schemas/example');
|
114 | assert.equal(proxied['meta:license'][id], 'https://example.com/schemas/example');
|
115 | assert.equal(proxied.properties.baz.anyOf[0][id], 'https://example.com/schemas/example');
|
116 | });
|
117 |
|
118 | it('Schema Proxy creates a JSON schema with Filename References', () => {
|
119 | const proxied = loader()(example, 'example.schema.json');
|
120 |
|
121 | assert.equal(proxied[filename], 'example.schema.json');
|
122 | assert.equal(proxied.properties[filename], 'example.schema.json');
|
123 | assert.equal(proxied.properties.foo[filename], 'example.schema.json');
|
124 | assert.equal(proxied['meta:license'][filename], 'example.schema.json');
|
125 | assert.equal(proxied.properties.baz.anyOf[0][filename], 'example.schema.json');
|
126 | });
|
127 |
|
128 | it('Schema Proxy creates a JSON schema with title References', () => {
|
129 | const proxied = loader()(example, 'example.schema.json');
|
130 |
|
131 | assert.deepStrictEqual(proxied[titles], ['Example']);
|
132 | assert.deepStrictEqual(proxied.properties.zip[titles], ['Example', undefined, 'An object']);
|
133 | });
|
134 |
|
135 | it('Schema proxy resolves JSON Pointers', () => {
|
136 | const myloader = loader();
|
137 | const proxied1 = myloader(example, 'example.schema.json');
|
138 |
|
139 | assert.deepStrictEqual(proxied1.properties, proxied1[resolve]('/properties'));
|
140 | assert.deepStrictEqual(proxied1.properties.foo, proxied1[resolve]('/properties/foo'));
|
141 | assert.deepStrictEqual(proxied1.properties.foo, proxied1.properties[resolve]('/foo'));
|
142 | });
|
143 |
|
144 | it('Schema proxy resolves Reference Pointers', () => {
|
145 | const myloader = loader();
|
146 | myloader(example, 'example.schema.json');
|
147 | const proxied2 = myloader(referencing, 'referencing.schema.json');
|
148 |
|
149 | assert.deepStrictEqual(new Set(Object.keys(proxied2.properties)), new Set([
|
150 | '$ref', 'zap',
|
151 | 'foo', 'bar', 'zip', 'zup', 'baz',
|
152 | ]));
|
153 | });
|
154 |
|
155 | it('Schema proxy generates unique names', () => {
|
156 | const myloader = loader();
|
157 | const proxied1 = myloader(example, 'example.schema.json');
|
158 | const proxied2 = myloader(referencing, 'referencing.schema.json');
|
159 | const proxied3 = myloader({
|
160 | title: 'Referencing',
|
161 | }, 'anotherreference.schema.json');
|
162 |
|
163 | assert.equal(proxied1[slug], 'example');
|
164 |
|
165 | assert.equal(proxied1.properties.zip[slug], 'example-properties-an-object');
|
166 | assert.equal(proxied1.properties.zup[slug], 'example-properties-an-object-1');
|
167 |
|
168 | assert.equal(proxied2[slug], 'referencing');
|
169 | assert.equal(proxied2[slug], 'referencing');
|
170 | assert.equal(proxied3[slug], 'anotherreference');
|
171 | });
|
172 |
|
173 | it('Schema proxy loads actual schemas with meta information', () => {
|
174 | const myloader = loader();
|
175 |
|
176 | const examplefile = path.resolve(__dirname, '..', 'examples', 'schemas', 'definitions.schema.json');
|
177 |
|
178 | const exampleschema = myloader(require(examplefile), examplefile);
|
179 |
|
180 | assert.equal(exampleschema[meta].shortdescription, 'This is an example of using a definitions object within a schema');
|
181 | });
|
182 |
|
183 | it('Schema proxy loads complex schemas with meta information', () => {
|
184 | const myloader = loader();
|
185 |
|
186 | const files = [
|
187 | 'abstract.schema.json',
|
188 | 'simple.schema.json',
|
189 | 'complex.schema.json',
|
190 | 'custom.schema.json',
|
191 | 'examples.schema.json',
|
192 | 'extending.schema.json',
|
193 | ];
|
194 |
|
195 | const schemas = files.map((file) => {
|
196 | const fname = path.resolve(__dirname, '..', 'examples', 'schemas', file);
|
197 |
|
198 | return myloader(require(fname), fname);
|
199 | });
|
200 |
|
201 | assert.equal(schemas[0][slug], 'abstract');
|
202 |
|
203 | assert.deepEqual(schemas[2].title, 'Complex References');
|
204 | assert.equal(schemas[2].properties.refnamed.title, 'Simple');
|
205 | assert.equal(schemas[2].properties.refrefed.title, 'Simple');
|
206 | assert.equal(schemas[2].properties.reflocalfile.title, 'Simple');
|
207 | assert.equal(schemas[2].properties.reflocalfiledef.properties.third.properties.baz.title, 'BAAAZ!');
|
208 |
|
209 | assert.equal(schemas[3].allOf[2].properties.bar.type, 'string');
|
210 |
|
211 | assert.equal(schemas[4].examples.length, 2);
|
212 | });
|
213 |
|
214 | it('Schema proxy loads complex schemas with meta information', () => {
|
215 | const myloader = loader();
|
216 |
|
217 | const files = [
|
218 | 'deadref.schema.json',
|
219 | ];
|
220 |
|
221 | const schemas = files.map((file) => {
|
222 | const fname = path.resolve(__dirname, 'fixtures', 'deadref', file);
|
223 |
|
224 | return myloader(require(fname), fname);
|
225 | });
|
226 |
|
227 | assert.equal(schemas[0][slug], 'deadref');
|
228 | assert.deepEqual(schemas[0].properties.foo, { $ref: 'http://unknown-ref' });
|
229 | });
|
230 | });
|