@hyperjump/json-schema 0.22.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -101,10 +101,14 @@ MediaTypePlugin to support that.
101
101
  way JSON is supported.
102
102
  * **MediaTypePlugin**: object
103
103
 
104
- * parse: (response: Response) => string -- Given a fetch Response object,
105
- parse the body of the request
106
- * matcher: (path) => boolean -- Given a filesystem path, return whether or
107
- not the file should be considered a member of this media type
104
+ * parse: (response: Response, mediaTypeParameters: object) => [SchemaObject, string]
105
+
106
+ Given a fetch Response object, parse the body of the request. Return the
107
+ parsed schema and an optional default dialectId.
108
+ * matcher: (path) => boolean
109
+
110
+ Given a filesystem path, return whether or not the file should be
111
+ considered a member of this media type.
108
112
 
109
113
  ```javascript
110
114
  const JsonSchema = require("@hyperjump/json-schema");
@@ -113,7 +117,7 @@ const YAML = require("yaml");
113
117
 
114
118
  // Add support for JSON Schemas written in YAML
115
119
  JsonSchema.addMediaTypePlugin("application/schema+yaml", {
116
- parse: async (response) => YAML.parse(await response.text()),
120
+ parse: async (response) => [YAML.parse(await response.text()), undefined],
117
121
  matcher: (path) => path.endsWith(".schema.yaml")
118
122
  });
119
123
 
@@ -1911,7 +1911,7 @@ define(['exports'], (function (exports) { 'use strict';
1911
1911
  return pointer.split("/").slice(1).map(unescape);
1912
1912
  };
1913
1913
 
1914
- const get$1 = (pointer, value = undefined) => {
1914
+ const get$2 = (pointer, value = undefined) => {
1915
1915
  const ptr = compile$P(pointer);
1916
1916
 
1917
1917
  const fn = (value) => ptr.reduce(([value, pointer], segment) => {
@@ -2030,7 +2030,7 @@ define(['exports'], (function (exports) { 'use strict';
2030
2030
 
2031
2031
  const isScalar = (value) => value === null || typeof value !== "object";
2032
2032
 
2033
- var lib$3 = { nil: nil$2, append, get: get$1, set, assign, unset, remove };
2033
+ var lib$3 = { nil: nil$2, append, get: get$2, set, assign, unset, remove };
2034
2034
 
2035
2035
  const $__value = Symbol("$__value");
2036
2036
  const $__href = Symbol("$__href");
@@ -2054,6 +2054,15 @@ define(['exports'], (function (exports) { 'use strict';
2054
2054
 
2055
2055
  const nil$1 = Object.freeze({ id: "", pointer: "", instance: undefined, value: undefined });
2056
2056
  const cons = (instance, id = "") => Object.freeze({ ...nil$1, id: resolveUrl$2(id, ""), instance, value: instance });
2057
+
2058
+ const get$1 = (url, instance = nil$1) => {
2059
+ if (!url.startsWith("#")) {
2060
+ throw Error(`No JSON document found at '${url.split("#")[0]}'`);
2061
+ }
2062
+
2063
+ return Object.freeze({ ...instance, pointer: url.substr(1) });
2064
+ };
2065
+
2057
2066
  const uri$1 = (doc) => `${doc.id}#${encodeURI(doc.pointer)}`;
2058
2067
  const value$1 = (doc) => Reference$2.isReference(doc.value) ? Reference$2.value(doc.value) : doc.value;
2059
2068
  const has$1 = (key, doc) => key in value$1(doc);
@@ -2073,6 +2082,9 @@ define(['exports'], (function (exports) { 'use strict';
2073
2082
  const map$4 = curry$8((fn, doc) => value$1(doc)
2074
2083
  .map((item, ndx, array, thisArg) => fn(step$1(ndx, doc), ndx, array, thisArg)));
2075
2084
 
2085
+ const forEach = curry$8((fn, doc) => value$1(doc)
2086
+ .forEach((item, ndx, array, thisArg) => fn(step$1(ndx, doc), ndx, array, thisArg)));
2087
+
2076
2088
  const filter$1 = curry$8((fn, doc) => value$1(doc)
2077
2089
  .map((item, ndx, array, thisArg) => step$1(ndx, doc))
2078
2090
  .filter((item, ndx, array, thisArg) => fn(item, ndx, array, thisArg)));
@@ -2088,7 +2100,10 @@ define(['exports'], (function (exports) { 'use strict';
2088
2100
 
2089
2101
  const length$1 = (doc) => value$1(doc).length;
2090
2102
 
2091
- var instance = { nil: nil$1, cons, uri: uri$1, value: value$1, has: has$1, typeOf: typeOf$1, step: step$1, entries: entries$3, keys: keys$1, map: map$4, filter: filter$1, reduce: reduce$3, every: every$1, some: some$1, length: length$1 };
2103
+ var instance = {
2104
+ nil: nil$1, cons, get: get$1, uri: uri$1, value: value$1, has: has$1, typeOf: typeOf$1, length: length$1,
2105
+ step: step$1, entries: entries$3, keys: keys$1, map: map$4, forEach, filter: filter$1, reduce: reduce$3, every: every$1, some: some$1
2106
+ };
2092
2107
 
2093
2108
  var entries$2 = async (doc) => Object.entries(await doc);
2094
2109
 
@@ -2405,11 +2420,11 @@ define(['exports'], (function (exports) { 'use strict';
2405
2420
  };
2406
2421
 
2407
2422
  const parse = (response) => {
2408
- const contentType = contentTypeParser.parse(response.headers.get("content-type")).type;
2409
- if (!(contentType in mediaTypePlugins)) {
2410
- throw Error(`${response.url} is not a schema. Found a document with media type: ${contentType}`);
2423
+ const contentType = contentTypeParser.parse(response.headers.get("content-type"));
2424
+ if (!(contentType.type in mediaTypePlugins)) {
2425
+ throw Error(`${response.url} is not a schema. Found a document with media type: ${contentType.type}`);
2411
2426
  }
2412
- return mediaTypePlugins[contentType].parse(response);
2427
+ return mediaTypePlugins[contentType.type].parse(response, contentType.parameters);
2413
2428
  };
2414
2429
 
2415
2430
  const getContentType = (path) => {
@@ -2441,6 +2456,8 @@ define(['exports'], (function (exports) { 'use strict';
2441
2456
  const dialectJsonSchemaVersion = {};
2442
2457
 
2443
2458
  const setConfig = (jsonSchemaVersion, key, value) => {
2459
+ dialectJsonSchemaVersion[jsonSchemaVersion] = jsonSchemaVersion;
2460
+
2444
2461
  if (!config[jsonSchemaVersion]) {
2445
2462
  config[jsonSchemaVersion] = {};
2446
2463
  }
@@ -2467,23 +2484,25 @@ define(['exports'], (function (exports) { 'use strict';
2467
2484
  }
2468
2485
  delete schema["$schema"];
2469
2486
 
2470
- // JSON Schema version
2487
+ // Determine JSON Schema version
2471
2488
  if (!(dialectId in dialectJsonSchemaVersion)) {
2472
2489
  if (schema?.$vocabulary?.[core201909Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core201909Id)[0]) {
2490
+ // Self describing 2019-09 meta-schema
2473
2491
  dialectJsonSchemaVersion[dialectId] = core201909Id;
2474
2492
  } else if (schema?.$vocabulary?.[core202012Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core202012Id)[0]) {
2493
+ // Self describing 2020-12 meta-schema
2475
2494
  dialectJsonSchemaVersion[dialectId] = core202012Id;
2476
- } else if (dialectId === getSchemaIdentifier(schema, externalId, dialectId)[0]) {
2477
- dialectJsonSchemaVersion[dialectId] = dialectId;
2478
- } else if (!(dialectId in schemaStore)) {
2479
- throw Error(`Couldn't determine JSON Schema version for dialect: '${dialectId}'`);
2480
2495
  } else {
2496
+ // Need to look at meta-schema to determine version
2481
2497
  const metaSchema = schemaStore[dialectId];
2482
- if (metaSchema.vocabulary[core201909Id] === true) {
2498
+ if (!metaSchema) {
2499
+ throw Error(`Couldn't determine JSON Schema version for dialect: '${dialectId}'`);
2500
+ } else if (metaSchema.vocabulary[core201909Id] === true) {
2483
2501
  dialectJsonSchemaVersion[dialectId] = core201909Id;
2484
2502
  } else if (metaSchema.vocabulary[core202012Id] === true) {
2485
2503
  dialectJsonSchemaVersion[dialectId] = core202012Id;
2486
2504
  } else {
2505
+ // Assume the jsonSchemaVersion is the meta-schema's dialectId (non-standard behavior)
2487
2506
  dialectJsonSchemaVersion[dialectId] = dialectJsonSchemaVersion[metaSchema.dialectId];
2488
2507
  }
2489
2508
  }
@@ -2622,7 +2641,15 @@ define(['exports'], (function (exports) { 'use strict';
2622
2641
  throw Error(`Failed to retrieve schema with id: ${id}`);
2623
2642
  }
2624
2643
 
2625
- add$1(await MediaTypes$1.parse(response), id);
2644
+ const [schema, defaultDialectId] = await MediaTypes$1.parse(response);
2645
+
2646
+ // Make sure the meta-schema is loaded if this isn't a known dialect
2647
+ const dialectId = resolveUrl$1(schema.$schema, "") || defaultDialectId;
2648
+ if (id !== dialectId && !(dialectId in dialectJsonSchemaVersion)) {
2649
+ await get(dialectId);
2650
+ }
2651
+
2652
+ add$1(schema, id);
2626
2653
  }
2627
2654
 
2628
2655
  const storedSchema = getStoredSchema(id);
@@ -2783,7 +2810,10 @@ define(['exports'], (function (exports) { 'use strict';
2783
2810
  let shouldMetaValidate = true;
2784
2811
 
2785
2812
  MediaTypes.addPlugin("application/schema+json", {
2786
- parse: async (response) => await response.json(),
2813
+ parse: async (response, contentTypeParameters) => [
2814
+ await response.json(),
2815
+ contentTypeParameters.schema || contentTypeParameters.profile
2816
+ ],
2787
2817
  matcher: (path) => path.endsWith(".schema.json")
2788
2818
  });
2789
2819