@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.
@@ -1913,7 +1913,7 @@ const compile$P = (pointer) => {
1913
1913
  return pointer.split("/").slice(1).map(unescape);
1914
1914
  };
1915
1915
 
1916
- const get$1 = (pointer, value = undefined) => {
1916
+ const get$2 = (pointer, value = undefined) => {
1917
1917
  const ptr = compile$P(pointer);
1918
1918
 
1919
1919
  const fn = (value) => ptr.reduce(([value, pointer], segment) => {
@@ -2032,7 +2032,7 @@ const applySegment = (value, segment, cursor = "") => {
2032
2032
 
2033
2033
  const isScalar = (value) => value === null || typeof value !== "object";
2034
2034
 
2035
- var lib$3 = { nil: nil$2, append, get: get$1, set, assign, unset, remove };
2035
+ var lib$3 = { nil: nil$2, append, get: get$2, set, assign, unset, remove };
2036
2036
 
2037
2037
  const $__value = Symbol("$__value");
2038
2038
  const $__href = Symbol("$__href");
@@ -2056,6 +2056,15 @@ const Reference$2 = reference;
2056
2056
 
2057
2057
  const nil$1 = Object.freeze({ id: "", pointer: "", instance: undefined, value: undefined });
2058
2058
  const cons = (instance, id = "") => Object.freeze({ ...nil$1, id: resolveUrl$2(id, ""), instance, value: instance });
2059
+
2060
+ const get$1 = (url, instance = nil$1) => {
2061
+ if (!url.startsWith("#")) {
2062
+ throw Error(`No JSON document found at '${url.split("#")[0]}'`);
2063
+ }
2064
+
2065
+ return Object.freeze({ ...instance, pointer: url.substr(1) });
2066
+ };
2067
+
2059
2068
  const uri$1 = (doc) => `${doc.id}#${encodeURI(doc.pointer)}`;
2060
2069
  const value$1 = (doc) => Reference$2.isReference(doc.value) ? Reference$2.value(doc.value) : doc.value;
2061
2070
  const has$1 = (key, doc) => key in value$1(doc);
@@ -2075,6 +2084,9 @@ const keys$1 = (doc) => Object.keys(value$1(doc));
2075
2084
  const map$4 = curry$8((fn, doc) => value$1(doc)
2076
2085
  .map((item, ndx, array, thisArg) => fn(step$1(ndx, doc), ndx, array, thisArg)));
2077
2086
 
2087
+ const forEach = curry$8((fn, doc) => value$1(doc)
2088
+ .forEach((item, ndx, array, thisArg) => fn(step$1(ndx, doc), ndx, array, thisArg)));
2089
+
2078
2090
  const filter$1 = curry$8((fn, doc) => value$1(doc)
2079
2091
  .map((item, ndx, array, thisArg) => step$1(ndx, doc))
2080
2092
  .filter((item, ndx, array, thisArg) => fn(item, ndx, array, thisArg)));
@@ -2090,7 +2102,10 @@ const some$1 = curry$8((fn, doc) => value$1(doc)
2090
2102
 
2091
2103
  const length$1 = (doc) => value$1(doc).length;
2092
2104
 
2093
- 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 };
2105
+ var instance = {
2106
+ nil: nil$1, cons, get: get$1, uri: uri$1, value: value$1, has: has$1, typeOf: typeOf$1, length: length$1,
2107
+ 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
2108
+ };
2094
2109
 
2095
2110
  var entries$2 = async (doc) => Object.entries(await doc);
2096
2111
 
@@ -2407,11 +2422,11 @@ const addPlugin = (contentType, plugin) => {
2407
2422
  };
2408
2423
 
2409
2424
  const parse = (response) => {
2410
- const contentType = contentTypeParser.parse(response.headers.get("content-type")).type;
2411
- if (!(contentType in mediaTypePlugins)) {
2412
- throw Error(`${response.url} is not a schema. Found a document with media type: ${contentType}`);
2425
+ const contentType = contentTypeParser.parse(response.headers.get("content-type"));
2426
+ if (!(contentType.type in mediaTypePlugins)) {
2427
+ throw Error(`${response.url} is not a schema. Found a document with media type: ${contentType.type}`);
2413
2428
  }
2414
- return mediaTypePlugins[contentType].parse(response);
2429
+ return mediaTypePlugins[contentType.type].parse(response, contentType.parameters);
2415
2430
  };
2416
2431
 
2417
2432
  const getContentType = (path) => {
@@ -2443,6 +2458,8 @@ const config = {};
2443
2458
  const dialectJsonSchemaVersion = {};
2444
2459
 
2445
2460
  const setConfig = (jsonSchemaVersion, key, value) => {
2461
+ dialectJsonSchemaVersion[jsonSchemaVersion] = jsonSchemaVersion;
2462
+
2446
2463
  if (!config[jsonSchemaVersion]) {
2447
2464
  config[jsonSchemaVersion] = {};
2448
2465
  }
@@ -2469,23 +2486,25 @@ const add$1 = (schema, url = "", defaultSchemaVersion = "") => {
2469
2486
  }
2470
2487
  delete schema["$schema"];
2471
2488
 
2472
- // JSON Schema version
2489
+ // Determine JSON Schema version
2473
2490
  if (!(dialectId in dialectJsonSchemaVersion)) {
2474
2491
  if (schema?.$vocabulary?.[core201909Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core201909Id)[0]) {
2492
+ // Self describing 2019-09 meta-schema
2475
2493
  dialectJsonSchemaVersion[dialectId] = core201909Id;
2476
2494
  } else if (schema?.$vocabulary?.[core202012Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core202012Id)[0]) {
2495
+ // Self describing 2020-12 meta-schema
2477
2496
  dialectJsonSchemaVersion[dialectId] = core202012Id;
2478
- } else if (dialectId === getSchemaIdentifier(schema, externalId, dialectId)[0]) {
2479
- dialectJsonSchemaVersion[dialectId] = dialectId;
2480
- } else if (!(dialectId in schemaStore)) {
2481
- throw Error(`Couldn't determine JSON Schema version for dialect: '${dialectId}'`);
2482
2497
  } else {
2498
+ // Need to look at meta-schema to determine version
2483
2499
  const metaSchema = schemaStore[dialectId];
2484
- if (metaSchema.vocabulary[core201909Id] === true) {
2500
+ if (!metaSchema) {
2501
+ throw Error(`Couldn't determine JSON Schema version for dialect: '${dialectId}'`);
2502
+ } else if (metaSchema.vocabulary[core201909Id] === true) {
2485
2503
  dialectJsonSchemaVersion[dialectId] = core201909Id;
2486
2504
  } else if (metaSchema.vocabulary[core202012Id] === true) {
2487
2505
  dialectJsonSchemaVersion[dialectId] = core202012Id;
2488
2506
  } else {
2507
+ // Assume the jsonSchemaVersion is the meta-schema's dialectId (non-standard behavior)
2489
2508
  dialectJsonSchemaVersion[dialectId] = dialectJsonSchemaVersion[metaSchema.dialectId];
2490
2509
  }
2491
2510
  }
@@ -2624,7 +2643,15 @@ const get = async (url, contextDoc = nil) => {
2624
2643
  throw Error(`Failed to retrieve schema with id: ${id}`);
2625
2644
  }
2626
2645
 
2627
- add$1(await MediaTypes$1.parse(response), id);
2646
+ const [schema, defaultDialectId] = await MediaTypes$1.parse(response);
2647
+
2648
+ // Make sure the meta-schema is loaded if this isn't a known dialect
2649
+ const dialectId = resolveUrl$1(schema.$schema, "") || defaultDialectId;
2650
+ if (id !== dialectId && !(dialectId in dialectJsonSchemaVersion)) {
2651
+ await get(dialectId);
2652
+ }
2653
+
2654
+ add$1(schema, id);
2628
2655
  }
2629
2656
 
2630
2657
  const storedSchema = getStoredSchema(id);
@@ -2785,7 +2812,10 @@ let metaOutputFormat = DETAILED;
2785
2812
  let shouldMetaValidate = true;
2786
2813
 
2787
2814
  MediaTypes.addPlugin("application/schema+json", {
2788
- parse: async (response) => await response.json(),
2815
+ parse: async (response, contentTypeParameters) => [
2816
+ await response.json(),
2817
+ contentTypeParameters.schema || contentTypeParameters.profile
2818
+ ],
2789
2819
  matcher: (path) => path.endsWith(".schema.json")
2790
2820
  });
2791
2821