@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.
@@ -1909,7 +1909,7 @@ const compile$P = (pointer) => {
1909
1909
  return pointer.split("/").slice(1).map(unescape);
1910
1910
  };
1911
1911
 
1912
- const get$1 = (pointer, value = undefined) => {
1912
+ const get$2 = (pointer, value = undefined) => {
1913
1913
  const ptr = compile$P(pointer);
1914
1914
 
1915
1915
  const fn = (value) => ptr.reduce(([value, pointer], segment) => {
@@ -2028,7 +2028,7 @@ const applySegment = (value, segment, cursor = "") => {
2028
2028
 
2029
2029
  const isScalar = (value) => value === null || typeof value !== "object";
2030
2030
 
2031
- var lib$3 = { nil: nil$2, append, get: get$1, set, assign, unset, remove };
2031
+ var lib$3 = { nil: nil$2, append, get: get$2, set, assign, unset, remove };
2032
2032
 
2033
2033
  const $__value = Symbol("$__value");
2034
2034
  const $__href = Symbol("$__href");
@@ -2052,6 +2052,15 @@ const Reference$2 = reference;
2052
2052
 
2053
2053
  const nil$1 = Object.freeze({ id: "", pointer: "", instance: undefined, value: undefined });
2054
2054
  const cons = (instance, id = "") => Object.freeze({ ...nil$1, id: resolveUrl$2(id, ""), instance, value: instance });
2055
+
2056
+ const get$1 = (url, instance = nil$1) => {
2057
+ if (!url.startsWith("#")) {
2058
+ throw Error(`No JSON document found at '${url.split("#")[0]}'`);
2059
+ }
2060
+
2061
+ return Object.freeze({ ...instance, pointer: url.substr(1) });
2062
+ };
2063
+
2055
2064
  const uri$1 = (doc) => `${doc.id}#${encodeURI(doc.pointer)}`;
2056
2065
  const value$1 = (doc) => Reference$2.isReference(doc.value) ? Reference$2.value(doc.value) : doc.value;
2057
2066
  const has$1 = (key, doc) => key in value$1(doc);
@@ -2071,6 +2080,9 @@ const keys$1 = (doc) => Object.keys(value$1(doc));
2071
2080
  const map$4 = curry$8((fn, doc) => value$1(doc)
2072
2081
  .map((item, ndx, array, thisArg) => fn(step$1(ndx, doc), ndx, array, thisArg)));
2073
2082
 
2083
+ const forEach = curry$8((fn, doc) => value$1(doc)
2084
+ .forEach((item, ndx, array, thisArg) => fn(step$1(ndx, doc), ndx, array, thisArg)));
2085
+
2074
2086
  const filter$1 = curry$8((fn, doc) => value$1(doc)
2075
2087
  .map((item, ndx, array, thisArg) => step$1(ndx, doc))
2076
2088
  .filter((item, ndx, array, thisArg) => fn(item, ndx, array, thisArg)));
@@ -2086,7 +2098,10 @@ const some$1 = curry$8((fn, doc) => value$1(doc)
2086
2098
 
2087
2099
  const length$1 = (doc) => value$1(doc).length;
2088
2100
 
2089
- 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 };
2101
+ var instance = {
2102
+ nil: nil$1, cons, get: get$1, uri: uri$1, value: value$1, has: has$1, typeOf: typeOf$1, length: length$1,
2103
+ 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
2104
+ };
2090
2105
 
2091
2106
  var entries$2 = async (doc) => Object.entries(await doc);
2092
2107
 
@@ -2403,11 +2418,11 @@ const addPlugin = (contentType, plugin) => {
2403
2418
  };
2404
2419
 
2405
2420
  const parse = (response) => {
2406
- const contentType = contentTypeParser.parse(response.headers.get("content-type")).type;
2407
- if (!(contentType in mediaTypePlugins)) {
2408
- throw Error(`${response.url} is not a schema. Found a document with media type: ${contentType}`);
2421
+ const contentType = contentTypeParser.parse(response.headers.get("content-type"));
2422
+ if (!(contentType.type in mediaTypePlugins)) {
2423
+ throw Error(`${response.url} is not a schema. Found a document with media type: ${contentType.type}`);
2409
2424
  }
2410
- return mediaTypePlugins[contentType].parse(response);
2425
+ return mediaTypePlugins[contentType.type].parse(response, contentType.parameters);
2411
2426
  };
2412
2427
 
2413
2428
  const getContentType = (path) => {
@@ -2439,6 +2454,8 @@ const config = {};
2439
2454
  const dialectJsonSchemaVersion = {};
2440
2455
 
2441
2456
  const setConfig = (jsonSchemaVersion, key, value) => {
2457
+ dialectJsonSchemaVersion[jsonSchemaVersion] = jsonSchemaVersion;
2458
+
2442
2459
  if (!config[jsonSchemaVersion]) {
2443
2460
  config[jsonSchemaVersion] = {};
2444
2461
  }
@@ -2465,23 +2482,25 @@ const add$1 = (schema, url = "", defaultSchemaVersion = "") => {
2465
2482
  }
2466
2483
  delete schema["$schema"];
2467
2484
 
2468
- // JSON Schema version
2485
+ // Determine JSON Schema version
2469
2486
  if (!(dialectId in dialectJsonSchemaVersion)) {
2470
2487
  if (schema?.$vocabulary?.[core201909Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core201909Id)[0]) {
2488
+ // Self describing 2019-09 meta-schema
2471
2489
  dialectJsonSchemaVersion[dialectId] = core201909Id;
2472
2490
  } else if (schema?.$vocabulary?.[core202012Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core202012Id)[0]) {
2491
+ // Self describing 2020-12 meta-schema
2473
2492
  dialectJsonSchemaVersion[dialectId] = core202012Id;
2474
- } else if (dialectId === getSchemaIdentifier(schema, externalId, dialectId)[0]) {
2475
- dialectJsonSchemaVersion[dialectId] = dialectId;
2476
- } else if (!(dialectId in schemaStore)) {
2477
- throw Error(`Couldn't determine JSON Schema version for dialect: '${dialectId}'`);
2478
2493
  } else {
2494
+ // Need to look at meta-schema to determine version
2479
2495
  const metaSchema = schemaStore[dialectId];
2480
- if (metaSchema.vocabulary[core201909Id] === true) {
2496
+ if (!metaSchema) {
2497
+ throw Error(`Couldn't determine JSON Schema version for dialect: '${dialectId}'`);
2498
+ } else if (metaSchema.vocabulary[core201909Id] === true) {
2481
2499
  dialectJsonSchemaVersion[dialectId] = core201909Id;
2482
2500
  } else if (metaSchema.vocabulary[core202012Id] === true) {
2483
2501
  dialectJsonSchemaVersion[dialectId] = core202012Id;
2484
2502
  } else {
2503
+ // Assume the jsonSchemaVersion is the meta-schema's dialectId (non-standard behavior)
2485
2504
  dialectJsonSchemaVersion[dialectId] = dialectJsonSchemaVersion[metaSchema.dialectId];
2486
2505
  }
2487
2506
  }
@@ -2620,7 +2639,15 @@ const get = async (url, contextDoc = nil) => {
2620
2639
  throw Error(`Failed to retrieve schema with id: ${id}`);
2621
2640
  }
2622
2641
 
2623
- add$1(await MediaTypes$1.parse(response), id);
2642
+ const [schema, defaultDialectId] = await MediaTypes$1.parse(response);
2643
+
2644
+ // Make sure the meta-schema is loaded if this isn't a known dialect
2645
+ const dialectId = resolveUrl$1(schema.$schema, "") || defaultDialectId;
2646
+ if (id !== dialectId && !(dialectId in dialectJsonSchemaVersion)) {
2647
+ await get(dialectId);
2648
+ }
2649
+
2650
+ add$1(schema, id);
2624
2651
  }
2625
2652
 
2626
2653
  const storedSchema = getStoredSchema(id);
@@ -2781,7 +2808,10 @@ let metaOutputFormat = DETAILED;
2781
2808
  let shouldMetaValidate = true;
2782
2809
 
2783
2810
  MediaTypes.addPlugin("application/schema+json", {
2784
- parse: async (response) => await response.json(),
2811
+ parse: async (response, contentTypeParameters) => [
2812
+ await response.json(),
2813
+ contentTypeParameters.schema || contentTypeParameters.profile
2814
+ ],
2785
2815
  matcher: (path) => path.endsWith(".schema.json")
2786
2816
  });
2787
2817