@hyperjump/json-schema 1.9.1 → 1.9.3

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
@@ -592,8 +592,6 @@ These are available from the `@hyperjump/json-schema/experimental` export.
592
592
  * includeDialect: "auto" | "always" | "never" (default: "auto") -- If
593
593
  "auto", `$schema` will only be included if it differs from
594
594
  `contextDialectId`.
595
- * selfIdentify: boolean (default: false) -- If true, `$id` will be
596
- included.
597
595
  * contextUri: string (default: "") -- `$id`s will be relative to this
598
596
  URI.
599
597
  * includeEmbedded: boolean (default: true) -- If false, embedded schemas
@@ -631,6 +629,10 @@ working with generators and async generators.
631
629
  * **fromJs**: (value: any, uri?: string) => JsonNode
632
630
 
633
631
  Construct a JsonNode from a JavaScript value.
632
+ * **cons**: (baseUri: string, pointer: string, value: any, type: string, children: JsonNode[], parent?: JsonNode) => JsonNode
633
+
634
+ Construct a JsonNode. This is used internally. You probably want `fromJs`
635
+ instead.
634
636
  * **get**: (url: string, instance: JsonNode) => JsonNode
635
637
 
636
638
  Apply a same-resource reference to a JsonNode.
@@ -18,7 +18,7 @@ export const annotation = (node, keyword, dialect = defaultDialectId) => {
18
18
  let currentNode = node.root;
19
19
  const errors = Object.keys(node.root.errors);
20
20
  for (let segment of JsonPointer.pointerSegments(node.pointer)) {
21
- segment = segment === "-" && currentNode.typeOf() === "array" ? currentNode.length() : segment;
21
+ segment = segment === "-" && Instance.typeOf(currentNode) === "array" ? Instance.length(currentNode) : segment;
22
22
  currentNode = Instance.step(segment, currentNode);
23
23
  errors.push(...Object.keys(currentNode.errors));
24
24
  }
package/bundle/index.js CHANGED
@@ -3,7 +3,6 @@ import { v4 as uuid } from "uuid";
3
3
  import * as Browser from "@hyperjump/browser";
4
4
  import * as JsonPointer from "@hyperjump/json-pointer";
5
5
  import { asyncCollectSet, asyncFilter, asyncFlatten, asyncMap, pipe } from "@hyperjump/pact";
6
- import { resolveUri } from "@hyperjump/uri";
7
6
  import {
8
7
  Validation,
9
8
  canonicalUri, getSchema, toSchema,
@@ -37,10 +36,13 @@ export const bundle = async (url, options = {}) => {
37
36
  }
38
37
 
39
38
  for (const uri of externalIds) {
39
+ if (fullOptions.externalSchemas.includes(uri)) {
40
+ continue;
41
+ }
42
+
40
43
  const externalSchema = await getSchema(uri);
41
44
  const embeddedSchema = toSchema(externalSchema, {
42
- selfIdentify: true,
43
- contextUri: contextUri,
45
+ contextUri: externalSchema.document.baseUri.startsWith("file:") ? contextUri : undefined,
44
46
  includeDialect: fullOptions.alwaysIncludeDialect ? "always" : "auto",
45
47
  contextDialectId: contextDialectId
46
48
  });
@@ -49,9 +51,6 @@ export const bundle = async (url, options = {}) => {
49
51
  const idToken = getKeywordName(externalSchema.document.dialectId, "https://json-schema.org/keyword/id")
50
52
  || getKeywordName(externalSchema.document.dialectId, "https://json-schema.org/keyword/draft-04/id");
51
53
  id = embeddedSchema[idToken];
52
- if (id in JsonPointer.get(bundlingLocation, bundled)) {
53
- id = resolveUri(id, contextUri);
54
- }
55
54
  } else if (fullOptions.definitionNamingStrategy === UUID) {
56
55
  id = uuid();
57
56
  } else {
package/lib/instance.d.ts CHANGED
@@ -3,6 +3,14 @@ import type { Json } from "@hyperjump/json-pointer";
3
3
 
4
4
  export const fromJs: (value: Json, uri?: string) => JsonNode;
5
5
 
6
+ export const cons: (
7
+ baseUri: string,
8
+ pointer: string,
9
+ value: Json,
10
+ type: JsonNodeType,
11
+ children: JsonNode[],
12
+ parent?: JsonNode
13
+ ) => JsonNode;
6
14
  export const get: (url: string, context: JsonNode) => JsonNode | undefined;
7
15
  export const uri: (node: JsonNode) => string;
8
16
  export const value: <A>(node: JsonNode) => A;
@@ -25,6 +33,8 @@ export type JsonNode = {
25
33
  parent: JsonNode;
26
34
  root: JsonNode;
27
35
  valid: boolean;
36
+ errors: Record<string, string>;
37
+ annotations: Record<string, Record<string, unknown>>;
28
38
  };
29
39
 
30
40
  type JsonNodeType = "object" | "array" | "string" | "number" | "boolean" | "null" | "property";
package/lib/instance.js CHANGED
@@ -40,7 +40,7 @@ export const fromJs = (value, uri = "", pointer = "", parent = undefined) => {
40
40
  }
41
41
  };
42
42
 
43
- const cons = (baseUri, pointer, value, type, children, parent) => {
43
+ export const cons = (baseUri, pointer, value, type, children, parent) => {
44
44
  const node = {
45
45
  baseUri: baseUri ? toAbsoluteIri(baseUri) : "",
46
46
  pointer: pointer,
package/lib/schema.js CHANGED
@@ -206,6 +206,10 @@ const processSchema = (json, id, dialectId, cursor, embedded, anchors, dynamicAn
206
206
  export const canonicalUri = (browser) => `${browser.document.baseUri}#${encodeURI(browser.cursor)}`;
207
207
 
208
208
  export const toSchema = (browser, options = {}) => {
209
+ if (!options.contextUri && browser.document.baseUri.startsWith("file:")) {
210
+ options.contextUri = browser.document.baseUri;
211
+ }
212
+
209
213
  const anchors = {};
210
214
  for (const anchor in browser.document.anchors) {
211
215
  if (anchor !== "" && !browser.document.dynamicAnchors[anchor]) {
@@ -237,9 +241,7 @@ export const toSchema = (browser, options = {}) => {
237
241
  if (JSON.stringify(value.toJSON()) === "{}") {
238
242
  return toSchema({ document: browser.document.embedded[toAbsoluteIri(value.href)] }, {
239
243
  ...options,
240
- contextDialectId: browser.document.dialectId,
241
- selfIdentify: true,
242
- contextUri: browser.document.baseUri
244
+ contextDialectId: browser.document.dialectId
243
245
  });
244
246
  } else {
245
247
  return { [legacyRefToken]: value.href };
@@ -247,9 +249,7 @@ export const toSchema = (browser, options = {}) => {
247
249
  } else if (options.includeEmbedded ?? true) {
248
250
  return toSchema({ document: browser.document.embedded[toAbsoluteIri(value.href)] }, {
249
251
  ...options,
250
- contextDialectId: browser.document.dialectId,
251
- selfIdentify: true,
252
- contextUri: browser.document.baseUri
252
+ contextDialectId: browser.document.dialectId
253
253
  });
254
254
  } else {
255
255
  return;
@@ -295,18 +295,25 @@ export const toSchema = (browser, options = {}) => {
295
295
  }
296
296
 
297
297
  // Self-identification
298
- if (options.selfIdentify ?? false) {
299
- if (options.contextUri) {
300
- schema = {
301
- [idToken]: toRelativeIri(normalizeIri(options.contextUri), browser.document.baseUri),
302
- ...schema
303
- };
304
- } else {
298
+ if (options.contextUri) {
299
+ const relativeUri = toRelativeIri(normalizeIri(options.contextUri), browser.document.baseUri);
300
+ if (relativeUri !== "") {
301
+ const id = schema[idToken] ? `${relativeUri}${schema[idToken]}` : relativeUri;
302
+ delete schema[idToken];
303
+
305
304
  schema = {
306
- [idToken]: browser.document.baseUri,
305
+ [idToken]: id,
307
306
  ...schema
308
307
  };
309
308
  }
309
+ } else if (!browser.document.baseUri.startsWith("file:")) {
310
+ const id = schema[idToken] ? `${browser.document.baseUri}${schema[idToken]}` : browser.document.baseUri;
311
+ delete schema[idToken];
312
+
313
+ schema = {
314
+ [idToken]: id,
315
+ ...schema
316
+ };
310
317
  }
311
318
 
312
319
  // $schema
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperjump/json-schema",
3
- "version": "1.9.1",
3
+ "version": "1.9.3",
4
4
  "description": "A JSON Schema validator with support for custom keywords, vocabularies, and dialects",
5
5
  "type": "module",
6
6
  "main": "./stable/index.js",