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