@amritk/helpers 0.15.3 → 0.16.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.
- package/dist/assert-id-scopes.js +9 -8
- package/dist/assign-key.d.ts +17 -0
- package/dist/assign-key.js +10 -0
- package/dist/build-anchor-map.js +8 -8
- package/dist/build-dynamic-ref-map.js +10 -10
- package/dist/build-resource-registry.d.ts +54 -0
- package/dist/build-resource-registry.js +35 -12
- package/dist/derive-root-type-name.js +9 -5
- package/dist/escape-regex-pattern.js +42 -35
- package/dist/extract-refs.js +11 -6
- package/dist/fold-nullable.js +17 -10
- package/dist/generate-index-barrel.js +7 -11
- package/dist/generate-type-definition.js +156 -138
- package/dist/graft-external-schemas.js +3 -2
- package/dist/has-ref.js +1 -1
- package/dist/max-schema-depth.d.ts +8 -1
- package/dist/max-schema-depth.js +3 -3
- package/dist/mjst-extension.js +3 -2
- package/dist/normalize-ref-scopes.js +12 -6
- package/dist/parse-documentation.js +1 -1
- package/dist/prune-external-schemas.js +10 -8
- package/dist/quote-js-string.js +3 -1
- package/dist/read-key.d.ts +25 -0
- package/dist/read-key.js +6 -0
- package/dist/ref-to-name.d.ts +41 -0
- package/dist/ref-to-name.js +11 -5
- package/dist/resolve-dynamic-refs.js +19 -12
- package/dist/resolve-ref.js +2 -1
- package/dist/safe-accessor.d.ts +1 -1
- package/dist/safe-accessor.js +1 -1
- package/dist/schema-guards.js +34 -34
- package/dist/string-length-check.js +2 -2
- package/dist/unknown-key-check.d.ts +14 -3
- package/dist/unknown-key-check.js +2 -2
- package/dist/upgrade-draft07-schema.js +49 -34
- package/dist/validate-record.d.ts +6 -1
- package/dist/validate-record.js +1 -1
- package/dist/walk-ref-graph.js +75 -33
- package/package.json +1 -1
- package/src/has-ref.ts +1 -1
- package/src/validate-record.ts +7 -2
package/dist/assert-id-scopes.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import { buildResourceRegistry,
|
|
1
|
+
import { buildResourceRegistry, entersSchemaMap, escapePointerSegment, isDataPosition } from "./build-resource-registry.js";
|
|
2
2
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
3
|
+
import { readKey } from "./read-key.js";
|
|
3
4
|
import { resolveRef } from "./resolve-ref.js";
|
|
4
5
|
import { resolveScopedRef } from "./resolve-scoped-ref.js";
|
|
5
|
-
const declaresLocalTargets = (record) => typeof record
|
|
6
|
+
const declaresLocalTargets = (record) => typeof readKey(record, "$defs") === "object" || typeof readKey(record, "definitions") === "object" || typeof readKey(record, "$anchor") === "string";
|
|
6
7
|
const assertIdScopes = (rootSchema) => {
|
|
7
8
|
const registry = buildResourceRegistry(rootSchema);
|
|
8
9
|
if (registry === null)
|
|
9
10
|
return;
|
|
10
|
-
const walk = (node, pointer, enclosing, resource, depth) => {
|
|
11
|
+
const walk = (node, pointer, enclosing, resource, depth, inSchemaMap) => {
|
|
11
12
|
assertSchemaDepth(depth, "assertIdScopes");
|
|
12
13
|
if (node === null || typeof node !== "object")
|
|
13
14
|
return;
|
|
14
15
|
if (Array.isArray(node)) {
|
|
15
16
|
for (let index = 0; index < node.length; index++) {
|
|
16
|
-
walk(node[index], `${pointer}/${index}`, enclosing, resource, depth + 1);
|
|
17
|
+
walk(node[index], `${pointer}/${index}`, enclosing, resource, depth + 1, inSchemaMap);
|
|
17
18
|
}
|
|
18
19
|
return;
|
|
19
20
|
}
|
|
@@ -22,7 +23,7 @@ const assertIdScopes = (rootSchema) => {
|
|
|
22
23
|
const base = declared ?? enclosing;
|
|
23
24
|
const scope = declared !== void 0 && pointer !== "" ? record : resource;
|
|
24
25
|
for (const key of ["$ref", "$dynamicRef"]) {
|
|
25
|
-
const ref = record
|
|
26
|
+
const ref = readKey(record, key);
|
|
26
27
|
if (typeof ref !== "string" || !ref.startsWith("#") || ref === "#")
|
|
27
28
|
continue;
|
|
28
29
|
if (scope === null || !declaresLocalTargets(scope))
|
|
@@ -33,12 +34,12 @@ const assertIdScopes = (rootSchema) => {
|
|
|
33
34
|
throw new Error(`The subschema with $id "${scope["$id"]}" declares its own definitions and contains the ref "${ref}", which names nothing inside it. $id base-URI scoping makes that ref unresolvable, but resolving it from the document root would silently select a different definition. Point the ref at a definition the embedded resource declares, hoist the resource into the root $defs, or drop the inner $id.`);
|
|
34
35
|
}
|
|
35
36
|
for (const key of Object.keys(record)) {
|
|
36
|
-
if (
|
|
37
|
+
if (isDataPosition(key, inSchemaMap))
|
|
37
38
|
continue;
|
|
38
|
-
walk(record[key], `${pointer}/${escapePointerSegment(key)}`, base, scope, depth + 1);
|
|
39
|
+
walk(record[key], `${pointer}/${escapePointerSegment(key)}`, base, scope, depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
39
40
|
}
|
|
40
41
|
};
|
|
41
|
-
walk(rootSchema, "", registry.rootBase, null, 0);
|
|
42
|
+
walk(rootSchema, "", registry.rootBase, null, 0, false);
|
|
42
43
|
};
|
|
43
44
|
export {
|
|
44
45
|
assertIdScopes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Assigns `value` under `key` on a rebuilt object without letting a `__proto__`
|
|
3
|
+
* key reach the prototype setter.
|
|
4
|
+
*
|
|
5
|
+
* Every walker in this package rebuilds schema objects key by key, and a
|
|
6
|
+
* schema may legitimately describe a property named `__proto__`. A plain
|
|
7
|
+
* `target[key] = value` on that key sets the rebuilt object's *prototype*
|
|
8
|
+
* instead of adding a property: the key disappears from the output — a
|
|
9
|
+
* declared property silently gone, along with every constraint on it — and the
|
|
10
|
+
* object starts inheriting the subschema's own keys, so a later `node.$ref` or
|
|
11
|
+
* `node.type` reads a value the node never had.
|
|
12
|
+
*
|
|
13
|
+
* Defining it as an own data property keeps the key verbatim and leaves the
|
|
14
|
+
* prototype alone. `@amritk/resolve-refs` carries the same helper for the same
|
|
15
|
+
* reason; the two are deliberately identical.
|
|
16
|
+
*/
|
|
17
|
+
export declare const assignKey: (target: Record<string, unknown>, key: string, value: unknown) => void;
|
package/dist/build-anchor-map.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
+
import { entersSchemaMap, escapePointerSegment, isDataPosition } from "./build-resource-registry.js";
|
|
1
2
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
3
|
+
import { readKey } from "./read-key.js";
|
|
2
4
|
import { isSchemaObject } from "./schema-guards.js";
|
|
3
|
-
const escapeSegment = (segment) => segment.indexOf("~") !== -1 || segment.indexOf("/") !== -1 ? segment.replace(/~/g, "~0").replace(/\//g, "~1") : segment;
|
|
4
|
-
const NON_SCHEMA_KEYWORDS = /* @__PURE__ */ new Set(["enum", "const", "default", "examples"]);
|
|
5
5
|
const buildAnchorMap = (rootSchema) => {
|
|
6
6
|
const map = /* @__PURE__ */ Object.create(null);
|
|
7
7
|
if (!isSchemaObject(rootSchema))
|
|
8
8
|
return map;
|
|
9
|
-
const walk = (node, pointer, depth) => {
|
|
9
|
+
const walk = (node, pointer, depth, inSchemaMap) => {
|
|
10
10
|
assertSchemaDepth(depth, "buildAnchorMap");
|
|
11
11
|
if (node === null || typeof node !== "object")
|
|
12
12
|
return;
|
|
13
13
|
if (Array.isArray(node)) {
|
|
14
14
|
for (let i = 0; i < node.length; i++)
|
|
15
|
-
walk(node[i], `${pointer}/${i}`, depth + 1);
|
|
15
|
+
walk(node[i], `${pointer}/${i}`, depth + 1, inSchemaMap);
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
18
|
const record = node;
|
|
19
19
|
for (const keyword of ["$anchor", "$dynamicAnchor"]) {
|
|
20
|
-
const anchor = record
|
|
20
|
+
const anchor = readKey(record, keyword);
|
|
21
21
|
if (typeof anchor === "string" && !Object.hasOwn(map, anchor))
|
|
22
22
|
map[anchor] = `#${pointer}`;
|
|
23
23
|
}
|
|
24
24
|
for (const key of Object.keys(record)) {
|
|
25
|
-
if (
|
|
25
|
+
if (isDataPosition(key, inSchemaMap))
|
|
26
26
|
continue;
|
|
27
|
-
walk(record[key], `${pointer}/${
|
|
27
|
+
walk(record[key], `${pointer}/${escapePointerSegment(key)}`, depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
28
28
|
}
|
|
29
29
|
};
|
|
30
|
-
walk(rootSchema, "", 0);
|
|
30
|
+
walk(rootSchema, "", 0, false);
|
|
31
31
|
return map;
|
|
32
32
|
};
|
|
33
33
|
export {
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
+
import { entersSchemaMap, escapePointerSegment, isDataPosition } from "./build-resource-registry.js";
|
|
1
2
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
3
|
+
import { readKey } from "./read-key.js";
|
|
2
4
|
import { isSchemaObject } from "./schema-guards.js";
|
|
3
|
-
const escapeSegment = (segment) => segment.indexOf("~") !== -1 || segment.indexOf("/") !== -1 ? segment.replace(/~/g, "~0").replace(/\//g, "~1") : segment;
|
|
4
|
-
const NON_SCHEMA_KEYWORDS = /* @__PURE__ */ new Set(["enum", "const", "default", "examples"]);
|
|
5
5
|
const buildDynamicRefMap = (rootSchema) => {
|
|
6
|
-
const map =
|
|
6
|
+
const map = /* @__PURE__ */ Object.create(null);
|
|
7
7
|
if (!isSchemaObject(rootSchema))
|
|
8
8
|
return map;
|
|
9
|
-
const walk = (node, pointer, depth) => {
|
|
9
|
+
const walk = (node, pointer, depth, inSchemaMap) => {
|
|
10
10
|
assertSchemaDepth(depth, "buildDynamicRefMap");
|
|
11
11
|
if (node === null || typeof node !== "object")
|
|
12
12
|
return;
|
|
13
13
|
if (Array.isArray(node)) {
|
|
14
14
|
for (let i = 0; i < node.length; i++)
|
|
15
|
-
walk(node[i], `${pointer}/${i}`, depth + 1);
|
|
15
|
+
walk(node[i], `${pointer}/${i}`, depth + 1, inSchemaMap);
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
18
|
const record = node;
|
|
19
|
-
const anchor = record
|
|
20
|
-
if (typeof anchor === "string" && !(`#${anchor}`
|
|
19
|
+
const anchor = readKey(record, "$dynamicAnchor");
|
|
20
|
+
if (typeof anchor === "string" && !Object.hasOwn(map, `#${anchor}`)) {
|
|
21
21
|
map[`#${anchor}`] = `#${pointer}`;
|
|
22
22
|
}
|
|
23
23
|
for (const key of Object.keys(record)) {
|
|
24
|
-
if (
|
|
24
|
+
if (isDataPosition(key, inSchemaMap))
|
|
25
25
|
continue;
|
|
26
|
-
walk(record[key], `${pointer}/${
|
|
26
|
+
walk(record[key], `${pointer}/${escapePointerSegment(key)}`, depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
|
-
walk(rootSchema, "", 0);
|
|
29
|
+
walk(rootSchema, "", 0, false);
|
|
30
30
|
return map;
|
|
31
31
|
};
|
|
32
32
|
export {
|
|
@@ -54,6 +54,17 @@ export declare const SYNTHETIC_BASE = "https://helpers.mjst.invalid/schema";
|
|
|
54
54
|
* these.
|
|
55
55
|
*/
|
|
56
56
|
export declare const DATA_KEYWORDS: Set<string>;
|
|
57
|
+
/**
|
|
58
|
+
* Keywords whose value is a map of author-chosen names to schemas.
|
|
59
|
+
*
|
|
60
|
+
* The distinction {@link DATA_KEYWORDS} needs to be useful: at a schema node
|
|
61
|
+
* those four are keywords, but inside one of these maps the keys are *names*,
|
|
62
|
+
* so a property genuinely called `default` or `examples` carries no keyword
|
|
63
|
+
* meaning. A walker that skips by key name alone skips that property's whole
|
|
64
|
+
* subtree, which is how a real tuple went unnormalized and a `nullable`
|
|
65
|
+
* property went unfolded.
|
|
66
|
+
*/
|
|
67
|
+
export declare const SCHEMA_MAPS: Set<string>;
|
|
57
68
|
/** `new URL(ref, base).href`, or `undefined` when the pair does not parse. */
|
|
58
69
|
export declare const resolveUri: (ref: string, base: string) => string | undefined;
|
|
59
70
|
/**
|
|
@@ -65,6 +76,49 @@ export declare const resolveUri: (ref: string, base: string) => string | undefin
|
|
|
65
76
|
* literally named `a%2Fb` would otherwise be looked up as `a/b`.
|
|
66
77
|
*/
|
|
67
78
|
export declare const escapePointerSegment: (segment: string) => string;
|
|
79
|
+
/**
|
|
80
|
+
* Whether `key` holds instance data rather than a schema, at a node whose own
|
|
81
|
+
* position is `inSchemaMap`.
|
|
82
|
+
*
|
|
83
|
+
* A schema walk has two kinds of object. At a **schema node** the keys are
|
|
84
|
+
* keywords, so {@link DATA_KEYWORDS} mark subtrees holding values the schema
|
|
85
|
+
* describes, which must not be read as schemas. Inside a {@link SCHEMA_MAPS}
|
|
86
|
+
* entry the keys are author-chosen **names**, where those same words carry no
|
|
87
|
+
* keyword meaning — a definition or property genuinely called `default` is
|
|
88
|
+
* ordinary.
|
|
89
|
+
*
|
|
90
|
+
* Every walker here used to test the key name alone, which conflated the two:
|
|
91
|
+
* a `$defs.default` had its whole subtree skipped, so an `$anchor` inside it
|
|
92
|
+
* never registered and the `$ref` naming it failed to resolve. Threading one
|
|
93
|
+
* boolean through the recursion is what tells them apart, and asking these two
|
|
94
|
+
* functions is what keeps the walkers agreeing about it.
|
|
95
|
+
*
|
|
96
|
+
* Two predicates rather than a generator over `{key, value, inSchemaMap}`
|
|
97
|
+
* objects: these are the inner loop of ten whole-document walkers, several of
|
|
98
|
+
* which document that they allocate nothing per node.
|
|
99
|
+
*
|
|
100
|
+
* Arrays are the caller's to handle, and the rule there is that **an element
|
|
101
|
+
* inherits its array's position**, the same one `resolve-refs`' `childRole`
|
|
102
|
+
* applies.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const walk = (node: unknown, inSchemaMap: boolean): void => {
|
|
107
|
+
* if (Array.isArray(node)) {
|
|
108
|
+
* for (const item of node) walk(item, inSchemaMap)
|
|
109
|
+
* return
|
|
110
|
+
* }
|
|
111
|
+
* if (!isObject(node)) return
|
|
112
|
+
* for (const key of Object.keys(node)) {
|
|
113
|
+
* if (isDataPosition(key, inSchemaMap)) continue
|
|
114
|
+
* walk(node[key], entersSchemaMap(key, inSchemaMap))
|
|
115
|
+
* }
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare const isDataPosition: (key: string, inSchemaMap: boolean) => boolean;
|
|
120
|
+
/** Whether the child at `key` is a map of author-chosen names. See {@link isDataPosition}. */
|
|
121
|
+
export declare const entersSchemaMap: (key: string, inSchemaMap: boolean) => boolean;
|
|
68
122
|
/**
|
|
69
123
|
* The base URI a subschema establishes through its `$id`, or the enclosing base
|
|
70
124
|
* when it declares none. A draft-07-style `$id: "#name"` resolves to a bare
|
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
2
|
+
import { readKey } from "./read-key.js";
|
|
2
3
|
const SYNTHETIC_BASE = "https://helpers.mjst.invalid/schema";
|
|
3
|
-
const DATA_KEYWORDS = /* @__PURE__ */ new Set([
|
|
4
|
+
const DATA_KEYWORDS = /* @__PURE__ */ new Set([
|
|
5
|
+
"enum",
|
|
6
|
+
"const",
|
|
7
|
+
"default",
|
|
8
|
+
"examples",
|
|
9
|
+
// OpenAPI 3.0's singular spelling. `foldNullable` runs specifically on 3.0
|
|
10
|
+
// documents, and the adapters take TypeBox/OpenAPI-derived input, so an
|
|
11
|
+
// `example` value was being walked as though it were a schema.
|
|
12
|
+
"example"
|
|
13
|
+
]);
|
|
14
|
+
const SCHEMA_MAPS = /* @__PURE__ */ new Set([
|
|
15
|
+
"properties",
|
|
16
|
+
"patternProperties",
|
|
17
|
+
"$defs",
|
|
18
|
+
"definitions",
|
|
19
|
+
"dependentSchemas",
|
|
20
|
+
// Draft-07's `dependencies`: its keys are trigger property names too.
|
|
21
|
+
"dependencies"
|
|
22
|
+
]);
|
|
4
23
|
const resolveUri = (ref, base) => {
|
|
5
24
|
try {
|
|
6
25
|
return new URL(ref, base).href;
|
|
@@ -9,8 +28,10 @@ const resolveUri = (ref, base) => {
|
|
|
9
28
|
}
|
|
10
29
|
};
|
|
11
30
|
const escapePointerSegment = (segment) => segment.indexOf("~") === -1 && segment.indexOf("/") === -1 && segment.indexOf("%") === -1 ? segment : segment.replace(/~/g, "~0").replace(/\//g, "~1").replace(/%/g, "%25");
|
|
31
|
+
const isDataPosition = (key, inSchemaMap) => !inSchemaMap && DATA_KEYWORDS.has(key);
|
|
32
|
+
const entersSchemaMap = (key, inSchemaMap) => !inSchemaMap && SCHEMA_MAPS.has(key);
|
|
12
33
|
const baseAfterId = (node, enclosing) => {
|
|
13
|
-
const id = node
|
|
34
|
+
const id = readKey(node, "$id");
|
|
14
35
|
if (typeof id !== "string" || id === "")
|
|
15
36
|
return enclosing;
|
|
16
37
|
const resolved = resolveUri(id, enclosing);
|
|
@@ -24,21 +45,20 @@ const registries = /* @__PURE__ */ new WeakMap();
|
|
|
24
45
|
const buildResourceRegistry = (root) => {
|
|
25
46
|
if (root === null || typeof root !== "object")
|
|
26
47
|
return null;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return memoized ?? null;
|
|
48
|
+
if (registries.has(root))
|
|
49
|
+
return registries.get(root) ?? null;
|
|
30
50
|
const resources = /* @__PURE__ */ new Map();
|
|
31
51
|
const anchors = /* @__PURE__ */ new Map();
|
|
32
52
|
const dynamicAnchors = /* @__PURE__ */ new Map();
|
|
33
53
|
const baseAt = /* @__PURE__ */ new Map();
|
|
34
54
|
let sawId = false;
|
|
35
|
-
const walk = (node, pointer, enclosing, depth) => {
|
|
55
|
+
const walk = (node, pointer, enclosing, depth, inSchemaMap) => {
|
|
36
56
|
assertSchemaDepth(depth, "buildResourceRegistry");
|
|
37
57
|
if (node === null || typeof node !== "object")
|
|
38
58
|
return;
|
|
39
59
|
if (Array.isArray(node)) {
|
|
40
60
|
for (let index = 0; index < node.length; index++)
|
|
41
|
-
walk(node[index], `${pointer}/${index}`, enclosing, depth + 1);
|
|
61
|
+
walk(node[index], `${pointer}/${index}`, enclosing, depth + 1, inSchemaMap);
|
|
42
62
|
return;
|
|
43
63
|
}
|
|
44
64
|
const record = node;
|
|
@@ -49,10 +69,10 @@ const buildResourceRegistry = (root) => {
|
|
|
49
69
|
if (!resources.has(base))
|
|
50
70
|
resources.set(base, pointer);
|
|
51
71
|
}
|
|
52
|
-
const anchor = record
|
|
72
|
+
const anchor = readKey(record, "$anchor");
|
|
53
73
|
if (typeof anchor === "string" && !anchors.has(`${base}#${anchor}`))
|
|
54
74
|
anchors.set(`${base}#${anchor}`, pointer);
|
|
55
|
-
const dynamicAnchor = record
|
|
75
|
+
const dynamicAnchor = readKey(record, "$dynamicAnchor");
|
|
56
76
|
if (typeof dynamicAnchor === "string") {
|
|
57
77
|
const key = `${base}#${dynamicAnchor}`;
|
|
58
78
|
if (!dynamicAnchors.has(key))
|
|
@@ -61,12 +81,12 @@ const buildResourceRegistry = (root) => {
|
|
|
61
81
|
anchors.set(key, pointer);
|
|
62
82
|
}
|
|
63
83
|
for (const key of Object.keys(record)) {
|
|
64
|
-
if (
|
|
84
|
+
if (isDataPosition(key, inSchemaMap))
|
|
65
85
|
continue;
|
|
66
|
-
walk(record[key], `${pointer}/${escapePointerSegment(key)}`, base, depth + 1);
|
|
86
|
+
walk(record[key], `${pointer}/${escapePointerSegment(key)}`, base, depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
67
87
|
}
|
|
68
88
|
};
|
|
69
|
-
walk(root, "", SYNTHETIC_BASE, 0);
|
|
89
|
+
walk(root, "", SYNTHETIC_BASE, 0, false);
|
|
70
90
|
if (!sawId) {
|
|
71
91
|
registries.set(root, null);
|
|
72
92
|
return null;
|
|
@@ -82,9 +102,12 @@ const buildResourceRegistry = (root) => {
|
|
|
82
102
|
};
|
|
83
103
|
export {
|
|
84
104
|
DATA_KEYWORDS,
|
|
105
|
+
SCHEMA_MAPS,
|
|
85
106
|
SYNTHETIC_BASE,
|
|
86
107
|
baseAfterId,
|
|
87
108
|
buildResourceRegistry,
|
|
109
|
+
entersSchemaMap,
|
|
88
110
|
escapePointerSegment,
|
|
111
|
+
isDataPosition,
|
|
89
112
|
resolveUri
|
|
90
113
|
};
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { isObject } from "./is-object.js";
|
|
2
|
+
import { readKey } from "./read-key.js";
|
|
3
|
+
import { isIdentifierStart, toPascalWords } from "./ref-to-name.js";
|
|
2
4
|
const titleToPascalCase = (title) => {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
const pascal = toPascalWords(title).replace(/^\d+/, "");
|
|
6
|
+
if (pascal === "" || isIdentifierStart(pascal))
|
|
7
|
+
return pascal;
|
|
8
|
+
return `_${pascal}`;
|
|
6
9
|
};
|
|
7
10
|
const deriveRootTypeName = (schema, fallbackName) => {
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
const title = isObject(schema) ? readKey(schema, "title") : void 0;
|
|
12
|
+
if (typeof title === "string") {
|
|
13
|
+
const fromTitle = titleToPascalCase(title);
|
|
10
14
|
if (fromTitle)
|
|
11
15
|
return fromTitle;
|
|
12
16
|
}
|
|
@@ -8,52 +8,59 @@ const lineTerminatorEscapes = {
|
|
|
8
8
|
8233: "\\u2029"
|
|
9
9
|
// PARAGRAPH SEPARATOR
|
|
10
10
|
};
|
|
11
|
+
const unicodeEscape = (code) => `\\u${code.toString(16).padStart(4, "0")}`;
|
|
11
12
|
const escapeCache = /* @__PURE__ */ new Map();
|
|
12
|
-
const
|
|
13
|
+
const modeCache = /* @__PURE__ */ new Map();
|
|
13
14
|
const ESCAPE_CACHE_LIMIT = 1e3;
|
|
14
|
-
const
|
|
15
|
-
|
|
15
|
+
const evictIfFull = (cache) => {
|
|
16
|
+
if (cache.size >= ESCAPE_CACHE_LIMIT)
|
|
17
|
+
cache.delete(cache.keys().next().value);
|
|
18
|
+
};
|
|
19
|
+
const compileMode = (pattern) => {
|
|
20
|
+
const cached = modeCache.get(pattern);
|
|
16
21
|
if (cached !== void 0)
|
|
17
22
|
return cached;
|
|
23
|
+
let mode;
|
|
18
24
|
try {
|
|
19
|
-
new RegExp(pattern);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return terminatorEscape;
|
|
29
|
-
return match;
|
|
30
|
-
});
|
|
31
|
-
if (escaped === "") {
|
|
32
|
-
escapeCache.set(pattern, "(?:)");
|
|
33
|
-
return "(?:)";
|
|
34
|
-
}
|
|
35
|
-
if (escapeCache.size >= ESCAPE_CACHE_LIMIT) {
|
|
36
|
-
escapeCache.delete(escapeCache.keys().next().value);
|
|
25
|
+
new RegExp(pattern, "u");
|
|
26
|
+
mode = "u";
|
|
27
|
+
} catch {
|
|
28
|
+
try {
|
|
29
|
+
new RegExp(pattern);
|
|
30
|
+
mode = "";
|
|
31
|
+
} catch {
|
|
32
|
+
mode = null;
|
|
33
|
+
}
|
|
37
34
|
}
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
evictIfFull(modeCache);
|
|
36
|
+
modeCache.set(pattern, mode);
|
|
37
|
+
return mode;
|
|
40
38
|
};
|
|
41
|
-
const
|
|
42
|
-
const cached =
|
|
39
|
+
const escapeRegexPattern = (pattern) => {
|
|
40
|
+
const cached = escapeCache.get(pattern);
|
|
43
41
|
if (cached !== void 0)
|
|
44
42
|
return cached;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
if (compileMode(pattern) === null) {
|
|
44
|
+
try {
|
|
45
|
+
new RegExp(pattern, "u");
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw new Error(`Invalid regex pattern ${JSON.stringify(pattern)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
}
|
|
51
49
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
const escaped = pattern.replace(/\\[\s\S]|[/\n\r\u2028\u2029]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, (match) => {
|
|
51
|
+
if (match.length !== 1)
|
|
52
|
+
return match;
|
|
53
|
+
if (match === "/")
|
|
54
|
+
return "\\/";
|
|
55
|
+
const code = match.charCodeAt(0);
|
|
56
|
+
return lineTerminatorEscapes[code] ?? unicodeEscape(code);
|
|
57
|
+
});
|
|
58
|
+
const result = escaped === "" ? "(?:)" : escaped;
|
|
59
|
+
evictIfFull(escapeCache);
|
|
60
|
+
escapeCache.set(pattern, result);
|
|
61
|
+
return result;
|
|
56
62
|
};
|
|
63
|
+
const regexFlagsFor = (pattern) => compileMode(pattern) ?? "";
|
|
57
64
|
const regexLiteral = (pattern) => `/${escapeRegexPattern(pattern)}/${regexFlagsFor(pattern)}`;
|
|
58
65
|
export {
|
|
59
66
|
escapeRegexPattern,
|
package/dist/extract-refs.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { entersSchemaMap, isDataPosition } from "./build-resource-registry.js";
|
|
1
2
|
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
3
|
+
import { readKey } from "./read-key.js";
|
|
2
4
|
const isResolvableRef = (ref) => {
|
|
3
5
|
if (ref === "#")
|
|
4
6
|
return false;
|
|
@@ -10,23 +12,26 @@ const isResolvableRef = (ref) => {
|
|
|
10
12
|
};
|
|
11
13
|
const extractRefs = (schema) => {
|
|
12
14
|
const refs = /* @__PURE__ */ new Set();
|
|
13
|
-
const traverse = (obj, depth) => {
|
|
15
|
+
const traverse = (obj, depth, inSchemaMap = false) => {
|
|
14
16
|
assertSchemaDepth(depth, "extractRefs");
|
|
15
17
|
if (typeof obj !== "object" || obj === null) {
|
|
16
18
|
return;
|
|
17
19
|
}
|
|
18
20
|
if (Array.isArray(obj)) {
|
|
19
21
|
for (const item of obj) {
|
|
20
|
-
traverse(item, depth + 1);
|
|
22
|
+
traverse(item, depth + 1, inSchemaMap);
|
|
21
23
|
}
|
|
22
24
|
return;
|
|
23
25
|
}
|
|
24
26
|
const record = obj;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
const ref = inSchemaMap ? void 0 : readKey(record, "$ref");
|
|
28
|
+
if (typeof ref === "string" && isResolvableRef(ref)) {
|
|
29
|
+
refs.add(ref);
|
|
27
30
|
}
|
|
28
|
-
for (const key
|
|
29
|
-
|
|
31
|
+
for (const key of Object.keys(record)) {
|
|
32
|
+
if (isDataPosition(key, inSchemaMap))
|
|
33
|
+
continue;
|
|
34
|
+
traverse(record[key], depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
30
35
|
}
|
|
31
36
|
};
|
|
32
37
|
traverse(schema, 0);
|
package/dist/fold-nullable.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import { assignKey } from "./assign-key.js";
|
|
2
|
+
import { entersSchemaMap, isDataPosition } from "./build-resource-registry.js";
|
|
3
|
+
import { assertSchemaDepth } from "./max-schema-depth.js";
|
|
1
4
|
const foldNullable = (schema) => {
|
|
2
|
-
const folded = fold(schema);
|
|
5
|
+
const folded = fold(schema, 0);
|
|
3
6
|
return folded;
|
|
4
7
|
};
|
|
5
|
-
const fold = (node) => {
|
|
8
|
+
const fold = (node, depth, inSchemaMap = false) => {
|
|
9
|
+
assertSchemaDepth(depth, "foldNullable");
|
|
6
10
|
if (Array.isArray(node)) {
|
|
7
11
|
let changed2 = false;
|
|
8
12
|
const next2 = node.map((item) => {
|
|
9
|
-
const folded = fold(item);
|
|
13
|
+
const folded = fold(item, depth + 1, inSchemaMap);
|
|
10
14
|
if (folded !== item)
|
|
11
15
|
changed2 = true;
|
|
12
16
|
return folded;
|
|
@@ -18,15 +22,18 @@ const fold = (node) => {
|
|
|
18
22
|
const record = node;
|
|
19
23
|
let changed = false;
|
|
20
24
|
const next = {};
|
|
21
|
-
for (const key
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
+
for (const [key, value] of Object.entries(record))
|
|
26
|
+
assignKey(next, key, value);
|
|
27
|
+
for (const key of Object.keys(record)) {
|
|
28
|
+
if (isDataPosition(key, inSchemaMap))
|
|
29
|
+
continue;
|
|
30
|
+
const folded = fold(record[key], depth + 1, entersSchemaMap(key, inSchemaMap));
|
|
31
|
+
if (folded !== record[key])
|
|
25
32
|
changed = true;
|
|
26
|
-
next
|
|
33
|
+
assignKey(next, key, folded);
|
|
27
34
|
}
|
|
28
|
-
const type = record["type"];
|
|
29
|
-
if (record["nullable"] === true) {
|
|
35
|
+
const type = Object.hasOwn(record, "type") ? record["type"] : void 0;
|
|
36
|
+
if (!inSchemaMap && Object.hasOwn(record, "nullable") && record["nullable"] === true) {
|
|
30
37
|
if (typeof type === "string" && type !== "null") {
|
|
31
38
|
next["type"] = [type, "null"];
|
|
32
39
|
changed = true;
|
|
@@ -19,25 +19,21 @@ const exportNameAt = (content, at, prefix) => {
|
|
|
19
19
|
return end > at + prefix.length ? content.slice(at + prefix.length, end) : null;
|
|
20
20
|
};
|
|
21
21
|
const isLineTerminator = (code) => code === 10 || code === 13 || code === 8232 || code === 8233;
|
|
22
|
+
const EXPORT_KEYWORD = "export ";
|
|
22
23
|
const collectExportNames = (content, typeNames, constNames) => {
|
|
23
|
-
let
|
|
24
|
-
while (
|
|
25
|
-
if (
|
|
26
|
-
const typeName = exportNameAt(content,
|
|
24
|
+
let at = content.indexOf(EXPORT_KEYWORD);
|
|
25
|
+
while (at !== -1) {
|
|
26
|
+
if (at === 0 || isLineTerminator(content.charCodeAt(at - 1))) {
|
|
27
|
+
const typeName = exportNameAt(content, at, "export type ");
|
|
27
28
|
if (typeName !== null) {
|
|
28
29
|
typeNames.push(typeName);
|
|
29
30
|
} else {
|
|
30
|
-
const constName = exportNameAt(content,
|
|
31
|
+
const constName = exportNameAt(content, at, "export const ");
|
|
31
32
|
if (constName !== null)
|
|
32
33
|
constNames.push(constName);
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
while (next < content.length && !isLineTerminator(content.charCodeAt(next)))
|
|
37
|
-
next++;
|
|
38
|
-
if (next >= content.length)
|
|
39
|
-
break;
|
|
40
|
-
lineStart = content.charCodeAt(next) === 13 && content.charCodeAt(next + 1) === 10 ? next + 2 : next + 1;
|
|
36
|
+
at = content.indexOf(EXPORT_KEYWORD, at + EXPORT_KEYWORD.length);
|
|
41
37
|
}
|
|
42
38
|
};
|
|
43
39
|
const generateIndexBarrel = (files, options = {}) => {
|