@amritk/helpers 0.8.0 → 0.10.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/build-dynamic-ref-map.d.ts +0 -1
- package/dist/derive-root-type-name.d.ts +0 -1
- package/dist/escape-regex-pattern.d.ts +0 -1
- package/dist/extract-dynamic-anchor-defs.d.ts +0 -1
- package/dist/extract-refs.d.ts +0 -1
- package/dist/generate-index-barrel.d.ts +0 -1
- package/dist/generate-type-definition.d.ts +0 -1
- package/dist/has-ref.d.ts +0 -1
- package/dist/is-object.d.ts +0 -1
- package/dist/mjst-extension.d.ts +0 -1
- package/dist/parse-documentation.d.ts +0 -1
- package/dist/ref-to-filename.d.ts +0 -1
- package/dist/ref-to-name.d.ts +0 -1
- package/dist/resolve-dynamic-refs.d.ts +0 -1
- package/dist/resolve-ref.d.ts +0 -1
- package/dist/safe-accessor.d.ts +0 -1
- package/dist/schema-guards.d.ts +8 -1
- package/dist/schema-guards.js +18 -0
- package/dist/unknown-key-check.d.ts +46 -0
- package/dist/unknown-key-check.js +40 -0
- package/dist/upgrade-draft07-schema.d.ts +0 -1
- package/dist/validate-array.d.ts +0 -1
- package/dist/validate-record.d.ts +0 -1
- package/dist/walk-ref-graph.d.ts +0 -1
- package/package.json +2 -4
- package/dist/build-dynamic-ref-map.d.ts.map +0 -1
- package/dist/derive-root-type-name.d.ts.map +0 -1
- package/dist/escape-regex-pattern.d.ts.map +0 -1
- package/dist/extract-dynamic-anchor-defs.d.ts.map +0 -1
- package/dist/extract-refs.d.ts.map +0 -1
- package/dist/generate-index-barrel.d.ts.map +0 -1
- package/dist/generate-type-definition.d.ts.map +0 -1
- package/dist/has-ref.d.ts.map +0 -1
- package/dist/is-object.d.ts.map +0 -1
- package/dist/mjst-extension.d.ts.map +0 -1
- package/dist/parse-documentation.d.ts.map +0 -1
- package/dist/ref-to-filename.d.ts.map +0 -1
- package/dist/ref-to-name.d.ts.map +0 -1
- package/dist/resolve-dynamic-refs.d.ts.map +0 -1
- package/dist/resolve-ref.d.ts.map +0 -1
- package/dist/safe-accessor.d.ts.map +0 -1
- package/dist/schema-guards.d.ts.map +0 -1
- package/dist/upgrade-draft07-schema.d.ts.map +0 -1
- package/dist/validate-array.d.ts.map +0 -1
- package/dist/validate-record.d.ts.map +0 -1
- package/dist/walk-ref-graph.d.ts.map +0 -1
- package/src/build-dynamic-ref-map.test.ts +0 -65
- package/src/build-dynamic-ref-map.ts +0 -42
- package/src/derive-root-type-name.test.ts +0 -42
- package/src/derive-root-type-name.ts +0 -42
- package/src/escape-regex-pattern.test.ts +0 -35
- package/src/escape-regex-pattern.ts +0 -20
- package/src/extract-dynamic-anchor-defs.test.ts +0 -29
- package/src/extract-dynamic-anchor-defs.ts +0 -33
- package/src/extract-refs.test.ts +0 -183
- package/src/extract-refs.ts +0 -73
- package/src/generate-index-barrel.test.ts +0 -41
- package/src/generate-index-barrel.ts +0 -62
- package/src/generate-type-definition.test.ts +0 -1386
- package/src/generate-type-definition.ts +0 -580
- package/src/has-ref.ts +0 -10
- package/src/is-object.test.ts +0 -65
- package/src/is-object.ts +0 -15
- package/src/mjst-extension.test.ts +0 -59
- package/src/mjst-extension.ts +0 -81
- package/src/parse-documentation.test.ts +0 -313
- package/src/parse-documentation.ts +0 -173
- package/src/ref-to-filename.test.ts +0 -64
- package/src/ref-to-filename.ts +0 -116
- package/src/ref-to-name.test.ts +0 -57
- package/src/ref-to-name.ts +0 -42
- package/src/resolve-dynamic-refs.test.ts +0 -106
- package/src/resolve-dynamic-refs.ts +0 -54
- package/src/resolve-ref.test.ts +0 -186
- package/src/resolve-ref.ts +0 -82
- package/src/safe-accessor.test.ts +0 -41
- package/src/safe-accessor.ts +0 -48
- package/src/schema-guards.test.ts +0 -427
- package/src/schema-guards.ts +0 -177
- package/src/upgrade-draft07-schema.ts +0 -179
- package/src/validate-array.test.ts +0 -52
- package/src/validate-array.ts +0 -15
- package/src/validate-record.test.ts +0 -56
- package/src/validate-record.ts +0 -19
- package/src/walk-ref-graph.test.ts +0 -118
- package/src/walk-ref-graph.ts +0 -184
package/src/ref-to-filename.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
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: string): string =>
|
|
15
|
-
value
|
|
16
|
-
// Collapse known mixed-case acronyms before splitting so they stay together
|
|
17
|
-
.replace(/OAuth/g, 'Oauth')
|
|
18
|
-
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2')
|
|
19
|
-
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
|
|
20
|
-
.toLowerCase()
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Derives a unique kebab-case filename from a URI ref.
|
|
24
|
-
*
|
|
25
|
-
* For a plain URI (no fragment), uses the path segments after the host,
|
|
26
|
-
* stripping version numbers, `.json` extension, and joining with `-`.
|
|
27
|
-
* For a URI with a fragment, appends the fragment's last path segment.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* uriRefToFilename('http://asyncapi.com/definitions/3.1.0/channel.json')
|
|
32
|
-
* // 'channel'
|
|
33
|
-
* uriRefToFilename('http://asyncapi.com/bindings/kafka/0.5.0/channel.json')
|
|
34
|
-
* // 'kafka-channel-binding'
|
|
35
|
-
* uriRefToFilename('http://asyncapi.com/bindings/sns/0.1.0/channel.json#/definitions/queue')
|
|
36
|
-
* // 'sns-channel-queue'
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
const uriRefToFilename = (uri: string): string => {
|
|
40
|
-
const hashIndex = uri.indexOf('#')
|
|
41
|
-
const baseUri = hashIndex === -1 ? uri : uri.slice(0, hashIndex)
|
|
42
|
-
const fragment = hashIndex === -1 ? '' : uri.slice(hashIndex + 1)
|
|
43
|
-
|
|
44
|
-
// Strip protocol + host, remove .json extension
|
|
45
|
-
const withoutProtocol = baseUri.replace(/^https?:\/\/[^/]+\//, '')
|
|
46
|
-
const withoutExt = withoutProtocol.replace(/\.json$/, '')
|
|
47
|
-
|
|
48
|
-
// Drop structural/noise segments:
|
|
49
|
-
// - "definitions" and "$defs" container keys
|
|
50
|
-
// - Version numbers that immediately follow "definitions" (e.g. "3.1.0" in "definitions/3.1.0/channel")
|
|
51
|
-
// but NOT version numbers in other positions (e.g. "0.5.0" in "bindings/kafka/0.5.0/channel")
|
|
52
|
-
// since those are needed to disambiguate multiple versions of the same binding
|
|
53
|
-
const rawSegments = withoutExt.split('/')
|
|
54
|
-
const SKIP_KEYS = new Set(['definitions', '$defs'])
|
|
55
|
-
const segments: string[] = []
|
|
56
|
-
for (let i = 0; i < rawSegments.length; i++) {
|
|
57
|
-
const s = rawSegments[i] as string
|
|
58
|
-
if (SKIP_KEYS.has(s)) continue
|
|
59
|
-
// Skip a version segment only if the previous (non-skipped) segment was "definitions"
|
|
60
|
-
const prevRaw = rawSegments[i - 1]
|
|
61
|
-
if (/^\d+\.\d+/.test(s) && prevRaw !== undefined && SKIP_KEYS.has(prevRaw)) continue
|
|
62
|
-
segments.push(s)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Join remaining segments, converting to kebab-case and replacing dots with dashes
|
|
66
|
-
const baseName = segments.map((s) => toKebabCase(s).replace(/\./g, '-')).join('-')
|
|
67
|
-
|
|
68
|
-
if (!fragment) return baseName
|
|
69
|
-
|
|
70
|
-
// Append the last meaningful segment of the fragment, skipping structural keys
|
|
71
|
-
const fragSegments = fragment.split('/').filter((s) => s && !SKIP_KEYS.has(s) && s !== 'properties')
|
|
72
|
-
const fragLast = fragSegments[fragSegments.length - 1]
|
|
73
|
-
if (!fragLast) return baseName
|
|
74
|
-
|
|
75
|
-
return `${baseName}-${toKebabCase(fragLast)}`
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Converts a JSON Schema $ref to a filename.
|
|
80
|
-
*
|
|
81
|
-
* Handles three ref forms:
|
|
82
|
-
* - Internal `#/$defs/contact` → `contact`
|
|
83
|
-
* - Internal `#/definitions/ServerVariable` → `server-variable`
|
|
84
|
-
* - URI `http://example.com/definitions/3.1.0/channel.json` → `channel`
|
|
85
|
-
* - URI with fragment `http://example.com/channel.json#/definitions/queue` → `channel-queue`
|
|
86
|
-
*
|
|
87
|
-
* @param ref - The $ref string
|
|
88
|
-
* @returns The filename without extension
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```ts
|
|
92
|
-
* refToFilename('#/$defs/contact') // 'contact'
|
|
93
|
-
* refToFilename('#/$defs/server-variable') // 'server-variable'
|
|
94
|
-
* refToFilename('#/definitions/ServerVariable') // 'server-variable'
|
|
95
|
-
* refToFilename('#/definitions/APIKeySecurityScheme') // 'api-key-security-scheme'
|
|
96
|
-
* refToFilename('http://asyncapi.com/definitions/3.1.0/channel.json') // 'channel'
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export const refToFilename = (ref: string): string => {
|
|
100
|
-
// URI ref — derive name from URI path
|
|
101
|
-
if (ref.startsWith('http://') || ref.startsWith('https://')) {
|
|
102
|
-
return uriRefToFilename(ref)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Internal ref — extract the last segment after the last /
|
|
106
|
-
const segments = ref.split('/')
|
|
107
|
-
// Non-null assertion is safe here: split always returns at least one element
|
|
108
|
-
let filename = segments[segments.length - 1] as string
|
|
109
|
-
|
|
110
|
-
// Normalise PascalCase/camelCase keys (e.g. from draft-07 "definitions") to kebab-case
|
|
111
|
-
if (/[A-Z]/.test(filename)) {
|
|
112
|
-
filename = toKebabCase(filename)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return filename
|
|
116
|
-
}
|
package/src/ref-to-name.test.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { refToName } from './ref-to-name'
|
|
4
|
-
|
|
5
|
-
describe('ref-to-name', () => {
|
|
6
|
-
it('converts simple ref to PascalCase', () => {
|
|
7
|
-
expect(refToName('#/$defs/contact')).toBe('Contact')
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
it('converts kebab-case ref to PascalCase', () => {
|
|
11
|
-
expect(refToName('#/$defs/server-variable')).toBe('ServerVariable')
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
it('converts multi-word kebab-case ref to PascalCase', () => {
|
|
15
|
-
expect(refToName('#/$defs/external-documentation')).toBe('ExternalDocumentation')
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('handles refs with multiple path segments', () => {
|
|
19
|
-
expect(refToName('#/components/schemas/user-profile')).toBe('UserProfile')
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('handles single word refs', () => {
|
|
23
|
-
expect(refToName('#/$defs/info')).toBe('Info')
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('handles refs with numbers', () => {
|
|
27
|
-
expect(refToName('#/$defs/oauth2-flow')).toBe('Oauth2Flow')
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('converts uppercase acronym keys to PascalCase via kebab normalization', () => {
|
|
31
|
-
expect(refToName('#/$defs/APIKey')).toBe('ApiKey')
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('derives type name from a URI ref', () => {
|
|
35
|
-
expect(refToName('http://asyncapi.com/definitions/3.1.0/channel.json')).toBe('Channel')
|
|
36
|
-
expect(refToName('http://asyncapi.com/definitions/3.1.0/info.json')).toBe('Info')
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('derives type name from a URI ref with a fragment', () => {
|
|
40
|
-
expect(refToName('http://asyncapi.com/bindings/sns/0.1.0/channel.json#/definitions/queue')).toBe(
|
|
41
|
-
'BindingsSns010ChannelQueue',
|
|
42
|
-
)
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('handles draft-07 schema URI', () => {
|
|
46
|
-
expect(refToName('http://json-schema.org/draft-07/schema')).toBe('Draft07Schema')
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('appends the suffix when one is provided', () => {
|
|
50
|
-
expect(refToName('#/$defs/contact', 'Object')).toBe('ContactObject')
|
|
51
|
-
expect(refToName('#/$defs/server-variable', 'Object')).toBe('ServerVariableObject')
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('treats an empty suffix the same as omitting it', () => {
|
|
55
|
-
expect(refToName('#/$defs/contact', '')).toBe('Contact')
|
|
56
|
-
})
|
|
57
|
-
})
|
package/src/ref-to-name.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { refToFilename } from './ref-to-filename'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Converts a kebab-case filename to PascalCase, appending an optional suffix.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```ts
|
|
8
|
-
* kebabToPascal('server-variable') // 'ServerVariable'
|
|
9
|
-
* kebabToPascal('channel') // 'Channel'
|
|
10
|
-
* kebabToPascal('channel', 'Object') // 'ChannelObject'
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
const kebabToPascal = (kebab: string, suffix: string): string => {
|
|
14
|
-
const words = kebab.split('-')
|
|
15
|
-
let pascalCase = ''
|
|
16
|
-
for (const word of words) {
|
|
17
|
-
pascalCase += word.charAt(0).toUpperCase() + word.slice(1)
|
|
18
|
-
}
|
|
19
|
-
return pascalCase + suffix
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Converts a JSON Schema $ref to a type name.
|
|
24
|
-
* Derives the filename via `refToFilename` then converts to PascalCase,
|
|
25
|
-
* appending an optional suffix.
|
|
26
|
-
*
|
|
27
|
-
* Handles all ref forms: internal `#/$defs/...`, `#/definitions/...`, and URI refs.
|
|
28
|
-
*
|
|
29
|
-
* @param ref - The $ref string
|
|
30
|
-
* @param suffix - Optional suffix appended to the PascalCase name. Defaults to
|
|
31
|
-
* `''` (no suffix). Pass e.g. `'Object'` to get `ContactObject`.
|
|
32
|
-
* @returns The type name in PascalCase with the suffix applied
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* refToName('#/$defs/contact') // 'Contact'
|
|
37
|
-
* refToName('#/$defs/server-variable') // 'ServerVariable'
|
|
38
|
-
* refToName('#/$defs/contact', 'Object') // 'ContactObject'
|
|
39
|
-
* refToName('http://asyncapi.com/definitions/3.1.0/channel.json') // 'Channel'
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
export const refToName = (ref: string, suffix = ''): string => kebabToPascal(refToFilename(ref), suffix)
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { resolveDynamicRefs } from './resolve-dynamic-refs'
|
|
4
|
-
|
|
5
|
-
describe('resolve-dynamic-refs', () => {
|
|
6
|
-
it('replaces $dynamicRef with $ref in properties', () => {
|
|
7
|
-
const schema = {
|
|
8
|
-
type: 'object' as const,
|
|
9
|
-
properties: {
|
|
10
|
-
schema: { $dynamicRef: '#meta' },
|
|
11
|
-
},
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const result = resolveDynamicRefs(schema, { '#meta': '#/$defs/schema' })
|
|
15
|
-
|
|
16
|
-
expect(result).toEqual({
|
|
17
|
-
type: 'object',
|
|
18
|
-
properties: {
|
|
19
|
-
schema: { $ref: '#/$defs/schema' },
|
|
20
|
-
},
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('replaces $dynamicRef in additionalProperties', () => {
|
|
25
|
-
const schema = {
|
|
26
|
-
type: 'object' as const,
|
|
27
|
-
additionalProperties: { $dynamicRef: '#meta' },
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const result = resolveDynamicRefs(schema, { '#meta': '#/$defs/schema' })
|
|
31
|
-
|
|
32
|
-
expect(result).toEqual({
|
|
33
|
-
type: 'object',
|
|
34
|
-
additionalProperties: { $ref: '#/$defs/schema' },
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
it('does not mutate the original schema', () => {
|
|
39
|
-
const schema = {
|
|
40
|
-
type: 'object' as const,
|
|
41
|
-
properties: {
|
|
42
|
-
schema: { $dynamicRef: '#meta' },
|
|
43
|
-
},
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
resolveDynamicRefs(schema, { '#meta': '#/$defs/schema' })
|
|
47
|
-
|
|
48
|
-
// Original should be unchanged
|
|
49
|
-
expect(schema.properties.schema).toEqual({ $dynamicRef: '#meta' })
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('returns schema unchanged when dynamicRefMap is empty', () => {
|
|
53
|
-
const schema = {
|
|
54
|
-
type: 'object' as const,
|
|
55
|
-
properties: {
|
|
56
|
-
schema: { $dynamicRef: '#meta' },
|
|
57
|
-
},
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const result = resolveDynamicRefs(schema, {})
|
|
61
|
-
|
|
62
|
-
expect(result).toEqual(schema)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it('returns non-object schemas unchanged', () => {
|
|
66
|
-
expect(resolveDynamicRefs(true, { '#meta': '#/$defs/schema' })).toBe(true)
|
|
67
|
-
expect(resolveDynamicRefs(false, { '#meta': '#/$defs/schema' })).toBe(false)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('replaces $dynamicRef nested inside an array keyword (allOf)', () => {
|
|
71
|
-
const schema = {
|
|
72
|
-
allOf: [{ $dynamicRef: '#meta' }, { type: 'object' as const, properties: { x: { $dynamicRef: '#meta' } } }],
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const result = resolveDynamicRefs(schema, { '#meta': '#/$defs/schema' })
|
|
76
|
-
|
|
77
|
-
expect(result).toEqual({
|
|
78
|
-
allOf: [{ $ref: '#/$defs/schema' }, { type: 'object', properties: { x: { $ref: '#/$defs/schema' } } }],
|
|
79
|
-
})
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('handles nested $dynamicRef in property sub-schemas', () => {
|
|
83
|
-
const schema = {
|
|
84
|
-
type: 'object' as const,
|
|
85
|
-
properties: {
|
|
86
|
-
schemas: {
|
|
87
|
-
type: 'object' as const,
|
|
88
|
-
additionalProperties: { $dynamicRef: '#meta' },
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const result = resolveDynamicRefs(schema, { '#meta': '#/$defs/schema' })
|
|
94
|
-
|
|
95
|
-
const expected = {
|
|
96
|
-
type: 'object',
|
|
97
|
-
properties: {
|
|
98
|
-
schemas: {
|
|
99
|
-
type: 'object',
|
|
100
|
-
additionalProperties: { $ref: '#/$defs/schema' },
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
}
|
|
104
|
-
expect(result).toEqual(expected)
|
|
105
|
-
})
|
|
106
|
-
})
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Replaces $dynamicRef with $ref in a schema using the provided anchor-to-path map.
|
|
5
|
-
*
|
|
6
|
-
* This creates a deep clone of the schema to avoid mutating the original, then walks
|
|
7
|
-
* the clone and converts any { $dynamicRef: "#meta" } to { $ref: "#/$defs/schema" }
|
|
8
|
-
* (or whatever the dynamicRefMap dictates).
|
|
9
|
-
*
|
|
10
|
-
* Walks both object properties and array elements, so a `$dynamicRef` nested
|
|
11
|
-
* inside a keyword whose value is an array of subschemas (`allOf`, `anyOf`,
|
|
12
|
-
* `oneOf`, `prefixItems`, …) is rewritten too. The build system generates a
|
|
13
|
-
* separate file per `$def`, so each schema walked here is relatively shallow.
|
|
14
|
-
*/
|
|
15
|
-
export const resolveDynamicRefs = (schema: JSONSchema, dynamicRefMap: Record<string, string>): JSONSchema => {
|
|
16
|
-
if (typeof schema !== 'object' || schema === null) {
|
|
17
|
-
return schema
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Skip if there are no dynamic refs to resolve
|
|
21
|
-
if (Object.keys(dynamicRefMap).length === 0) {
|
|
22
|
-
return schema
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const clone = JSON.parse(JSON.stringify(schema)) as Record<string, unknown>
|
|
26
|
-
|
|
27
|
-
const walk = (obj: unknown): void => {
|
|
28
|
-
if (typeof obj !== 'object' || obj === null) {
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (Array.isArray(obj)) {
|
|
33
|
-
for (const item of obj) walk(item)
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const record = obj as Record<string, unknown>
|
|
38
|
-
|
|
39
|
-
if ('$dynamicRef' in record && typeof record['$dynamicRef'] === 'string') {
|
|
40
|
-
const resolved = dynamicRefMap[record['$dynamicRef'] as string]
|
|
41
|
-
if (resolved) {
|
|
42
|
-
record['$ref'] = resolved
|
|
43
|
-
delete record['$dynamicRef']
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
for (const key in record) {
|
|
48
|
-
walk(record[key])
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
walk(clone)
|
|
53
|
-
return clone as JSONSchema
|
|
54
|
-
}
|
package/src/resolve-ref.test.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { resolveRef } from './resolve-ref'
|
|
4
|
-
|
|
5
|
-
describe('resolveRef', () => {
|
|
6
|
-
it('resolves a simple $ref to $defs', () => {
|
|
7
|
-
const schema = {
|
|
8
|
-
$defs: {
|
|
9
|
-
contact: {
|
|
10
|
-
type: 'object',
|
|
11
|
-
properties: {
|
|
12
|
-
email: { type: 'string' },
|
|
13
|
-
phone: { type: 'string' },
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const result = resolveRef('#/$defs/contact', schema)
|
|
20
|
-
|
|
21
|
-
expect(result).toEqual({
|
|
22
|
-
type: 'object',
|
|
23
|
-
properties: {
|
|
24
|
-
email: { type: 'string' },
|
|
25
|
-
phone: { type: 'string' },
|
|
26
|
-
},
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('resolves a nested $ref', () => {
|
|
31
|
-
const schema = {
|
|
32
|
-
components: {
|
|
33
|
-
schemas: {
|
|
34
|
-
User: {
|
|
35
|
-
type: 'object',
|
|
36
|
-
properties: {
|
|
37
|
-
name: { type: 'string' },
|
|
38
|
-
age: { type: 'number' },
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const result = resolveRef('#/components/schemas/User', schema)
|
|
46
|
-
|
|
47
|
-
expect(result).toEqual({
|
|
48
|
-
type: 'object',
|
|
49
|
-
properties: {
|
|
50
|
-
name: { type: 'string' },
|
|
51
|
-
age: { type: 'number' },
|
|
52
|
-
},
|
|
53
|
-
})
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('returns undefined for non-existent $ref', () => {
|
|
57
|
-
const schema = {
|
|
58
|
-
$defs: {
|
|
59
|
-
contact: { type: 'object' },
|
|
60
|
-
},
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const result = resolveRef('#/$defs/nonexistent', schema)
|
|
64
|
-
|
|
65
|
-
expect(result).toBeUndefined()
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('returns undefined for URI refs not present in $defs', () => {
|
|
69
|
-
const schema = {
|
|
70
|
-
$defs: {
|
|
71
|
-
contact: { type: 'object' },
|
|
72
|
-
},
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const result = resolveRef('http://example.com/schema.json', schema)
|
|
76
|
-
|
|
77
|
-
expect(result).toBeUndefined()
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
it('resolves a URI ref that is a $defs key', () => {
|
|
81
|
-
const schema = {
|
|
82
|
-
$defs: {
|
|
83
|
-
'http://example.com/channel.json': { type: 'object', properties: { name: { type: 'string' } } },
|
|
84
|
-
},
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const result = resolveRef('http://example.com/channel.json', schema)
|
|
88
|
-
|
|
89
|
-
expect(result).toEqual({ type: 'object', properties: { name: { type: 'string' } } })
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it('resolves a URI ref with a fragment into a nested definition', () => {
|
|
93
|
-
const schema = {
|
|
94
|
-
$defs: {
|
|
95
|
-
'http://example.com/channel.json': {
|
|
96
|
-
type: 'object',
|
|
97
|
-
$defs: {
|
|
98
|
-
queue: { type: 'object', properties: { name: { type: 'string' } } },
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const result = resolveRef('http://example.com/channel.json#/$defs/queue', schema)
|
|
105
|
-
|
|
106
|
-
expect(result).toEqual({ type: 'object', properties: { name: { type: 'string' } } })
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
it('handles URI-encoded characters in $ref', () => {
|
|
110
|
-
const schema = {
|
|
111
|
-
$defs: {
|
|
112
|
-
'my~field': {
|
|
113
|
-
type: 'string',
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// ~0 represents ~
|
|
119
|
-
const result = resolveRef('#/$defs/my~0field', schema)
|
|
120
|
-
|
|
121
|
-
expect(result).toEqual({ type: 'string' })
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
it('resolves deeply nested $ref', () => {
|
|
125
|
-
const schema = {
|
|
126
|
-
definitions: {
|
|
127
|
-
nested: {
|
|
128
|
-
deep: {
|
|
129
|
-
value: {
|
|
130
|
-
type: 'boolean',
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const result = resolveRef('#/definitions/nested/deep/value', schema)
|
|
138
|
-
|
|
139
|
-
expect(result).toEqual({ type: 'boolean' })
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
it('returns undefined for empty $ref', () => {
|
|
143
|
-
const schema = {
|
|
144
|
-
$defs: {
|
|
145
|
-
contact: { type: 'object' },
|
|
146
|
-
},
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const result = resolveRef('#', schema)
|
|
150
|
-
|
|
151
|
-
expect(result).toEqual(schema)
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
it('handles $ref to array items', () => {
|
|
155
|
-
const schema = {
|
|
156
|
-
$defs: {
|
|
157
|
-
stringArray: {
|
|
158
|
-
type: 'array',
|
|
159
|
-
items: { type: 'string' },
|
|
160
|
-
},
|
|
161
|
-
},
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const result = resolveRef('#/$defs/stringArray', schema)
|
|
165
|
-
|
|
166
|
-
expect(result).toEqual({
|
|
167
|
-
type: 'array',
|
|
168
|
-
items: { type: 'string' },
|
|
169
|
-
})
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
it('returns undefined when a path segment resolves to a non-object value', () => {
|
|
173
|
-
// When a path segment exists but its value is a primitive (not an object),
|
|
174
|
-
// navigation cannot continue and the ref is unresolvable.
|
|
175
|
-
const schema = {
|
|
176
|
-
$defs: {
|
|
177
|
-
// "count" is a number, not an object — resolveRef cannot navigate into it
|
|
178
|
-
count: 42,
|
|
179
|
-
},
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const result = resolveRef('#/$defs/count', schema as unknown as Record<string, unknown>)
|
|
183
|
-
|
|
184
|
-
expect(result).toBeUndefined()
|
|
185
|
-
})
|
|
186
|
-
})
|
package/src/resolve-ref.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
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: string, schema: Record<string, unknown>): Record<string, unknown> | undefined => {
|
|
6
|
-
const parts = pointer.split('/').filter(Boolean)
|
|
7
|
-
let current = schema
|
|
8
|
-
|
|
9
|
-
for (const part of parts) {
|
|
10
|
-
const decodedPart = part.replace(/~1/g, '/').replace(/~0/g, '~')
|
|
11
|
-
|
|
12
|
-
if (current && typeof current === 'object' && decodedPart in current) {
|
|
13
|
-
const next = current[decodedPart as keyof typeof current]
|
|
14
|
-
if (typeof next === 'object' && next !== null) {
|
|
15
|
-
current = next as Record<string, unknown>
|
|
16
|
-
} else {
|
|
17
|
-
return undefined
|
|
18
|
-
}
|
|
19
|
-
} else {
|
|
20
|
-
return undefined
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return current
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Resolves a JSON Schema $ref pointer to the actual schema definition.
|
|
29
|
-
*
|
|
30
|
-
* Supports three ref forms:
|
|
31
|
-
* - Internal: `#/$defs/contact` — navigates the root schema by JSON Pointer
|
|
32
|
-
* - URI key: `http://example.com/foo.json` — looks up the key directly in `$defs`
|
|
33
|
-
* - URI with fragment: `http://example.com/foo.json#/definitions/bar` — looks up
|
|
34
|
-
* the base URI in `$defs`, then navigates the fragment within that definition
|
|
35
|
-
*
|
|
36
|
-
* @param ref - The $ref string
|
|
37
|
-
* @param rootSchema - The root schema containing the definitions
|
|
38
|
-
* @returns The resolved schema or undefined if not found
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* const rootSchema = {
|
|
43
|
-
* $defs: {
|
|
44
|
-
* contact: { type: 'object', properties: { email: { type: 'string' } } },
|
|
45
|
-
* 'http://example.com/server.json': { type: 'object' },
|
|
46
|
-
* }
|
|
47
|
-
* }
|
|
48
|
-
* resolveRef('#/$defs/contact', rootSchema)
|
|
49
|
-
* resolveRef('http://example.com/server.json', rootSchema)
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export const resolveRef = (ref: string, rootSchema: Record<string, unknown>): Record<string, unknown> | undefined => {
|
|
53
|
-
// Internal reference: navigate from root by JSON Pointer
|
|
54
|
-
if (ref.startsWith('#')) {
|
|
55
|
-
return navigatePointer(ref.slice(1), rootSchema)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// URI ref: may have a fragment (e.g. "http://foo.com/bar.json#/definitions/baz")
|
|
59
|
-
// A trailing bare "#" (e.g. "http://foo.com/schema#") means "the whole document" — treat as no fragment
|
|
60
|
-
const hashIndex = ref.indexOf('#')
|
|
61
|
-
const baseUri = hashIndex === -1 ? ref : ref.slice(0, hashIndex)
|
|
62
|
-
const rawFragment = hashIndex === -1 ? '' : ref.slice(hashIndex + 1)
|
|
63
|
-
const fragment = rawFragment === '' || rawFragment === '/' ? '' : rawFragment
|
|
64
|
-
|
|
65
|
-
// Look up the base URI as a key in $defs
|
|
66
|
-
const defs = rootSchema['$defs']
|
|
67
|
-
if (typeof defs !== 'object' || defs === null) return undefined
|
|
68
|
-
|
|
69
|
-
const defsRecord = defs as Record<string, unknown>
|
|
70
|
-
const base = defsRecord[baseUri]
|
|
71
|
-
if (typeof base !== 'object' || base === null) return undefined
|
|
72
|
-
|
|
73
|
-
// No fragment — return the definition directly
|
|
74
|
-
if (!fragment) return base as Record<string, unknown>
|
|
75
|
-
|
|
76
|
-
// Normalize the fragment: draft-07 schemas use `/definitions/` but after
|
|
77
|
-
// upgradeDraft07Schema the key is renamed to `/$defs/`
|
|
78
|
-
const normalizedFragment = fragment.replace(/^\/definitions\//, '/$defs/')
|
|
79
|
-
|
|
80
|
-
// Navigate the fragment within the resolved definition
|
|
81
|
-
return navigatePointer(normalizedFragment, base as Record<string, unknown>)
|
|
82
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { safeAccessor, safeKey } from './safe-accessor'
|
|
4
|
-
|
|
5
|
-
describe('safe-accessor', () => {
|
|
6
|
-
it('uses dot notation for simple identifiers', () => {
|
|
7
|
-
expect(safeAccessor('input', 'name')).toBe('input.name')
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
it('uses bracket notation for hyphenated keys', () => {
|
|
11
|
-
expect(safeAccessor('input', 'x-linkedin')).toBe("input['x-linkedin']")
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
it('uses optional chaining with bracket notation for hyphenated keys', () => {
|
|
15
|
-
expect(safeAccessor('input?', 'x-linkedin')).toBe("input?.['x-linkedin']")
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('uses dot notation for underscored identifiers', () => {
|
|
19
|
-
expect(safeAccessor('input', '_private')).toBe('input._private')
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('uses bracket notation for keys starting with numbers', () => {
|
|
23
|
-
expect(safeAccessor('input', '0foo')).toBe("input['0foo']")
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('uses bracket notation for keys with dots', () => {
|
|
27
|
-
expect(safeAccessor('input', 'foo.bar')).toBe("input['foo.bar']")
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('returns unquoted key for simple identifiers', () => {
|
|
31
|
-
expect(safeKey('name')).toBe('name')
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('returns quoted key for hyphenated names', () => {
|
|
35
|
-
expect(safeKey('x-linkedin')).toBe("'x-linkedin'")
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
it('returns quoted key for names with dots', () => {
|
|
39
|
-
expect(safeKey('foo.bar')).toBe("'foo.bar'")
|
|
40
|
-
})
|
|
41
|
-
})
|