@hyperjump/json-schema 1.9.2 → 1.9.4

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Jason Desrosiers
3
+ Copyright (c) 2022 Hyperjump Software, LLC
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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
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
@@ -23,14 +23,14 @@ export const values: (node: JsonNode) => Generator<JsonNode>;
23
23
  export const entries: (node: JsonNode) => Generator<[JsonNode, JsonNode]>;
24
24
  export const length: (node: JsonNode) => number;
25
25
 
26
- export const allNodes: (node) => Generator<JsonNode>;
26
+ export const allNodes: (node: JsonNode) => Generator<JsonNode>;
27
27
 
28
28
  export type JsonNode = {
29
29
  baseUri: string;
30
30
  pointer: string;
31
31
  type: JsonNodeType;
32
32
  children: JsonNode[];
33
- parent: JsonNode;
33
+ parent?: JsonNode;
34
34
  root: JsonNode;
35
35
  valid: boolean;
36
36
  errors: Record<string, string>;
package/lib/instance.js CHANGED
@@ -118,7 +118,9 @@ export const values = function* (node) {
118
118
  }
119
119
 
120
120
  for (const property of node.children) {
121
- yield property.children[1];
121
+ if (property.children[1]) {
122
+ yield property.children[1];
123
+ }
122
124
  }
123
125
  };
124
126
 
@@ -128,7 +130,9 @@ export const entries = function* (node) {
128
130
  }
129
131
 
130
132
  for (const property of node.children) {
131
- yield property.children;
133
+ if (property.children.length === 2) {
134
+ yield property.children;
135
+ }
132
136
  }
133
137
  };
134
138
 
@@ -1,13 +1,18 @@
1
1
  import * as Browser from "@hyperjump/browser";
2
2
  import * as Instance from "../../annotations/annotated-instance.js";
3
+ import { pointerSegments } from "@hyperjump/json-pointer";
3
4
 
4
5
 
5
6
  const id = "https://json-schema.org/keyword/unknown";
6
7
 
7
- const compile = (schema) => Browser.value(schema);
8
+ const compile = (schema) => {
9
+ const keywordName = [...pointerSegments(schema.cursor)].pop();
10
+ return [keywordName, Browser.value(schema)];
11
+ };
8
12
 
9
- const interpret = (value, instance, _ast, _dynamicAnchors, _quiet, schemaLocation) => {
10
- Instance.setAnnotation(instance, id, schemaLocation, value);
13
+ const interpret = ([keywordName, value], instance, _ast, _dynamicAnchors, _quiet, schemaLocation) => {
14
+ const keywordId = `${id}#${keywordName}`;
15
+ Instance.setAnnotation(instance, keywordId, schemaLocation, value);
11
16
  return true;
12
17
  };
13
18
 
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.2",
3
+ "version": "1.9.4",
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",