@amritk/helpers 0.2.1 → 0.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/dist/build-dynamic-ref-map.js +5 -3
- package/dist/generate-type-definition.js +5 -3
- package/dist/has-ref.d.ts +4 -0
- package/dist/has-ref.js +7 -0
- package/dist/schema-guards.d.ts +1 -5
- package/dist/schema-guards.js +5 -3
- package/package.json +10 -3
- package/src/build-dynamic-ref-map.test.ts +65 -0
- package/src/build-dynamic-ref-map.ts +42 -0
- package/src/extract-refs.test.ts +183 -0
- package/src/extract-refs.ts +73 -0
- package/src/generate-type-definition.test.ts +1210 -0
- package/src/generate-type-definition.ts +487 -0
- package/src/has-ref.ts +10 -0
- package/src/is-object.test.ts +65 -0
- package/src/is-object.ts +15 -0
- package/src/parse-documentation.test.ts +313 -0
- package/src/parse-documentation.ts +173 -0
- package/src/ref-to-filename.test.ts +64 -0
- package/src/ref-to-filename.ts +116 -0
- package/src/ref-to-name.test.ts +48 -0
- package/src/ref-to-name.ts +38 -0
- package/src/resolve-dynamic-refs.test.ts +94 -0
- package/src/resolve-dynamic-refs.ts +48 -0
- package/src/resolve-ref.test.ts +186 -0
- package/src/resolve-ref.ts +82 -0
- package/src/safe-accessor.test.ts +41 -0
- package/src/safe-accessor.ts +48 -0
- package/src/schema-guards.test.ts +406 -0
- package/src/schema-guards.ts +160 -0
- package/src/upgrade-draft07-schema.ts +179 -0
- package/src/validate-array.test.ts +52 -0
- package/src/validate-array.ts +15 -0
- package/src/validate-record.test.ts +56 -0
- package/src/validate-record.ts +19 -0
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// src/has-ref.ts
|
|
2
|
+
var hasRef = (value) => {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && "$ref" in value && typeof value.$ref === "string";
|
|
4
|
+
};
|
|
5
|
+
|
|
1
6
|
// src/schema-guards.ts
|
|
2
7
|
var isSchemaObject = (schema) => {
|
|
3
8
|
return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
|
|
@@ -83,9 +88,6 @@ var hasMinProperties = (schema) => {
|
|
|
83
88
|
var hasMaxProperties = (schema) => {
|
|
84
89
|
return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
|
|
85
90
|
};
|
|
86
|
-
var hasRef = (schema) => {
|
|
87
|
-
return isSchemaObject(schema) && "$ref" in schema && typeof schema.$ref === "string";
|
|
88
|
-
};
|
|
89
91
|
|
|
90
92
|
// src/build-dynamic-ref-map.ts
|
|
91
93
|
var buildDynamicRefMap = (rootSchema) => {
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// src/has-ref.ts
|
|
2
|
+
var hasRef = (value) => {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && "$ref" in value && typeof value.$ref === "string";
|
|
4
|
+
};
|
|
5
|
+
|
|
1
6
|
// src/schema-guards.ts
|
|
2
7
|
var isSchemaObject = (schema) => {
|
|
3
8
|
return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
|
|
@@ -83,9 +88,6 @@ var hasMinProperties = (schema) => {
|
|
|
83
88
|
var hasMaxProperties = (schema) => {
|
|
84
89
|
return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
|
|
85
90
|
};
|
|
86
|
-
var hasRef = (schema) => {
|
|
87
|
-
return isSchemaObject(schema) && "$ref" in schema && typeof schema.$ref === "string";
|
|
88
|
-
};
|
|
89
91
|
|
|
90
92
|
// src/ref-to-filename.ts
|
|
91
93
|
var toKebabCase = (value) => value.replace(/OAuth/g, "Oauth").replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").replace(/([a-z\d])([A-Z])/g, "$1-$2").toLowerCase();
|
package/dist/has-ref.js
ADDED
package/dist/schema-guards.d.ts
CHANGED
|
@@ -108,8 +108,4 @@ 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
|
-
|
|
112
|
-
export declare const hasRef: (schema: JSONSchema) => schema is SchemaObject & {
|
|
113
|
-
$ref: string;
|
|
114
|
-
};
|
|
115
|
-
export {};
|
|
111
|
+
export { hasRef } from './has-ref';
|
package/dist/schema-guards.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// src/has-ref.ts
|
|
2
|
+
var hasRef = (value) => {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && "$ref" in value && typeof value.$ref === "string";
|
|
4
|
+
};
|
|
5
|
+
|
|
1
6
|
// src/schema-guards.ts
|
|
2
7
|
var isSchemaObject = (schema) => {
|
|
3
8
|
return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
|
|
@@ -83,9 +88,6 @@ var hasMinProperties = (schema) => {
|
|
|
83
88
|
var hasMaxProperties = (schema) => {
|
|
84
89
|
return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
|
|
85
90
|
};
|
|
86
|
-
var hasRef = (schema) => {
|
|
87
|
-
return isSchemaObject(schema) && "$ref" in schema && typeof schema.$ref === "string";
|
|
88
|
-
};
|
|
89
91
|
export {
|
|
90
92
|
isSchemaObject,
|
|
91
93
|
isObjectSchema,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amritk/helpers",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Shared utilities for the mjst code generation ecosystem.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,20 +20,22 @@
|
|
|
20
20
|
"url": "https://github.com/amritk/mjst/issues"
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
|
-
"dist"
|
|
23
|
+
"dist",
|
|
24
|
+
"src"
|
|
24
25
|
],
|
|
25
26
|
"publishConfig": {
|
|
26
27
|
"access": "public"
|
|
27
28
|
},
|
|
28
29
|
"scripts": {
|
|
29
30
|
"build": "bun run build:code && bun run build:types",
|
|
30
|
-
"build:code": "bun build ./src/build-dynamic-ref-map.ts ./src/extract-refs.ts ./src/generate-type-definition.ts ./src/is-object.ts ./src/parse-documentation.ts ./src/ref-to-filename.ts ./src/ref-to-name.ts ./src/resolve-dynamic-refs.ts ./src/resolve-ref.ts ./src/safe-accessor.ts ./src/schema-guards.ts ./src/upgrade-draft07-schema.ts ./src/validate-array.ts ./src/validate-record.ts --outdir=dist --root=src --target=node",
|
|
31
|
+
"build:code": "bun build ./src/build-dynamic-ref-map.ts ./src/extract-refs.ts ./src/generate-type-definition.ts ./src/has-ref.ts ./src/is-object.ts ./src/parse-documentation.ts ./src/ref-to-filename.ts ./src/ref-to-name.ts ./src/resolve-dynamic-refs.ts ./src/resolve-ref.ts ./src/safe-accessor.ts ./src/schema-guards.ts ./src/upgrade-draft07-schema.ts ./src/validate-array.ts ./src/validate-record.ts --outdir=dist --root=src --target=node",
|
|
31
32
|
"build:types": "tsc -p ."
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
35
|
"json-schema-typed": "catalog:"
|
|
35
36
|
},
|
|
36
37
|
"exports": {
|
|
38
|
+
"./package.json": "./package.json",
|
|
37
39
|
"./build-dynamic-ref-map": {
|
|
38
40
|
"bun": "./src/build-dynamic-ref-map.ts",
|
|
39
41
|
"default": "./dist/build-dynamic-ref-map.js",
|
|
@@ -49,6 +51,11 @@
|
|
|
49
51
|
"default": "./dist/generate-type-definition.js",
|
|
50
52
|
"types": "./dist/generate-type-definition.d.ts"
|
|
51
53
|
},
|
|
54
|
+
"./has-ref": {
|
|
55
|
+
"bun": "./src/has-ref.ts",
|
|
56
|
+
"default": "./dist/has-ref.js",
|
|
57
|
+
"types": "./dist/has-ref.d.ts"
|
|
58
|
+
},
|
|
52
59
|
"./is-object": {
|
|
53
60
|
"bun": "./src/is-object.ts",
|
|
54
61
|
"default": "./dist/is-object.js",
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { buildDynamicRefMap } from './build-dynamic-ref-map'
|
|
4
|
+
|
|
5
|
+
describe('build-dynamic-ref-map', () => {
|
|
6
|
+
it('maps $dynamicAnchor values to $ref paths', () => {
|
|
7
|
+
const schema = {
|
|
8
|
+
type: 'object' as const,
|
|
9
|
+
$defs: {
|
|
10
|
+
schema: {
|
|
11
|
+
$dynamicAnchor: 'meta',
|
|
12
|
+
type: 'object',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const result = buildDynamicRefMap(schema)
|
|
18
|
+
expect(result).toEqual({ '#meta': '#/$defs/schema' })
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('returns empty map when no $dynamicAnchor exists', () => {
|
|
22
|
+
const schema = {
|
|
23
|
+
type: 'object' as const,
|
|
24
|
+
$defs: {
|
|
25
|
+
info: { type: 'object' },
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const result = buildDynamicRefMap(schema)
|
|
30
|
+
expect(result).toEqual({})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('returns empty map for non-object schema', () => {
|
|
34
|
+
const result = buildDynamicRefMap(true)
|
|
35
|
+
expect(result).toEqual({})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('returns empty map when no $defs exist', () => {
|
|
39
|
+
const schema = { type: 'object' as const }
|
|
40
|
+
const result = buildDynamicRefMap(schema)
|
|
41
|
+
expect(result).toEqual({})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('handles multiple $dynamicAnchor definitions', () => {
|
|
45
|
+
const schema = {
|
|
46
|
+
type: 'object' as const,
|
|
47
|
+
$defs: {
|
|
48
|
+
schema: {
|
|
49
|
+
$dynamicAnchor: 'meta',
|
|
50
|
+
type: 'object',
|
|
51
|
+
},
|
|
52
|
+
other: {
|
|
53
|
+
$dynamicAnchor: 'other-anchor',
|
|
54
|
+
type: 'string',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const result = buildDynamicRefMap(schema)
|
|
60
|
+
expect(result).toEqual({
|
|
61
|
+
'#meta': '#/$defs/schema',
|
|
62
|
+
'#other-anchor': '#/$defs/other',
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
+
|
|
3
|
+
import { isSchemaObject } from './schema-guards'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Builds a map of $dynamicRef anchor values to their corresponding $ref paths.
|
|
7
|
+
*
|
|
8
|
+
* JSON Schema 2020-12 uses $dynamicAnchor and $dynamicRef for late-binding references.
|
|
9
|
+
* In the OpenAPI spec, $dynamicAnchor: "meta" on the schema definition allows properties
|
|
10
|
+
* like media-type.schema to reference it via $dynamicRef: "#meta".
|
|
11
|
+
*
|
|
12
|
+
* This function scans all $defs for entries with $dynamicAnchor and builds a lookup
|
|
13
|
+
* so we can convert $dynamicRef values to concrete $ref paths.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Given a schema with $defs.schema having $dynamicAnchor: "meta"
|
|
17
|
+
* buildDynamicRefMap(rootSchema)
|
|
18
|
+
* // Returns: { "#meta": "#/$defs/schema" }
|
|
19
|
+
*/
|
|
20
|
+
export const buildDynamicRefMap = (rootSchema: JSONSchema): Record<string, string> => {
|
|
21
|
+
const map: Record<string, string> = {}
|
|
22
|
+
|
|
23
|
+
if (!isSchemaObject(rootSchema) || !('$defs' in rootSchema)) {
|
|
24
|
+
return map
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const defs = rootSchema.$defs as Record<string, unknown>
|
|
28
|
+
|
|
29
|
+
for (const [key, value] of Object.entries(defs)) {
|
|
30
|
+
if (
|
|
31
|
+
typeof value === 'object' &&
|
|
32
|
+
value !== null &&
|
|
33
|
+
'$dynamicAnchor' in value &&
|
|
34
|
+
typeof (value as Record<string, unknown>)['$dynamicAnchor'] === 'string'
|
|
35
|
+
) {
|
|
36
|
+
const anchor = (value as Record<string, unknown>)['$dynamicAnchor'] as string
|
|
37
|
+
map[`#${anchor}`] = `#/$defs/${key}`
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return map
|
|
42
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { extractRefs } from './extract-refs'
|
|
4
|
+
|
|
5
|
+
describe('extract-refs', () => {
|
|
6
|
+
it('extracts refs from a simple schema', () => {
|
|
7
|
+
const schema = {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
contact: { $ref: '#/$defs/contact' },
|
|
11
|
+
server: { $ref: '#/$defs/server' },
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const refs = extractRefs(schema)
|
|
16
|
+
|
|
17
|
+
expect(refs).toEqual(new Set(['#/$defs/contact', '#/$defs/server']))
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('extracts refs from nested objects', () => {
|
|
21
|
+
const schema = {
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
info: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
contact: { $ref: '#/$defs/contact' },
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const refs = extractRefs(schema)
|
|
34
|
+
|
|
35
|
+
expect(refs).toEqual(new Set(['#/$defs/contact']))
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('extracts refs from arrays', () => {
|
|
39
|
+
const schema = {
|
|
40
|
+
type: 'object',
|
|
41
|
+
properties: {
|
|
42
|
+
servers: {
|
|
43
|
+
type: 'array',
|
|
44
|
+
items: { $ref: '#/$defs/server' },
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const refs = extractRefs(schema)
|
|
50
|
+
|
|
51
|
+
expect(refs).toEqual(new Set(['#/$defs/server']))
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('extracts refs from allOf, anyOf, oneOf', () => {
|
|
55
|
+
const schema = {
|
|
56
|
+
allOf: [{ $ref: '#/$defs/base' }],
|
|
57
|
+
anyOf: [{ $ref: '#/$defs/option1' }, { $ref: '#/$defs/option2' }],
|
|
58
|
+
oneOf: [{ $ref: '#/$defs/choice1' }],
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const refs = extractRefs(schema)
|
|
62
|
+
|
|
63
|
+
expect(refs).toEqual(new Set(['#/$defs/base', '#/$defs/option1', '#/$defs/option2', '#/$defs/choice1']))
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('extracts refs from additionalProperties', () => {
|
|
67
|
+
const schema = {
|
|
68
|
+
type: 'object',
|
|
69
|
+
additionalProperties: { $ref: '#/$defs/value' },
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const refs = extractRefs(schema)
|
|
73
|
+
|
|
74
|
+
expect(refs).toEqual(new Set(['#/$defs/value']))
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('includes URI refs (http/https)', () => {
|
|
78
|
+
const schema = {
|
|
79
|
+
type: 'object',
|
|
80
|
+
properties: {
|
|
81
|
+
internal: { $ref: '#/$defs/internal' },
|
|
82
|
+
external: { $ref: 'https://example.com/schema.json' },
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const refs = extractRefs(schema)
|
|
87
|
+
|
|
88
|
+
expect(refs).toEqual(new Set(['#/$defs/internal', 'https://example.com/schema.json']))
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('includes URI refs with fragments', () => {
|
|
92
|
+
const schema = {
|
|
93
|
+
type: 'object',
|
|
94
|
+
properties: {
|
|
95
|
+
queue: { $ref: 'http://example.com/channel.json#/definitions/queue' },
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const refs = extractRefs(schema)
|
|
100
|
+
|
|
101
|
+
expect(refs).toEqual(new Set(['http://example.com/channel.json#/definitions/queue']))
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('ignores bare # self-references', () => {
|
|
105
|
+
const schema = {
|
|
106
|
+
type: 'object',
|
|
107
|
+
properties: {
|
|
108
|
+
self: { $ref: '#' },
|
|
109
|
+
contact: { $ref: '#/$defs/contact' },
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const refs = extractRefs(schema)
|
|
114
|
+
|
|
115
|
+
expect(refs).toEqual(new Set(['#/$defs/contact']))
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('ignores relative path refs', () => {
|
|
119
|
+
const schema = {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
msg: { $ref: '/components/messages/userSignedUp' },
|
|
123
|
+
contact: { $ref: '#/$defs/contact' },
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const refs = extractRefs(schema)
|
|
128
|
+
|
|
129
|
+
expect(refs).toEqual(new Set(['#/$defs/contact']))
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('returns empty set when no refs exist', () => {
|
|
133
|
+
const schema = {
|
|
134
|
+
type: 'object',
|
|
135
|
+
properties: {
|
|
136
|
+
name: { type: 'string' },
|
|
137
|
+
age: { type: 'number' },
|
|
138
|
+
},
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const refs = extractRefs(schema)
|
|
142
|
+
|
|
143
|
+
expect(refs).toEqual(new Set())
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('handles deeply nested refs', () => {
|
|
147
|
+
const schema = {
|
|
148
|
+
type: 'object',
|
|
149
|
+
properties: {
|
|
150
|
+
level1: {
|
|
151
|
+
type: 'object',
|
|
152
|
+
properties: {
|
|
153
|
+
level2: {
|
|
154
|
+
type: 'object',
|
|
155
|
+
properties: {
|
|
156
|
+
level3: { $ref: '#/$defs/deep' },
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const refs = extractRefs(schema)
|
|
165
|
+
|
|
166
|
+
expect(refs).toEqual(new Set(['#/$defs/deep']))
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('deduplicates refs', () => {
|
|
170
|
+
const schema = {
|
|
171
|
+
type: 'object',
|
|
172
|
+
properties: {
|
|
173
|
+
contact1: { $ref: '#/$defs/contact' },
|
|
174
|
+
contact2: { $ref: '#/$defs/contact' },
|
|
175
|
+
contact3: { $ref: '#/$defs/contact' },
|
|
176
|
+
},
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const refs = extractRefs(schema)
|
|
180
|
+
|
|
181
|
+
expect(refs).toEqual(new Set(['#/$defs/contact']))
|
|
182
|
+
})
|
|
183
|
+
})
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns true if a $ref value should be queued for processing.
|
|
5
|
+
*
|
|
6
|
+
* Accepted forms:
|
|
7
|
+
* - Internal: `#/$defs/foo` or `#/definitions/foo`
|
|
8
|
+
* - URI key: `http://example.com/foo.json` (no fragment, or empty fragment)
|
|
9
|
+
* - URI with fragment: `http://example.com/foo.json#/definitions/bar`
|
|
10
|
+
*
|
|
11
|
+
* Excluded:
|
|
12
|
+
* - `#` alone (self-reference, not a standalone definition)
|
|
13
|
+
* - Relative path refs (e.g. `/components/messages/foo`) — these point into
|
|
14
|
+
* example data in the schema document, not into type definitions
|
|
15
|
+
*/
|
|
16
|
+
const isResolvableRef = (ref: string): boolean => {
|
|
17
|
+
if (ref === '#') return false
|
|
18
|
+
if (ref.startsWith('#')) return true
|
|
19
|
+
if (ref.startsWith('http://') || ref.startsWith('https://')) return true
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Extracts all $ref values from a JSON Schema recursively.
|
|
25
|
+
* Returns both internal (`#`-prefixed) and URI refs so the build pipeline
|
|
26
|
+
* can resolve and generate files for all referenced definitions.
|
|
27
|
+
*
|
|
28
|
+
* @param schema - The JSON Schema to extract refs from
|
|
29
|
+
* @returns A Set of unique ref strings found in the schema
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const schema = {
|
|
34
|
+
* type: 'object',
|
|
35
|
+
* properties: {
|
|
36
|
+
* contact: { $ref: '#/$defs/contact' },
|
|
37
|
+
* channel: { $ref: 'http://example.com/channel.json' },
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
* const refs = extractRefs(schema)
|
|
41
|
+
* // refs = Set(['#/$defs/contact', 'http://example.com/channel.json'])
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export const extractRefs = (schema: JSONSchema): Set<string> => {
|
|
45
|
+
const refs = new Set<string>()
|
|
46
|
+
|
|
47
|
+
const traverse = (obj: unknown): void => {
|
|
48
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (Array.isArray(obj)) {
|
|
53
|
+
for (const item of obj) {
|
|
54
|
+
traverse(item)
|
|
55
|
+
}
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const record = obj as Record<string, unknown>
|
|
60
|
+
|
|
61
|
+
if ('$ref' in record && typeof record['$ref'] === 'string' && isResolvableRef(record['$ref'] as string)) {
|
|
62
|
+
refs.add(record['$ref'] as string)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Recursively traverse all properties using for...in to avoid intermediate array allocation
|
|
66
|
+
for (const key in record) {
|
|
67
|
+
traverse(record[key])
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
traverse(schema)
|
|
72
|
+
return refs
|
|
73
|
+
}
|