@amritk/helpers 0.13.1 → 0.13.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/LICENSE +21 -0
- package/dist/build-dynamic-ref-map.js +30 -53
- package/dist/derive-root-type-name.js +19 -49
- package/dist/escape-regex-pattern.js +39 -80
- package/dist/extract-dynamic-anchor-defs.js +5 -22
- package/dist/extract-refs.js +31 -63
- package/dist/generate-index-barrel.js +53 -84
- package/dist/generate-type-definition.js +372 -458
- package/dist/has-ref.js +5 -7
- package/dist/is-object.js +4 -14
- package/dist/mjst-extension.js +25 -44
- package/dist/multiple-of-check.js +12 -26
- package/dist/parse-documentation.js +86 -128
- package/dist/quote-js-string.js +4 -18
- package/dist/ref-to-filename.js +40 -105
- package/dist/ref-to-name.js +17 -53
- package/dist/resolve-dynamic-refs.js +47 -61
- package/dist/resolve-ref.js +37 -73
- package/dist/safe-accessor.js +18 -45
- package/dist/schema-guards.js +158 -175
- package/dist/unknown-key-check.js +19 -37
- package/dist/upgrade-draft07-schema.js +86 -149
- package/dist/validate-array.js +20 -31
- package/dist/validate-record.js +18 -24
- package/dist/walk-ref-graph.js +113 -158
- package/package.json +3 -3
|
@@ -1,69 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return schema;
|
|
1
|
+
const resolveDynamicRefs = (schema, dynamicRefMap) => {
|
|
2
|
+
if (typeof schema !== "object" || schema === null) {
|
|
3
|
+
return schema;
|
|
4
|
+
}
|
|
5
|
+
if (Object.keys(dynamicRefMap).length === 0) {
|
|
6
|
+
return schema;
|
|
7
|
+
}
|
|
8
|
+
if (!containsDynamicRef(schema)) {
|
|
9
|
+
return schema;
|
|
10
|
+
}
|
|
11
|
+
const clone = JSON.parse(JSON.stringify(schema));
|
|
12
|
+
const walk = (obj) => {
|
|
13
|
+
if (typeof obj !== "object" || obj === null) {
|
|
14
|
+
return;
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
if (Array.isArray(obj)) {
|
|
17
|
+
for (const item of obj)
|
|
18
|
+
walk(item);
|
|
19
|
+
return;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
const record = obj;
|
|
22
|
+
if ("$dynamicRef" in record && typeof record["$dynamicRef"] === "string") {
|
|
23
|
+
const resolved = dynamicRefMap[record["$dynamicRef"]];
|
|
24
|
+
if (resolved) {
|
|
25
|
+
record["$ref"] = resolved;
|
|
26
|
+
delete record["$dynamicRef"];
|
|
27
|
+
}
|
|
26
28
|
}
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
for (const item of obj)
|
|
34
|
-
walk(item);
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const record = obj;
|
|
38
|
-
if ('$dynamicRef' in record && typeof record['$dynamicRef'] === 'string') {
|
|
39
|
-
const resolved = dynamicRefMap[record['$dynamicRef']];
|
|
40
|
-
if (resolved) {
|
|
41
|
-
record['$ref'] = resolved;
|
|
42
|
-
delete record['$dynamicRef'];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
for (const key in record) {
|
|
46
|
-
walk(record[key]);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
walk(clone);
|
|
50
|
-
return clone;
|
|
29
|
+
for (const key in record) {
|
|
30
|
+
walk(record[key]);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
walk(clone);
|
|
34
|
+
return clone;
|
|
51
35
|
};
|
|
52
|
-
/** True if `value` contains a `$dynamicRef` string anywhere in its subtree. */
|
|
53
36
|
const containsDynamicRef = (value) => {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return true;
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
const record = value;
|
|
63
|
-
if (typeof record['$dynamicRef'] === 'string')
|
|
37
|
+
if (typeof value !== "object" || value === null)
|
|
38
|
+
return false;
|
|
39
|
+
if (Array.isArray(value)) {
|
|
40
|
+
for (const item of value)
|
|
41
|
+
if (containsDynamicRef(item))
|
|
64
42
|
return true;
|
|
65
|
-
for (const key in record)
|
|
66
|
-
if (containsDynamicRef(record[key]))
|
|
67
|
-
return true;
|
|
68
43
|
return false;
|
|
44
|
+
}
|
|
45
|
+
const record = value;
|
|
46
|
+
if (typeof record["$dynamicRef"] === "string")
|
|
47
|
+
return true;
|
|
48
|
+
for (const key in record)
|
|
49
|
+
if (containsDynamicRef(record[key]))
|
|
50
|
+
return true;
|
|
51
|
+
return false;
|
|
52
|
+
};
|
|
53
|
+
export {
|
|
54
|
+
resolveDynamicRefs
|
|
69
55
|
};
|
package/dist/resolve-ref.js
CHANGED
|
@@ -1,77 +1,41 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Navigates a JSON Pointer fragment (e.g. `/$defs/foo` or `/definitions/bar`)
|
|
3
|
-
* through a schema object, returning the target or undefined if not found.
|
|
4
|
-
*/
|
|
5
1
|
const navigatePointer = (pointer, schema) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
else {
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
2
|
+
const parts = pointer.split("/").filter(Boolean);
|
|
3
|
+
let current = schema;
|
|
4
|
+
for (const part of parts) {
|
|
5
|
+
const decodedPart = part.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
6
|
+
if (current && typeof current === "object" && decodedPart in current) {
|
|
7
|
+
const next = current[decodedPart];
|
|
8
|
+
if (typeof next === "object" && next !== null) {
|
|
9
|
+
current = next;
|
|
10
|
+
} else {
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
} else {
|
|
14
|
+
return void 0;
|
|
22
15
|
}
|
|
23
|
-
|
|
16
|
+
}
|
|
17
|
+
return current;
|
|
24
18
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
* resolveRef('http://example.com/server.json', rootSchema)
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export const resolveRef = (ref, rootSchema) => {
|
|
51
|
-
// Internal reference: navigate from root by JSON Pointer
|
|
52
|
-
if (ref.startsWith('#')) {
|
|
53
|
-
return navigatePointer(ref.slice(1), rootSchema);
|
|
54
|
-
}
|
|
55
|
-
// URI ref: may have a fragment (e.g. "http://foo.com/bar.json#/definitions/baz")
|
|
56
|
-
// A trailing bare "#" (e.g. "http://foo.com/schema#") means "the whole document" — treat as no fragment
|
|
57
|
-
const hashIndex = ref.indexOf('#');
|
|
58
|
-
const baseUri = hashIndex === -1 ? ref : ref.slice(0, hashIndex);
|
|
59
|
-
const rawFragment = hashIndex === -1 ? '' : ref.slice(hashIndex + 1);
|
|
60
|
-
const fragment = rawFragment === '' || rawFragment === '/' ? '' : rawFragment;
|
|
61
|
-
// Look up the base URI as a key in $defs
|
|
62
|
-
const defs = rootSchema['$defs'];
|
|
63
|
-
if (typeof defs !== 'object' || defs === null)
|
|
64
|
-
return undefined;
|
|
65
|
-
const defsRecord = defs;
|
|
66
|
-
const base = defsRecord[baseUri];
|
|
67
|
-
if (typeof base !== 'object' || base === null)
|
|
68
|
-
return undefined;
|
|
69
|
-
// No fragment — return the definition directly
|
|
70
|
-
if (!fragment)
|
|
71
|
-
return base;
|
|
72
|
-
// Normalize the fragment: draft-07 schemas use `/definitions/` but after
|
|
73
|
-
// upgradeDraft07Schema the key is renamed to `/$defs/`
|
|
74
|
-
const normalizedFragment = fragment.replace(/^\/definitions\//, '/$defs/');
|
|
75
|
-
// Navigate the fragment within the resolved definition
|
|
76
|
-
return navigatePointer(normalizedFragment, base);
|
|
19
|
+
const resolveRef = (ref, rootSchema) => {
|
|
20
|
+
if (ref.startsWith("#")) {
|
|
21
|
+
return navigatePointer(ref.slice(1), rootSchema);
|
|
22
|
+
}
|
|
23
|
+
const hashIndex = ref.indexOf("#");
|
|
24
|
+
const baseUri = hashIndex === -1 ? ref : ref.slice(0, hashIndex);
|
|
25
|
+
const rawFragment = hashIndex === -1 ? "" : ref.slice(hashIndex + 1);
|
|
26
|
+
const fragment = rawFragment === "" || rawFragment === "/" ? "" : rawFragment;
|
|
27
|
+
const defs = rootSchema["$defs"];
|
|
28
|
+
if (typeof defs !== "object" || defs === null)
|
|
29
|
+
return void 0;
|
|
30
|
+
const defsRecord = defs;
|
|
31
|
+
const base = defsRecord[baseUri];
|
|
32
|
+
if (typeof base !== "object" || base === null)
|
|
33
|
+
return void 0;
|
|
34
|
+
if (!fragment)
|
|
35
|
+
return base;
|
|
36
|
+
const normalizedFragment = fragment.replace(/^\/definitions\//, "/$defs/");
|
|
37
|
+
return navigatePointer(normalizedFragment, base);
|
|
38
|
+
};
|
|
39
|
+
export {
|
|
40
|
+
resolveRef
|
|
77
41
|
};
|
package/dist/safe-accessor.js
CHANGED
|
@@ -1,48 +1,21 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks whether a property name is a valid JavaScript identifier that can be
|
|
3
|
-
* accessed with dot notation. Property names containing hyphens, dots, or
|
|
4
|
-
* other special characters (e.g., "x-linkedin") must use bracket notation.
|
|
5
|
-
*/
|
|
6
1
|
const JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* @example
|
|
17
|
-
* safeAccessor("input", "name") // "input.name"
|
|
18
|
-
* safeAccessor("input?", "x-linkedin") // 'input?.["x-linkedin"]'
|
|
19
|
-
* safeAccessor("input", "x-linkedin") // 'input["x-linkedin"]'
|
|
20
|
-
*/
|
|
21
|
-
export const safeAccessor = (variable, key) => {
|
|
22
|
-
if (JS_IDENTIFIER.test(key)) {
|
|
23
|
-
return `${variable}.${key}`;
|
|
24
|
-
}
|
|
25
|
-
// Bracket keys are schema-controlled, so escape via JSON.stringify — a key like
|
|
26
|
-
// `it's` or `a']; evil(); //` would otherwise break or hijack the generated code.
|
|
27
|
-
const literal = JSON.stringify(key);
|
|
28
|
-
// Handle optional chaining: "input?" -> "input?.['key']"
|
|
29
|
-
if (variable.endsWith('?')) {
|
|
30
|
-
return `${variable}.[${literal}]`;
|
|
31
|
-
}
|
|
32
|
-
return `${variable}[${literal}]`;
|
|
2
|
+
const safeAccessor = (variable, key) => {
|
|
3
|
+
if (JS_IDENTIFIER.test(key)) {
|
|
4
|
+
return `${variable}.${key}`;
|
|
5
|
+
}
|
|
6
|
+
const literal = JSON.stringify(key);
|
|
7
|
+
if (variable.endsWith("?")) {
|
|
8
|
+
return `${variable}.[${literal}]`;
|
|
9
|
+
}
|
|
10
|
+
return `${variable}[${literal}]`;
|
|
33
11
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (JS_IDENTIFIER.test(key)) {
|
|
44
|
-
return key;
|
|
45
|
-
}
|
|
46
|
-
// Schema-controlled keys must be escaped; a bare-quoted `it's` produces broken TS.
|
|
47
|
-
return JSON.stringify(key);
|
|
12
|
+
const safeKey = (key) => {
|
|
13
|
+
if (JS_IDENTIFIER.test(key)) {
|
|
14
|
+
return key;
|
|
15
|
+
}
|
|
16
|
+
return JSON.stringify(key);
|
|
17
|
+
};
|
|
18
|
+
export {
|
|
19
|
+
safeAccessor,
|
|
20
|
+
safeKey
|
|
48
21
|
};
|