@amritk/helpers 0.6.2 → 0.7.1
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/build-dynamic-ref-map.d.ts +1 -0
- package/dist/build-dynamic-ref-map.d.ts.map +1 -0
- package/dist/build-dynamic-ref-map.js +31 -107
- package/dist/derive-root-type-name.d.ts +1 -0
- package/dist/derive-root-type-name.d.ts.map +1 -0
- package/dist/derive-root-type-name.js +37 -17
- package/dist/extract-dynamic-anchor-defs.d.ts +20 -0
- package/dist/extract-dynamic-anchor-defs.d.ts.map +1 -0
- package/dist/extract-dynamic-anchor-defs.js +30 -0
- package/dist/extract-refs.d.ts +1 -0
- package/dist/extract-refs.d.ts.map +1 -0
- package/dist/extract-refs.js +64 -33
- package/dist/generate-index-barrel.d.ts +29 -0
- package/dist/generate-index-barrel.d.ts.map +1 -0
- package/dist/generate-index-barrel.js +43 -0
- package/dist/generate-type-definition.d.ts +1 -0
- package/dist/generate-type-definition.d.ts.map +1 -0
- package/dist/generate-type-definition.js +449 -567
- package/dist/has-ref.d.ts +1 -0
- package/dist/has-ref.d.ts.map +1 -0
- package/dist/has-ref.js +7 -6
- package/dist/is-object.d.ts +1 -0
- package/dist/is-object.d.ts.map +1 -0
- package/dist/is-object.js +14 -5
- package/dist/mjst-extension.d.ts +1 -0
- package/dist/mjst-extension.d.ts.map +1 -0
- package/dist/mjst-extension.js +50 -122
- package/dist/parse-documentation.d.ts +1 -0
- package/dist/parse-documentation.d.ts.map +1 -0
- package/dist/parse-documentation.js +124 -87
- package/dist/ref-to-filename.d.ts +1 -0
- package/dist/ref-to-filename.d.ts.map +1 -0
- package/dist/ref-to-filename.js +106 -42
- package/dist/ref-to-name.d.ts +1 -0
- package/dist/ref-to-name.d.ts.map +1 -0
- package/dist/ref-to-name.js +39 -53
- package/dist/resolve-dynamic-refs.d.ts +1 -0
- package/dist/resolve-dynamic-refs.d.ts.map +1 -0
- package/dist/resolve-dynamic-refs.js +36 -28
- package/dist/resolve-ref.d.ts +1 -0
- package/dist/resolve-ref.d.ts.map +1 -0
- package/dist/resolve-ref.js +74 -39
- package/dist/safe-accessor.d.ts +1 -0
- package/dist/safe-accessor.d.ts.map +1 -0
- package/dist/safe-accessor.js +42 -19
- package/dist/schema-guards.d.ts +2 -1
- package/dist/schema-guards.d.ts.map +1 -0
- package/dist/schema-guards.js +119 -120
- package/dist/upgrade-draft07-schema.d.ts +1 -0
- package/dist/upgrade-draft07-schema.d.ts.map +1 -0
- package/dist/upgrade-draft07-schema.js +152 -130
- package/dist/validate-array.d.ts +1 -0
- package/dist/validate-array.d.ts.map +1 -0
- package/dist/validate-array.js +12 -14
- package/dist/validate-record.d.ts +1 -0
- package/dist/validate-record.d.ts.map +1 -0
- package/dist/validate-record.js +15 -17
- package/dist/walk-ref-graph.d.ts +54 -0
- package/dist/walk-ref-graph.d.ts.map +1 -0
- package/dist/walk-ref-graph.js +123 -0
- package/package.json +9 -88
- package/src/extract-dynamic-anchor-defs.test.ts +29 -0
- package/src/extract-dynamic-anchor-defs.ts +33 -0
- package/src/generate-index-barrel.test.ts +41 -0
- package/src/generate-index-barrel.ts +62 -0
- package/src/walk-ref-graph.test.ts +118 -0
- package/src/walk-ref-graph.ts +184 -0
package/dist/ref-to-filename.js
CHANGED
|
@@ -1,44 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Converts a PascalCase or camelCase string to kebab-case.
|
|
3
|
+
* Handles consecutive uppercase sequences (e.g. "APIKey" → "api-key") and
|
|
4
|
+
* known mixed-case acronyms like "OAuth" that would otherwise split incorrectly.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* toKebabCase('ServerVariable') // 'server-variable'
|
|
9
|
+
* toKebabCase('APIKeySecurityScheme') // 'api-key-security-scheme'
|
|
10
|
+
* toKebabCase('OAuthFlows') // 'oauth-flows'
|
|
11
|
+
* toKebabCase('already-kebab') // 'already-kebab'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export const toKebabCase = (value) => value
|
|
15
|
+
// Collapse known mixed-case acronyms before splitting so they stay together
|
|
16
|
+
.replace(/OAuth/g, 'Oauth')
|
|
17
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2')
|
|
18
|
+
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
|
|
19
|
+
.toLowerCase();
|
|
20
|
+
/**
|
|
21
|
+
* Derives a unique kebab-case filename from a URI ref.
|
|
22
|
+
*
|
|
23
|
+
* For a plain URI (no fragment), uses the path segments after the host,
|
|
24
|
+
* stripping version numbers, `.json` extension, and joining with `-`.
|
|
25
|
+
* For a URI with a fragment, appends the fragment's last path segment.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* uriRefToFilename('http://asyncapi.com/definitions/3.1.0/channel.json')
|
|
30
|
+
* // 'channel'
|
|
31
|
+
* uriRefToFilename('http://asyncapi.com/bindings/kafka/0.5.0/channel.json')
|
|
32
|
+
* // 'kafka-channel-binding'
|
|
33
|
+
* uriRefToFilename('http://asyncapi.com/bindings/sns/0.1.0/channel.json#/definitions/queue')
|
|
34
|
+
* // 'sns-channel-queue'
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
const uriRefToFilename = (uri) => {
|
|
38
|
+
const hashIndex = uri.indexOf('#');
|
|
39
|
+
const baseUri = hashIndex === -1 ? uri : uri.slice(0, hashIndex);
|
|
40
|
+
const fragment = hashIndex === -1 ? '' : uri.slice(hashIndex + 1);
|
|
41
|
+
// Strip protocol + host, remove .json extension
|
|
42
|
+
const withoutProtocol = baseUri.replace(/^https?:\/\/[^/]+\//, '');
|
|
43
|
+
const withoutExt = withoutProtocol.replace(/\.json$/, '');
|
|
44
|
+
// Drop structural/noise segments:
|
|
45
|
+
// - "definitions" and "$defs" container keys
|
|
46
|
+
// - Version numbers that immediately follow "definitions" (e.g. "3.1.0" in "definitions/3.1.0/channel")
|
|
47
|
+
// but NOT version numbers in other positions (e.g. "0.5.0" in "bindings/kafka/0.5.0/channel")
|
|
48
|
+
// since those are needed to disambiguate multiple versions of the same binding
|
|
49
|
+
const rawSegments = withoutExt.split('/');
|
|
50
|
+
const SKIP_KEYS = new Set(['definitions', '$defs']);
|
|
51
|
+
const segments = [];
|
|
52
|
+
for (let i = 0; i < rawSegments.length; i++) {
|
|
53
|
+
const s = rawSegments[i];
|
|
54
|
+
if (SKIP_KEYS.has(s))
|
|
55
|
+
continue;
|
|
56
|
+
// Skip a version segment only if the previous (non-skipped) segment was "definitions"
|
|
57
|
+
const prevRaw = rawSegments[i - 1];
|
|
58
|
+
if (/^\d+\.\d+/.test(s) && prevRaw !== undefined && SKIP_KEYS.has(prevRaw))
|
|
59
|
+
continue;
|
|
60
|
+
segments.push(s);
|
|
61
|
+
}
|
|
62
|
+
// Join remaining segments, converting to kebab-case and replacing dots with dashes
|
|
63
|
+
const baseName = segments.map((s) => toKebabCase(s).replace(/\./g, '-')).join('-');
|
|
64
|
+
if (!fragment)
|
|
65
|
+
return baseName;
|
|
66
|
+
// Append the last meaningful segment of the fragment, skipping structural keys
|
|
67
|
+
const fragSegments = fragment.split('/').filter((s) => s && !SKIP_KEYS.has(s) && s !== 'properties');
|
|
68
|
+
const fragLast = fragSegments[fragSegments.length - 1];
|
|
69
|
+
if (!fragLast)
|
|
70
|
+
return baseName;
|
|
71
|
+
return `${baseName}-${toKebabCase(fragLast)}`;
|
|
29
72
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Converts a JSON Schema $ref to a filename.
|
|
75
|
+
*
|
|
76
|
+
* Handles three ref forms:
|
|
77
|
+
* - Internal `#/$defs/contact` → `contact`
|
|
78
|
+
* - Internal `#/definitions/ServerVariable` → `server-variable`
|
|
79
|
+
* - URI `http://example.com/definitions/3.1.0/channel.json` → `channel`
|
|
80
|
+
* - URI with fragment `http://example.com/channel.json#/definitions/queue` → `channel-queue`
|
|
81
|
+
*
|
|
82
|
+
* @param ref - The $ref string
|
|
83
|
+
* @returns The filename without extension
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* refToFilename('#/$defs/contact') // 'contact'
|
|
88
|
+
* refToFilename('#/$defs/server-variable') // 'server-variable'
|
|
89
|
+
* refToFilename('#/definitions/ServerVariable') // 'server-variable'
|
|
90
|
+
* refToFilename('#/definitions/APIKeySecurityScheme') // 'api-key-security-scheme'
|
|
91
|
+
* refToFilename('http://asyncapi.com/definitions/3.1.0/channel.json') // 'channel'
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export const refToFilename = (ref) => {
|
|
95
|
+
// URI ref — derive name from URI path
|
|
96
|
+
if (ref.startsWith('http://') || ref.startsWith('https://')) {
|
|
97
|
+
return uriRefToFilename(ref);
|
|
98
|
+
}
|
|
99
|
+
// Internal ref — extract the last segment after the last /
|
|
100
|
+
const segments = ref.split('/');
|
|
101
|
+
// Non-null assertion is safe here: split always returns at least one element
|
|
102
|
+
let filename = segments[segments.length - 1];
|
|
103
|
+
// Normalise PascalCase/camelCase keys (e.g. from draft-07 "definitions") to kebab-case
|
|
104
|
+
if (/[A-Z]/.test(filename)) {
|
|
105
|
+
filename = toKebabCase(filename);
|
|
106
|
+
}
|
|
107
|
+
return filename;
|
|
44
108
|
};
|
package/dist/ref-to-name.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ref-to-name.d.ts","sourceRoot":"","sources":["../src/ref-to-name.ts"],"names":[],"mappings":"AAqBA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,SAAS,QAAS,MAAM,sBAAgB,MAAmD,CAAA"}
|
package/dist/ref-to-name.js
CHANGED
|
@@ -1,54 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
segments.push(s);
|
|
20
|
-
}
|
|
21
|
-
const baseName = segments.map((s) => toKebabCase(s).replace(/\./g, "-")).join("-");
|
|
22
|
-
if (!fragment)
|
|
23
|
-
return baseName;
|
|
24
|
-
const fragSegments = fragment.split("/").filter((s) => s && !SKIP_KEYS.has(s) && s !== "properties");
|
|
25
|
-
const fragLast = fragSegments[fragSegments.length - 1];
|
|
26
|
-
if (!fragLast)
|
|
27
|
-
return baseName;
|
|
28
|
-
return `${baseName}-${toKebabCase(fragLast)}`;
|
|
29
|
-
};
|
|
30
|
-
var refToFilename = (ref) => {
|
|
31
|
-
if (ref.startsWith("http://") || ref.startsWith("https://")) {
|
|
32
|
-
return uriRefToFilename(ref);
|
|
33
|
-
}
|
|
34
|
-
const segments = ref.split("/");
|
|
35
|
-
let filename = segments[segments.length - 1];
|
|
36
|
-
if (/[A-Z]/.test(filename)) {
|
|
37
|
-
filename = toKebabCase(filename);
|
|
38
|
-
}
|
|
39
|
-
return filename;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// src/ref-to-name.ts
|
|
43
|
-
var kebabToPascal = (kebab, suffix) => {
|
|
44
|
-
const words = kebab.split("-");
|
|
45
|
-
let pascalCase = "";
|
|
46
|
-
for (const word of words) {
|
|
47
|
-
pascalCase += word.charAt(0).toUpperCase() + word.slice(1);
|
|
48
|
-
}
|
|
49
|
-
return pascalCase + suffix;
|
|
50
|
-
};
|
|
51
|
-
var refToName = (ref, suffix = "") => kebabToPascal(refToFilename(ref), suffix);
|
|
52
|
-
export {
|
|
53
|
-
refToName
|
|
1
|
+
import { refToFilename } from './ref-to-filename.js';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a kebab-case filename to PascalCase, appending an optional suffix.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* kebabToPascal('server-variable') // 'ServerVariable'
|
|
8
|
+
* kebabToPascal('channel') // 'Channel'
|
|
9
|
+
* kebabToPascal('channel', 'Object') // 'ChannelObject'
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
const kebabToPascal = (kebab, suffix) => {
|
|
13
|
+
const words = kebab.split('-');
|
|
14
|
+
let pascalCase = '';
|
|
15
|
+
for (const word of words) {
|
|
16
|
+
pascalCase += word.charAt(0).toUpperCase() + word.slice(1);
|
|
17
|
+
}
|
|
18
|
+
return pascalCase + suffix;
|
|
54
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Converts a JSON Schema $ref to a type name.
|
|
22
|
+
* Derives the filename via `refToFilename` then converts to PascalCase,
|
|
23
|
+
* appending an optional suffix.
|
|
24
|
+
*
|
|
25
|
+
* Handles all ref forms: internal `#/$defs/...`, `#/definitions/...`, and URI refs.
|
|
26
|
+
*
|
|
27
|
+
* @param ref - The $ref string
|
|
28
|
+
* @param suffix - Optional suffix appended to the PascalCase name. Defaults to
|
|
29
|
+
* `''` (no suffix). Pass e.g. `'Object'` to get `ContactObject`.
|
|
30
|
+
* @returns The type name in PascalCase with the suffix applied
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* refToName('#/$defs/contact') // 'Contact'
|
|
35
|
+
* refToName('#/$defs/server-variable') // 'ServerVariable'
|
|
36
|
+
* refToName('#/$defs/contact', 'Object') // 'ContactObject'
|
|
37
|
+
* refToName('http://asyncapi.com/definitions/3.1.0/channel.json') // 'Channel'
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export const refToName = (ref, suffix = '') => kebabToPascal(refToFilename(ref), suffix);
|
|
@@ -11,3 +11,4 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
|
11
11
|
* so each schema is relatively shallow.
|
|
12
12
|
*/
|
|
13
13
|
export declare const resolveDynamicRefs: (schema: JSONSchema, dynamicRefMap: Record<string, string>) => JSONSchema;
|
|
14
|
+
//# sourceMappingURL=resolve-dynamic-refs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-dynamic-refs.d.ts","sourceRoot":"","sources":["../src/resolve-dynamic-refs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAEjE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,WAAY,UAAU,iBAAiB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,UAkC9F,CAAA"}
|
|
@@ -1,31 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Replaces $dynamicRef with $ref in a schema using the provided anchor-to-path map.
|
|
3
|
+
*
|
|
4
|
+
* This creates a deep clone of the schema to avoid mutating the original, then walks
|
|
5
|
+
* the clone and converts any { $dynamicRef: "#meta" } to { $ref: "#/$defs/schema" }
|
|
6
|
+
* (or whatever the dynamicRefMap dictates).
|
|
7
|
+
*
|
|
8
|
+
* Only the direct properties and their nested schemas are walked — this does not need
|
|
9
|
+
* to be infinitely deep because the build system generates separate files for each $def,
|
|
10
|
+
* so each schema is relatively shallow.
|
|
11
|
+
*/
|
|
12
|
+
export const resolveDynamicRefs = (schema, dynamicRefMap) => {
|
|
13
|
+
if (typeof schema !== 'object' || schema === null) {
|
|
14
|
+
return schema;
|
|
13
15
|
}
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
if (resolved) {
|
|
18
|
-
record["$ref"] = resolved;
|
|
19
|
-
delete record["$dynamicRef"];
|
|
20
|
-
}
|
|
16
|
+
// Skip if there are no dynamic refs to resolve
|
|
17
|
+
if (Object.keys(dynamicRefMap).length === 0) {
|
|
18
|
+
return schema;
|
|
21
19
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
const clone = JSON.parse(JSON.stringify(schema));
|
|
21
|
+
const walk = (obj) => {
|
|
22
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const record = obj;
|
|
26
|
+
if ('$dynamicRef' in record && typeof record['$dynamicRef'] === 'string') {
|
|
27
|
+
const resolved = dynamicRefMap[record['$dynamicRef']];
|
|
28
|
+
if (resolved) {
|
|
29
|
+
record['$ref'] = resolved;
|
|
30
|
+
delete record['$dynamicRef'];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
for (const key in record) {
|
|
34
|
+
walk(record[key]);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
walk(clone);
|
|
38
|
+
return clone;
|
|
31
39
|
};
|
package/dist/resolve-ref.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-ref.d.ts","sourceRoot":"","sources":["../src/resolve-ref.ts"],"names":[],"mappings":"AA0BA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,UAAU,QAAS,MAAM,cAAc,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SA8BvG,CAAA"}
|
package/dist/resolve-ref.js
CHANGED
|
@@ -1,42 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
current
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
const navigatePointer = (pointer, schema) => {
|
|
6
|
+
const parts = pointer.split('/').filter(Boolean);
|
|
7
|
+
let current = schema;
|
|
8
|
+
for (const part of parts) {
|
|
9
|
+
const decodedPart = part.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
10
|
+
if (current && typeof current === 'object' && decodedPart in current) {
|
|
11
|
+
const next = current[decodedPart];
|
|
12
|
+
if (typeof next === 'object' && next !== null) {
|
|
13
|
+
current = next;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
16
22
|
}
|
|
17
|
-
|
|
18
|
-
return current;
|
|
23
|
+
return current;
|
|
19
24
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Resolves a JSON Schema $ref pointer to the actual schema definition.
|
|
27
|
+
*
|
|
28
|
+
* Supports three ref forms:
|
|
29
|
+
* - Internal: `#/$defs/contact` — navigates the root schema by JSON Pointer
|
|
30
|
+
* - URI key: `http://example.com/foo.json` — looks up the key directly in `$defs`
|
|
31
|
+
* - URI with fragment: `http://example.com/foo.json#/definitions/bar` — looks up
|
|
32
|
+
* the base URI in `$defs`, then navigates the fragment within that definition
|
|
33
|
+
*
|
|
34
|
+
* @param ref - The $ref string
|
|
35
|
+
* @param rootSchema - The root schema containing the definitions
|
|
36
|
+
* @returns The resolved schema or undefined if not found
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const rootSchema = {
|
|
41
|
+
* $defs: {
|
|
42
|
+
* contact: { type: 'object', properties: { email: { type: 'string' } } },
|
|
43
|
+
* 'http://example.com/server.json': { type: 'object' },
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
* resolveRef('#/$defs/contact', rootSchema)
|
|
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);
|
|
42
77
|
};
|
package/dist/safe-accessor.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-accessor.d.ts","sourceRoot":"","sources":["../src/safe-accessor.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,YAAY,aAAc,MAAM,OAAO,MAAM,KAAG,MAW5D,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,QAAS,MAAM,KAAG,MAKrC,CAAA"}
|
package/dist/safe-accessor.js
CHANGED
|
@@ -1,21 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
+
const JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
7
|
+
/**
|
|
8
|
+
* Generates a safe property accessor for a given key on an object variable.
|
|
9
|
+
* Uses dot notation for simple identifiers and bracket notation for keys
|
|
10
|
+
* that contain special characters like hyphens.
|
|
11
|
+
*
|
|
12
|
+
* @param variable - The variable name (e.g., "input", "input?")
|
|
13
|
+
* @param key - The property name to access
|
|
14
|
+
* @returns A valid JS property access expression
|
|
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
|
+
// Handle optional chaining: "input?" -> "input?.['key']"
|
|
26
|
+
if (variable.endsWith('?')) {
|
|
27
|
+
return `${variable}.['${key}']`;
|
|
28
|
+
}
|
|
29
|
+
return `${variable}['${key}']`;
|
|
11
30
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Generates a safe property key for use in object literals.
|
|
33
|
+
* Wraps keys that are not valid identifiers in quotes.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* safeKey("name") // "name"
|
|
37
|
+
* safeKey("x-linkedin") // "'x-linkedin'"
|
|
38
|
+
*/
|
|
39
|
+
export const safeKey = (key) => {
|
|
40
|
+
if (JS_IDENTIFIER.test(key)) {
|
|
41
|
+
return key;
|
|
42
|
+
}
|
|
43
|
+
return `'${key}'`;
|
|
21
44
|
};
|
package/dist/schema-guards.d.ts
CHANGED
|
@@ -108,4 +108,5 @@ export declare const hasMinProperties: (schema: JSONSchema) => schema is SchemaO
|
|
|
108
108
|
export declare const hasMaxProperties: (schema: JSONSchema) => schema is SchemaObject & {
|
|
109
109
|
maxProperties: number;
|
|
110
110
|
};
|
|
111
|
-
export { hasRef } from './has-ref';
|
|
111
|
+
export { hasRef } from './has-ref.js';
|
|
112
|
+
//# sourceMappingURL=schema-guards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-guards.d.ts","sourceRoot":"","sources":["../src/schema-guards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAEjE,KAAK,YAAY,GAAG,OAAO,CAAC,UAAU,EAAE,KAAK,GAAG,OAAO,CAAC,CAAA;AAExD,iDAAiD;AACjD,eAAO,MAAM,cAAc,WAAY,UAAU,KAAG,MAAM,IAAI,YAE7D,CAAA;AAED,wDAAwD;AACxD,eAAO,MAAM,OAAO,WAAY,UAAU,KAAG,MAAM,IAAI,YAAY,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAEnF,CAAA;AAED,wDAAwD;AACxD,eAAO,MAAM,cAAc,WAAY,UAAU,KAAG,MAAM,IAAI,UAAU,CAAC,MAExE,CAAA;AAED,mDAAmD;AACnD,eAAO,MAAM,aAAa,WAChB,UAAU,KACjB,MAAM,IAAI,YAAY,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;CAOnE,CAAA;AAED,6CAA6C;AAC7C,eAAO,MAAM,OAAO,WAAY,UAAU,KAAG,MAAM,IAAI,YAAY,GAAG;IAAE,IAAI,EAAE,SAAS,OAAO,EAAE,CAAA;CAE/F,CAAA;AAED,8CAA8C;AAC9C,eAAO,MAAM,QAAQ,WAAY,UAAU,KAAG,MAAM,IAAI,YAAY,GAAG;IAAE,KAAK,EAAE,OAAO,CAAA;CAEtF,CAAA;AAED,gDA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