@hyperjump/json-schema 0.20.0 → 0.21.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.
@@ -2094,6 +2094,88 @@
2094
2094
 
2095
2095
  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 };
2096
2096
 
2097
+ var entries$2 = async (doc) => Object.entries(await doc);
2098
+
2099
+ const curry$7 = justCurryIt;
2100
+
2101
+
2102
+ var map$3 = curry$7(async (fn, doc) => (await doc).map(fn));
2103
+
2104
+ const curry$6 = justCurryIt;
2105
+
2106
+
2107
+ var reduce$2 = curry$6(async (fn, acc, doc) => {
2108
+ return (await doc).reduce(async (acc, item) => fn(await acc, item), acc);
2109
+ });
2110
+
2111
+ const curry$5 = justCurryIt;
2112
+ const reduce$1 = reduce$2;
2113
+
2114
+
2115
+ var filter = curry$5(async (fn, doc, options = {}) => {
2116
+ return reduce$1(async (acc, item) => {
2117
+ return (await fn(item)) ? acc.concat([item]) : acc;
2118
+ }, [], doc, options);
2119
+ });
2120
+
2121
+ const curry$4 = justCurryIt;
2122
+ const map$2 = map$3;
2123
+
2124
+
2125
+ var some = curry$4(async (fn, doc) => {
2126
+ const results = await map$2(fn, doc);
2127
+ return (await Promise.all(results))
2128
+ .some((a) => a);
2129
+ });
2130
+
2131
+ const curry$3 = justCurryIt;
2132
+ const map$1 = map$3;
2133
+
2134
+
2135
+ var every = curry$3(async (fn, doc) => {
2136
+ const results = await map$1(fn, doc);
2137
+ return (await Promise.all(results))
2138
+ .every((a) => a);
2139
+ });
2140
+
2141
+ const curry$2 = justCurryIt;
2142
+
2143
+
2144
+ var pipeline$1 = curry$2((fns, doc) => {
2145
+ return fns.reduce(async (acc, fn) => fn(await acc), doc);
2146
+ });
2147
+
2148
+ var all = (doc) => Promise.all(doc);
2149
+
2150
+ const pipeline = pipeline$1;
2151
+ const entries$1 = entries$2;
2152
+ const reduce = reduce$2;
2153
+
2154
+
2155
+ var allValues = (doc) => {
2156
+ return pipeline([
2157
+ entries$1,
2158
+ reduce(async (acc, [propertyName, propertyValue]) => {
2159
+ acc[propertyName] = await propertyValue;
2160
+ return acc;
2161
+ }, {})
2162
+ ], doc);
2163
+ };
2164
+
2165
+ var lib$2 = {
2166
+ entries: entries$2,
2167
+ map: map$3,
2168
+ filter: filter,
2169
+ reduce: reduce$2,
2170
+ some: some,
2171
+ every: every,
2172
+ pipeline: pipeline$1,
2173
+ all: all,
2174
+ allValues: allValues
2175
+ };
2176
+
2177
+ var fetch_browser = fetch;
2178
+
2097
2179
  var contentType = {};
2098
2180
 
2099
2181
  /*!
@@ -2148,7 +2230,7 @@
2148
2230
  */
2149
2231
 
2150
2232
  contentType.format = format$1;
2151
- contentType.parse = parse;
2233
+ contentType.parse = parse$1;
2152
2234
 
2153
2235
  /**
2154
2236
  * Format object to media type.
@@ -2199,7 +2281,7 @@
2199
2281
  * @public
2200
2282
  */
2201
2283
 
2202
- function parse (string) {
2284
+ function parse$1 (string) {
2203
2285
  if (!string) {
2204
2286
  throw new TypeError('argument string is required')
2205
2287
  }
@@ -2317,97 +2399,47 @@
2317
2399
  this.type = type;
2318
2400
  }
2319
2401
 
2320
- var entries$2 = async (doc) => Object.entries(await doc);
2321
-
2322
- const curry$7 = justCurryIt;
2323
-
2324
-
2325
- var map$3 = curry$7(async (fn, doc) => (await doc).map(fn));
2326
-
2327
- const curry$6 = justCurryIt;
2328
-
2329
-
2330
- var reduce$2 = curry$6(async (fn, acc, doc) => {
2331
- return (await doc).reduce(async (acc, item) => fn(await acc, item), acc);
2332
- });
2333
-
2334
- const curry$5 = justCurryIt;
2335
- const reduce$1 = reduce$2;
2336
-
2337
-
2338
- var filter = curry$5(async (fn, doc, options = {}) => {
2339
- return reduce$1(async (acc, item) => {
2340
- return (await fn(item)) ? acc.concat([item]) : acc;
2341
- }, [], doc, options);
2342
- });
2343
-
2344
- const curry$4 = justCurryIt;
2345
- const map$2 = map$3;
2346
-
2347
-
2348
- var some = curry$4(async (fn, doc) => {
2349
- const results = await map$2(fn, doc);
2350
- return (await Promise.all(results))
2351
- .some((a) => a);
2352
- });
2353
-
2354
- const curry$3 = justCurryIt;
2355
- const map$1 = map$3;
2356
-
2357
-
2358
- var every = curry$3(async (fn, doc) => {
2359
- const results = await map$1(fn, doc);
2360
- return (await Promise.all(results))
2361
- .every((a) => a);
2362
- });
2363
-
2364
- const curry$2 = justCurryIt;
2365
-
2366
-
2367
- var pipeline$1 = curry$2((fns, doc) => {
2368
- return fns.reduce(async (acc, fn) => fn(await acc), doc);
2369
- });
2402
+ const contentTypeParser = contentType;
2370
2403
 
2371
- var all = (doc) => Promise.all(doc);
2372
2404
 
2373
- const pipeline = pipeline$1;
2374
- const entries$1 = entries$2;
2375
- const reduce = reduce$2;
2405
+ const mediaTypePlugins = {};
2376
2406
 
2407
+ const addPlugin = (contentType, plugin) => {
2408
+ mediaTypePlugins[contentType] = plugin;
2409
+ };
2377
2410
 
2378
- var allValues = (doc) => {
2379
- return pipeline([
2380
- entries$1,
2381
- reduce(async (acc, [propertyName, propertyValue]) => {
2382
- acc[propertyName] = await propertyValue;
2383
- return acc;
2384
- }, {})
2385
- ], doc);
2411
+ const parse = (response) => {
2412
+ const contentType = contentTypeParser.parse(response.headers.get("content-type")).type;
2413
+ if (!(contentType in mediaTypePlugins)) {
2414
+ throw Error(`${response.url} is not a schema. Found a document with media type: ${contentType}`);
2415
+ }
2416
+ return mediaTypePlugins[contentType].parse(response);
2386
2417
  };
2387
2418
 
2388
- var lib$2 = {
2389
- entries: entries$2,
2390
- map: map$3,
2391
- filter: filter,
2392
- reduce: reduce$2,
2393
- some: some,
2394
- every: every,
2395
- pipeline: pipeline$1,
2396
- all: all,
2397
- allValues: allValues
2419
+ const getContentType = (path) => {
2420
+ for (const contentType in mediaTypePlugins) {
2421
+ if (mediaTypePlugins[contentType].matcher(path)) {
2422
+ return contentType;
2423
+ }
2424
+ }
2425
+
2426
+ return "application/octet-stream";
2398
2427
  };
2399
2428
 
2400
- var fetch_browser = fetch;
2429
+ var mediaTypes = { addPlugin, parse, getContentType };
2401
2430
 
2402
- const contentTypeParser = contentType;
2403
2431
  const curry$1 = justCurryIt;
2404
2432
  const Pact$a = lib$2;
2405
2433
  const JsonPointer = lib$3;
2406
2434
  const { jsonTypeOf, resolveUrl: resolveUrl$1, urlFragment, pathRelative } = common$1;
2407
2435
  const fetch$1 = fetch_browser;
2408
2436
  const Reference$1 = reference;
2437
+ const MediaTypes$1 = mediaTypes;
2409
2438
 
2410
2439
 
2440
+ const core201909Id = "https://json-schema.org/draft/2019-09/vocab/core";
2441
+ const core202012Id = "https://json-schema.org/draft/2020-12/vocab/core";
2442
+
2411
2443
  // Config
2412
2444
  const config = {};
2413
2445
  const dialectJsonSchemaVersion = {};
@@ -2420,10 +2452,8 @@
2420
2452
  };
2421
2453
 
2422
2454
  const getConfig = (dialectId, key) => {
2423
- const jsonSchemaVersion = dialectJsonSchemaVersion[dialectId] || dialectId;
2424
- if (jsonSchemaVersion in config) {
2425
- return config[jsonSchemaVersion][key];
2426
- }
2455
+ const jsonSchemaVersion = dialectJsonSchemaVersion[dialectId];
2456
+ return config[jsonSchemaVersion]?.[key];
2427
2457
  };
2428
2458
 
2429
2459
  // Schema Management
@@ -2432,6 +2462,7 @@
2432
2462
 
2433
2463
  const add$1 = (schema, url = "", defaultSchemaVersion = "") => {
2434
2464
  schema = JSON.parse(JSON.stringify(schema));
2465
+ const externalId = resolveUrl$1(url, "");
2435
2466
 
2436
2467
  // Dialect / JSON Schema Version
2437
2468
  const dialectId = resolveUrl$1(schema["$schema"] || defaultSchemaVersion, "");
@@ -2440,35 +2471,43 @@
2440
2471
  }
2441
2472
  delete schema["$schema"];
2442
2473
 
2443
- let jsonSchemaVersion;
2444
- if (schema.$vocabulary?.["https://json-schema.org/draft/2019-09/vocab/core"] === true) {
2445
- jsonSchemaVersion = "https://json-schema.org/draft/2019-09/vocab/core";
2446
- } else if (schema.$vocabulary?.["https://json-schema.org/draft/2020-12/vocab/core"] === true) {
2447
- jsonSchemaVersion = "https://json-schema.org/draft/2020-12/vocab/core";
2448
- } else {
2449
- jsonSchemaVersion = dialectJsonSchemaVersion[dialectId] || dialectId;
2474
+ // JSON Schema version
2475
+ if (!(dialectId in dialectJsonSchemaVersion)) {
2476
+ if (schema?.$vocabulary?.[core201909Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core201909Id)[0]) {
2477
+ dialectJsonSchemaVersion[dialectId] = core201909Id;
2478
+ } else if (schema?.$vocabulary?.[core202012Id] === true && dialectId === getSchemaIdentifier(schema, externalId, core202012Id)[0]) {
2479
+ dialectJsonSchemaVersion[dialectId] = core202012Id;
2480
+ } else if (dialectId === getSchemaIdentifier(schema, externalId, dialectId)[0]) {
2481
+ dialectJsonSchemaVersion[dialectId] = dialectId;
2482
+ } else if (!(dialectId in schemaStore)) {
2483
+ throw Error(`Couldn't determine JSON Schema version for dialect: '${dialectId}'`);
2484
+ } else {
2485
+ const metaSchema = schemaStore[dialectId];
2486
+ if (metaSchema.vocabulary[core201909Id] === true) {
2487
+ dialectJsonSchemaVersion[dialectId] = core201909Id;
2488
+ } else if (metaSchema.vocabulary[core202012Id] === true) {
2489
+ dialectJsonSchemaVersion[dialectId] = core202012Id;
2490
+ } else {
2491
+ dialectJsonSchemaVersion[dialectId] = dialectJsonSchemaVersion[metaSchema.dialectId];
2492
+ }
2493
+ }
2450
2494
  }
2451
2495
 
2452
- // Identifier
2453
- const baseToken = getConfig(jsonSchemaVersion, "baseToken");
2454
- const anchorToken = getConfig(jsonSchemaVersion, "anchorToken");
2455
- const externalId = resolveUrl$1(url, "");
2456
- if (!externalId && !resolveUrl$1(schema[baseToken] || "", "")) {
2496
+ // Internal Identifier
2497
+ const [id, fragment] = getSchemaIdentifier(schema, externalId, dialectJsonSchemaVersion[dialectId]);
2498
+ if (!id) {
2457
2499
  throw Error("Couldn't determine an identifier for the schema");
2458
2500
  }
2459
- const internalUrl = resolveUrl$1(externalId, schema[baseToken] || "");
2460
- const id = resolveUrl$1(internalUrl, "");
2461
- const fragment = urlFragment(internalUrl);
2501
+ const baseToken = getConfig(dialectId, "baseToken");
2462
2502
  delete schema[baseToken];
2463
- if (fragment && baseToken === anchorToken) {
2464
- schema[anchorToken] = anchorToken !== baseToken ? encodeURI(fragment) : `#${encodeURI(fragment)}`;
2465
- }
2466
2503
  if (externalId) {
2467
2504
  schemaStoreAlias[externalId] = id;
2468
2505
  }
2469
2506
 
2470
- // JSON Schema version
2471
- dialectJsonSchemaVersion[id] = jsonSchemaVersion;
2507
+ const anchorToken = getConfig(dialectId, "anchorToken");
2508
+ if (fragment && baseToken === anchorToken) {
2509
+ schema[anchorToken] = anchorToken !== baseToken ? encodeURI(fragment) : `#${encodeURI(fragment)}`;
2510
+ }
2472
2511
 
2473
2512
  // recursiveAnchor
2474
2513
  const dynamicAnchors = {};
@@ -2504,6 +2543,12 @@
2504
2543
  return id;
2505
2544
  };
2506
2545
 
2546
+ const getSchemaIdentifier = (schema, externalId, jsonSchemaVersion) => {
2547
+ const baseToken = config[jsonSchemaVersion]?.["baseToken"];
2548
+ const internalUrl = resolveUrl$1(externalId, schema[baseToken] || "");
2549
+ return [resolveUrl$1(internalUrl, ""), urlFragment(internalUrl)];
2550
+ };
2551
+
2507
2552
  const processSchema = (subject, id, dialectId, pointer, anchors, dynamicAnchors) => {
2508
2553
  if (jsonTypeOf(subject, "object")) {
2509
2554
  const embeddedSchemaDialectId = typeof subject.$schema === "string" ? resolveUrl$1(subject.$schema, "") : dialectId;
@@ -2581,14 +2626,7 @@
2581
2626
  throw Error(`Failed to retrieve schema with id: ${id}`);
2582
2627
  }
2583
2628
 
2584
- if (response.headers.has("content-type")) {
2585
- const contentType = contentTypeParser.parse(response.headers.get("content-type")).type;
2586
- if (contentType !== "application/schema+json") {
2587
- throw Error(`${id} is not a schema. Found a document with media type: ${contentType}`);
2588
- }
2589
- }
2590
-
2591
- add$1(await response.json(), id);
2629
+ add$1(await MediaTypes$1.parse(response), id);
2592
2630
  }
2593
2631
 
2594
2632
  const storedSchema = getStoredSchema(id);
@@ -2731,6 +2769,7 @@
2731
2769
  const Instance$E = instance;
2732
2770
  const Schema$R = schema$5;
2733
2771
  const InvalidSchemaError$2 = invalidSchemaError;
2772
+ const MediaTypes = mediaTypes;
2734
2773
 
2735
2774
 
2736
2775
  const FLAG = "FLAG", BASIC = "BASIC", DETAILED = "DETAILED", VERBOSE = "VERBOSE";
@@ -2738,6 +2777,11 @@
2738
2777
  let metaOutputFormat = DETAILED;
2739
2778
  let shouldMetaValidate = true;
2740
2779
 
2780
+ MediaTypes.addPlugin("application/schema+json", {
2781
+ parse: async (response) => await response.json(),
2782
+ matcher: (path) => path.endsWith(".schema.json")
2783
+ });
2784
+
2741
2785
  const validate$2 = async (schema, value = undefined, outputFormat = undefined) => {
2742
2786
  const compiled = await compile$O(schema);
2743
2787
  const interpretAst = (value, outputFormat) => interpret$O(compiled, Instance$E.cons(value), outputFormat);
@@ -2917,7 +2961,8 @@
2917
2961
  validate: validate$2, compile: compile$O, interpret: interpret$O,
2918
2962
  setMetaOutputFormat, setShouldMetaValidate, FLAG, BASIC, DETAILED, VERBOSE,
2919
2963
  add, getKeyword, hasKeyword, defineVocabulary,
2920
- compileSchema, interpretSchema, collectEvaluatedProperties: collectEvaluatedProperties$e, collectEvaluatedItems: collectEvaluatedItems$f
2964
+ compileSchema, interpretSchema, collectEvaluatedProperties: collectEvaluatedProperties$e, collectEvaluatedItems: collectEvaluatedItems$f,
2965
+ addMediaTypePlugin: MediaTypes.addPlugin
2921
2966
  };
2922
2967
 
2923
2968
  const Schema$Q = schema$5;
@@ -5631,6 +5676,7 @@
5631
5676
  interpret: Core.interpret,
5632
5677
  setMetaOutputFormat: Core.setMetaOutputFormat,
5633
5678
  setShouldMetaValidate: Core.setShouldMetaValidate,
5679
+ addMediaTypePlugin: Core.addMediaTypePlugin,
5634
5680
  FLAG: Core.FLAG,
5635
5681
  BASIC: Core.BASIC,
5636
5682
  DETAILED: Core.DETAILED,