@hyperjump/json-schema 1.2.0 → 1.2.2
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/lib/common.d.ts +4 -2
- package/lib/common.js +63 -0
- package/lib/instance.js +2 -2
- package/lib/invalid-schema-error.d.ts +3 -3
- package/lib/schema.js +9 -10
- package/package.json +5 -3
package/lib/common.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export type JsonType = "object" | "array" | "string" | "number" | "boolean" | "null";
|
|
2
2
|
export type JsonSchemaType = JsonType | "integer";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
export const pathRelative: (from: string, to: string) => string;
|
|
5
|
+
|
|
6
|
+
export type Replacer = (key: string, value: unknown) => unknown;
|
|
7
|
+
export const jsonStringify: (value: unknown, replacer?: Replacer, space?: string) => string;
|
package/lib/common.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { resolveIri, toAbsoluteIri, parseIriReference } from "@hyperjump/uri";
|
|
2
|
+
import * as JsonPointer from "@hyperjump/json-pointer";
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
const isObject = (value) => typeof value === "object" && !Array.isArray(value) && value !== null;
|
|
@@ -86,3 +87,65 @@ export const pathRelative = (from, to) => {
|
|
|
86
87
|
|
|
87
88
|
return to.slice(toStart, to.length);
|
|
88
89
|
};
|
|
90
|
+
|
|
91
|
+
const defaultReplacer = (key, value) => value;
|
|
92
|
+
export const jsonStringify = (value, replacer = defaultReplacer, space = "") => {
|
|
93
|
+
return stringifyValue(value, replacer, space, "", JsonPointer.nil, 1);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const stringifyValue = (value, replacer, space, key, pointer, depth) => {
|
|
97
|
+
value = replacer(key, value, pointer);
|
|
98
|
+
let result;
|
|
99
|
+
if (Array.isArray(value)) {
|
|
100
|
+
result = stringifyArray(value, replacer, space, pointer, depth);
|
|
101
|
+
} else if (typeof value === "object" && value !== null) {
|
|
102
|
+
result = stringifyObject(value, replacer, space, pointer, depth);
|
|
103
|
+
} else {
|
|
104
|
+
result = JSON.stringify(value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return result;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const stringifyArray = (value, replacer, space, pointer, depth) => {
|
|
111
|
+
if (value.length === 0) {
|
|
112
|
+
return "[]";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const padding = space ? `\n${space.repeat(depth - 1)}` : "";
|
|
116
|
+
|
|
117
|
+
let result = "[" + padding + space;
|
|
118
|
+
for (let index = 0; index < value.length; index++) {
|
|
119
|
+
const indexPointer = JsonPointer.append(index, pointer);
|
|
120
|
+
const stringifiedValue = stringifyValue(value[index], replacer, space, String(index), indexPointer, depth + 1);
|
|
121
|
+
result += stringifiedValue === undefined ? "null" : stringifiedValue;
|
|
122
|
+
if (index + 1 < value.length) {
|
|
123
|
+
result += `,${padding}${space}`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return result + padding + "]";
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const stringifyObject = (value, replacer, space, pointer, depth) => {
|
|
130
|
+
const entries = Object.entries(value);
|
|
131
|
+
if (entries.length === 0) {
|
|
132
|
+
return "{}";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const padding = space ? `\n${space.repeat(depth - 1)}` : "";
|
|
136
|
+
const colonSpacing = space ? " " : "";
|
|
137
|
+
|
|
138
|
+
let result = "{" + padding + space;
|
|
139
|
+
for (let index = 0; index < entries.length; index++) {
|
|
140
|
+
const [key, value] = entries[index];
|
|
141
|
+
const keyPointer = JsonPointer.append(key, pointer);
|
|
142
|
+
const stringifiedValue = stringifyValue(value, replacer, space, key, keyPointer, depth + 1);
|
|
143
|
+
if (stringifiedValue !== undefined) {
|
|
144
|
+
result += JSON.stringify(key) + ":" + colonSpacing + stringifiedValue;
|
|
145
|
+
if (entries[index + 1]) {
|
|
146
|
+
result += `,${padding}${space}`;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result + padding + "}";
|
|
151
|
+
};
|
package/lib/instance.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { append as pointerAppend } from "@hyperjump/json-pointer";
|
|
2
2
|
import curry from "just-curry-it";
|
|
3
3
|
import { toAbsoluteUri, jsonTypeOf } from "./common.js";
|
|
4
4
|
import * as Reference from "./reference.js";
|
|
@@ -27,7 +27,7 @@ export const typeOf = curry((doc, type) => jsonTypeOf(value(doc), type));
|
|
|
27
27
|
|
|
28
28
|
export const step = (key, doc) => ({
|
|
29
29
|
...doc,
|
|
30
|
-
pointer:
|
|
30
|
+
pointer: pointerAppend(key, doc.pointer),
|
|
31
31
|
value: value(doc)[key]
|
|
32
32
|
});
|
|
33
33
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OutputUnit } from "./core.js";
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
export class InvalidSchemaError extends Error {
|
|
5
|
-
public output:
|
|
5
|
+
public output: OutputUnit;
|
|
6
6
|
|
|
7
|
-
public constructor(output:
|
|
7
|
+
public constructor(output: OutputUnit);
|
|
8
8
|
}
|
package/lib/schema.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import curry from "just-curry-it";
|
|
2
2
|
import * as Pact from "@hyperjump/pact";
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import { jsonTypeOf, resolveUri, toAbsoluteUri, uriFragment, pathRelative } from "./common.js";
|
|
3
|
+
import { nil as nilPointer, append as pointerAppend, get as pointerGet } from "@hyperjump/json-pointer";
|
|
4
|
+
import { jsonTypeOf, resolveUri, toAbsoluteUri, uriFragment, pathRelative, jsonStringify } from "./common.js";
|
|
6
5
|
import fetch from "./fetch.js";
|
|
7
6
|
import { hasDialect, loadDialect, getKeywordName } from "./keywords.js";
|
|
8
7
|
import { parseResponse } from "./media-types.js";
|
|
@@ -64,7 +63,7 @@ export const add = (schema, retrievalUri = undefined, contextDialectId = undefin
|
|
|
64
63
|
schemaStore[id] = {
|
|
65
64
|
id: id,
|
|
66
65
|
dialectId: dialectId,
|
|
67
|
-
schema: processSchema(schema, id, dialectId,
|
|
66
|
+
schema: processSchema(schema, id, dialectId, nilPointer, anchors, dynamicAnchors),
|
|
68
67
|
anchors: anchors,
|
|
69
68
|
dynamicAnchors: dynamicAnchors,
|
|
70
69
|
validated: false
|
|
@@ -126,11 +125,11 @@ const processSchema = (subject, id, dialectId, pointer, anchors, dynamicAnchors)
|
|
|
126
125
|
}
|
|
127
126
|
|
|
128
127
|
for (const key in subject) {
|
|
129
|
-
subject[key] = processSchema(subject[key], id, dialectId,
|
|
128
|
+
subject[key] = processSchema(subject[key], id, dialectId, pointerAppend(key, pointer), anchors, dynamicAnchors);
|
|
130
129
|
}
|
|
131
130
|
} else if (Array.isArray(subject)) {
|
|
132
131
|
for (let index = 0; index < subject.length; index++) {
|
|
133
|
-
subject[index] = processSchema(subject[index], id, dialectId,
|
|
132
|
+
subject[index] = processSchema(subject[index], id, dialectId, pointerAppend(index, pointer), anchors, dynamicAnchors);
|
|
134
133
|
}
|
|
135
134
|
}
|
|
136
135
|
|
|
@@ -148,7 +147,7 @@ export const markValidated = (id) => {
|
|
|
148
147
|
const nil = {
|
|
149
148
|
id: undefined,
|
|
150
149
|
dialectId: undefined,
|
|
151
|
-
pointer:
|
|
150
|
+
pointer: nilPointer,
|
|
152
151
|
schema: undefined,
|
|
153
152
|
value: undefined,
|
|
154
153
|
anchors: {},
|
|
@@ -184,7 +183,7 @@ export const get = async (url, contextDoc = nil) => {
|
|
|
184
183
|
const doc = {
|
|
185
184
|
...storedSchema,
|
|
186
185
|
pointer: pointer,
|
|
187
|
-
value:
|
|
186
|
+
value: pointerGet(pointer, storedSchema.schema)
|
|
188
187
|
};
|
|
189
188
|
|
|
190
189
|
return followReferences(doc);
|
|
@@ -210,7 +209,7 @@ export const step = (key, doc) => {
|
|
|
210
209
|
const storedSchema = getStoredSchema(doc.id);
|
|
211
210
|
return followReferences({
|
|
212
211
|
...doc,
|
|
213
|
-
pointer:
|
|
212
|
+
pointer: pointerAppend(`${key}`, doc.pointer),
|
|
214
213
|
value: value(doc)[key],
|
|
215
214
|
validated: storedSchema.validated
|
|
216
215
|
});
|
|
@@ -261,7 +260,7 @@ export const toSchema = (schemaDoc, options = {}) => {
|
|
|
261
260
|
dynamicAnchors[pointer] = anchor;
|
|
262
261
|
}
|
|
263
262
|
|
|
264
|
-
const schema = JSON.parse(
|
|
263
|
+
const schema = JSON.parse(jsonStringify(schemaDoc.schema, (key, value, pointer) => {
|
|
265
264
|
if (Reference.isReference(value)) {
|
|
266
265
|
const refValue = Reference.value(value);
|
|
267
266
|
if (fullOptions.includeEmbedded || !(idToken in refValue)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperjump/json-schema",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
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",
|
|
@@ -62,13 +62,15 @@
|
|
|
62
62
|
"yaml": "*"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@hyperjump/json": "^
|
|
66
|
-
"@hyperjump/json-pointer": "^0.9.5",
|
|
65
|
+
"@hyperjump/json-pointer": "^1.0.0",
|
|
67
66
|
"@hyperjump/pact": "^0.2.4",
|
|
68
67
|
"@hyperjump/uri": "^1.0.0",
|
|
69
68
|
"content-type": "^1.0.4",
|
|
70
69
|
"fastest-stable-stringify": "^2.0.2",
|
|
71
70
|
"node-fetch": "^3.3.0",
|
|
72
71
|
"uuid": "^9.0.0"
|
|
72
|
+
},
|
|
73
|
+
"engines": {
|
|
74
|
+
"node": "^18.0.0"
|
|
73
75
|
}
|
|
74
76
|
}
|