@amritk/helpers 0.12.0 → 0.13.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 +5 -2
- package/dist/build-dynamic-ref-map.js +35 -13
- package/dist/escape-regex-pattern.js +8 -0
- package/dist/extract-dynamic-anchor-defs.d.ts +5 -2
- package/dist/extract-dynamic-anchor-defs.js +7 -15
- package/dist/schema-guards.d.ts +20 -0
- package/dist/schema-guards.js +26 -0
- package/dist/walk-ref-graph.js +4 -5
- package/package.json +1 -1
- package/src/build-dynamic-ref-map.ts +35 -16
- package/src/escape-regex-pattern.ts +9 -0
- package/src/extract-dynamic-anchor-defs.ts +8 -16
- package/src/schema-guards.ts +39 -0
- package/src/walk-ref-graph.ts +4 -5
|
@@ -6,8 +6,11 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
|
6
6
|
* In the OpenAPI spec, $dynamicAnchor: "meta" on the schema definition allows properties
|
|
7
7
|
* like media-type.schema to reference it via $dynamicRef: "#meta".
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* The whole document is scanned — a `$dynamicAnchor` may sit anywhere, not just
|
|
10
|
+
* on a direct `$defs` entry — and each anchor maps to the JSON Pointer of the
|
|
11
|
+
* subschema that declares it. When the same anchor name appears more than once,
|
|
12
|
+
* the first occurrence in document order wins, matching how the resolvers bind
|
|
13
|
+
* a name to a single document-global target.
|
|
11
14
|
*
|
|
12
15
|
* @example
|
|
13
16
|
* // Given a schema with $defs.schema having $dynamicAnchor: "meta"
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { isSchemaObject } from './schema-guards.js';
|
|
2
|
+
/** Escapes a JSON Pointer segment (RFC 6901): `~` → `~0`, `/` → `~1`. */
|
|
3
|
+
const escapeSegment = (segment) => segment.indexOf('~') !== -1 || segment.indexOf('/') !== -1
|
|
4
|
+
? segment.replace(/~/g, '~0').replace(/\//g, '~1')
|
|
5
|
+
: segment;
|
|
6
|
+
// Keywords whose values are data, not subschemas — a `$dynamicAnchor` key inside
|
|
7
|
+
// an enum member or example value is instance data and must not register.
|
|
8
|
+
const NON_SCHEMA_KEYWORDS = new Set(['enum', 'const', 'default', 'examples']);
|
|
2
9
|
/**
|
|
3
10
|
* Builds a map of $dynamicRef anchor values to their corresponding $ref paths.
|
|
4
11
|
*
|
|
@@ -6,8 +13,11 @@ import { isSchemaObject } from './schema-guards.js';
|
|
|
6
13
|
* In the OpenAPI spec, $dynamicAnchor: "meta" on the schema definition allows properties
|
|
7
14
|
* like media-type.schema to reference it via $dynamicRef: "#meta".
|
|
8
15
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
16
|
+
* The whole document is scanned — a `$dynamicAnchor` may sit anywhere, not just
|
|
17
|
+
* on a direct `$defs` entry — and each anchor maps to the JSON Pointer of the
|
|
18
|
+
* subschema that declares it. When the same anchor name appears more than once,
|
|
19
|
+
* the first occurrence in document order wins, matching how the resolvers bind
|
|
20
|
+
* a name to a single document-global target.
|
|
11
21
|
*
|
|
12
22
|
* @example
|
|
13
23
|
* // Given a schema with $defs.schema having $dynamicAnchor: "meta"
|
|
@@ -16,18 +26,30 @@ import { isSchemaObject } from './schema-guards.js';
|
|
|
16
26
|
*/
|
|
17
27
|
export const buildDynamicRefMap = (rootSchema) => {
|
|
18
28
|
const map = {};
|
|
19
|
-
if (!isSchemaObject(rootSchema)
|
|
29
|
+
if (!isSchemaObject(rootSchema))
|
|
20
30
|
return map;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const anchor = value['$dynamicAnchor'];
|
|
29
|
-
map[`#${anchor}`] = `#/$defs/${key}`;
|
|
31
|
+
const walk = (node, pointer) => {
|
|
32
|
+
if (node === null || typeof node !== 'object')
|
|
33
|
+
return;
|
|
34
|
+
if (Array.isArray(node)) {
|
|
35
|
+
for (let i = 0; i < node.length; i++)
|
|
36
|
+
walk(node[i], `${pointer}/${i}`);
|
|
37
|
+
return;
|
|
30
38
|
}
|
|
31
|
-
|
|
39
|
+
const record = node;
|
|
40
|
+
const anchor = record['$dynamicAnchor'];
|
|
41
|
+
// The document root is skipped: mapping it would rewrite `$dynamicRef` to
|
|
42
|
+
// `$ref: "#"`, a self-reference the file-per-definition generators cannot
|
|
43
|
+
// name an output file for. Every nested subschema gets a real pointer.
|
|
44
|
+
if (pointer !== '' && typeof anchor === 'string' && !(`#${anchor}` in map)) {
|
|
45
|
+
map[`#${anchor}`] = `#${pointer}`;
|
|
46
|
+
}
|
|
47
|
+
for (const key of Object.keys(record)) {
|
|
48
|
+
if (NON_SCHEMA_KEYWORDS.has(key))
|
|
49
|
+
continue;
|
|
50
|
+
walk(record[key], `${pointer}/${escapeSegment(key)}`);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
walk(rootSchema, '');
|
|
32
54
|
return map;
|
|
33
55
|
};
|
|
@@ -64,6 +64,14 @@ export const escapeRegexPattern = (pattern) => {
|
|
|
64
64
|
// An escape pair like `\/` or `\d`: keep exactly as authored.
|
|
65
65
|
return match;
|
|
66
66
|
});
|
|
67
|
+
// The empty pattern is valid JSON Schema (it matches everything), but an empty
|
|
68
|
+
// regex body would emit `//` \u2014 a line comment, not a literal \u2014 and break the
|
|
69
|
+
// generated file. Emit `(?:)` instead, exactly what `new RegExp('').source`
|
|
70
|
+
// yields: an empty non-capturing group that still matches everything.
|
|
71
|
+
if (escaped === '') {
|
|
72
|
+
escapeCache.set(pattern, '(?:)');
|
|
73
|
+
return '(?:)';
|
|
74
|
+
}
|
|
67
75
|
if (escapeCache.size >= ESCAPE_CACHE_LIMIT) {
|
|
68
76
|
// Evict the single oldest entry (Maps iterate in insertion order) instead
|
|
69
77
|
// of clearing wholesale — a corpus slightly over the limit keeps its hot
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
2
|
/**
|
|
3
|
-
* Collects
|
|
4
|
-
* `$dynamicAnchor
|
|
3
|
+
* Collects a `#/...` ref for every subschema in the document that carries a
|
|
4
|
+
* `$dynamicAnchor` — anywhere in the tree, not just direct `$defs` entries.
|
|
5
5
|
*
|
|
6
6
|
* These definitions are reachable only through `$dynamicRef`, which the ref
|
|
7
7
|
* walker does not follow directly (a `$dynamicRef` is rewritten to a concrete
|
|
@@ -10,6 +10,9 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
|
10
10
|
* generated for each dynamic-anchor target — without this, a generator would
|
|
11
11
|
* emit code that imports a type whose file was never produced.
|
|
12
12
|
*
|
|
13
|
+
* Delegates to {@link buildDynamicRefMap} so the set of seeded refs and the
|
|
14
|
+
* `$dynamicRef` rewrite targets can never disagree.
|
|
15
|
+
*
|
|
13
16
|
* @example
|
|
14
17
|
* ```ts
|
|
15
18
|
* // $defs.schema has $dynamicAnchor: "meta"
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { buildDynamicRefMap } from './build-dynamic-ref-map.js';
|
|
1
2
|
/**
|
|
2
|
-
* Collects
|
|
3
|
-
* `$dynamicAnchor
|
|
3
|
+
* Collects a `#/...` ref for every subschema in the document that carries a
|
|
4
|
+
* `$dynamicAnchor` — anywhere in the tree, not just direct `$defs` entries.
|
|
4
5
|
*
|
|
5
6
|
* These definitions are reachable only through `$dynamicRef`, which the ref
|
|
6
7
|
* walker does not follow directly (a `$dynamicRef` is rewritten to a concrete
|
|
@@ -9,22 +10,13 @@
|
|
|
9
10
|
* generated for each dynamic-anchor target — without this, a generator would
|
|
10
11
|
* emit code that imports a type whose file was never produced.
|
|
11
12
|
*
|
|
13
|
+
* Delegates to {@link buildDynamicRefMap} so the set of seeded refs and the
|
|
14
|
+
* `$dynamicRef` rewrite targets can never disagree.
|
|
15
|
+
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```ts
|
|
14
18
|
* // $defs.schema has $dynamicAnchor: "meta"
|
|
15
19
|
* extractDynamicAnchorDefs(rootSchema) // ['#/$defs/schema']
|
|
16
20
|
* ```
|
|
17
21
|
*/
|
|
18
|
-
export const extractDynamicAnchorDefs = (schema) =>
|
|
19
|
-
const refs = [];
|
|
20
|
-
if (typeof schema !== 'object' || schema === null)
|
|
21
|
-
return refs;
|
|
22
|
-
if (!('$defs' in schema) || typeof schema['$defs'] !== 'object' || schema['$defs'] === null)
|
|
23
|
-
return refs;
|
|
24
|
-
for (const [key, value] of Object.entries(schema['$defs'])) {
|
|
25
|
-
if (typeof value === 'object' && value !== null && '$dynamicAnchor' in value) {
|
|
26
|
-
refs.push(`#/$defs/${key}`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return refs;
|
|
30
|
-
};
|
|
22
|
+
export const extractDynamicAnchorDefs = (schema) => Object.values(buildDynamicRefMap(schema));
|
package/dist/schema-guards.d.ts
CHANGED
|
@@ -124,4 +124,24 @@ export declare const hasDependentRequired: (schema: JSONSchema) => schema is Sch
|
|
|
124
124
|
export declare const hasPropertyNames: (schema: JSONSchema) => schema is SchemaObject & {
|
|
125
125
|
propertyNames: JSONSchema;
|
|
126
126
|
};
|
|
127
|
+
/** Type guard to check if schema has dependentSchemas (2020-12). */
|
|
128
|
+
export declare const hasDependentSchemas: (schema: JSONSchema) => schema is SchemaObject & {
|
|
129
|
+
dependentSchemas: Record<string, JSONSchema>;
|
|
130
|
+
};
|
|
131
|
+
/** Type guard to check if schema has patternProperties. */
|
|
132
|
+
export declare const hasPatternProperties: (schema: JSONSchema) => schema is SchemaObject & {
|
|
133
|
+
patternProperties: Record<string, JSONSchema>;
|
|
134
|
+
};
|
|
135
|
+
/** Type guard to check if schema has a `contains` subschema (array). */
|
|
136
|
+
export declare const hasContains: (schema: JSONSchema) => schema is SchemaObject & {
|
|
137
|
+
contains: JSONSchema;
|
|
138
|
+
};
|
|
139
|
+
/** Type guard to check if schema has a `not` subschema. */
|
|
140
|
+
export declare const hasNot: (schema: JSONSchema) => schema is SchemaObject & {
|
|
141
|
+
not: JSONSchema;
|
|
142
|
+
};
|
|
143
|
+
/** Type guard to check if schema has an `if` subschema (with optional then/else). */
|
|
144
|
+
export declare const hasIf: (schema: JSONSchema) => schema is SchemaObject & {
|
|
145
|
+
if: JSONSchema;
|
|
146
|
+
};
|
|
127
147
|
export { hasRef } from './has-ref.js';
|
package/dist/schema-guards.js
CHANGED
|
@@ -146,4 +146,30 @@ export const hasDependentRequired = (schema) => {
|
|
|
146
146
|
export const hasPropertyNames = (schema) => {
|
|
147
147
|
return isSchemaObject(schema) && 'propertyNames' in schema;
|
|
148
148
|
};
|
|
149
|
+
/** Type guard to check if schema has dependentSchemas (2020-12). */
|
|
150
|
+
export const hasDependentSchemas = (schema) => {
|
|
151
|
+
return (isSchemaObject(schema) &&
|
|
152
|
+
'dependentSchemas' in schema &&
|
|
153
|
+
typeof schema.dependentSchemas === 'object' &&
|
|
154
|
+
schema.dependentSchemas !== null);
|
|
155
|
+
};
|
|
156
|
+
/** Type guard to check if schema has patternProperties. */
|
|
157
|
+
export const hasPatternProperties = (schema) => {
|
|
158
|
+
return (isSchemaObject(schema) &&
|
|
159
|
+
'patternProperties' in schema &&
|
|
160
|
+
typeof schema.patternProperties === 'object' &&
|
|
161
|
+
schema.patternProperties !== null);
|
|
162
|
+
};
|
|
163
|
+
/** Type guard to check if schema has a `contains` subschema (array). */
|
|
164
|
+
export const hasContains = (schema) => {
|
|
165
|
+
return isSchemaObject(schema) && 'contains' in schema;
|
|
166
|
+
};
|
|
167
|
+
/** Type guard to check if schema has a `not` subschema. */
|
|
168
|
+
export const hasNot = (schema) => {
|
|
169
|
+
return isSchemaObject(schema) && 'not' in schema;
|
|
170
|
+
};
|
|
171
|
+
/** Type guard to check if schema has an `if` subschema (with optional then/else). */
|
|
172
|
+
export const hasIf = (schema) => {
|
|
173
|
+
return isSchemaObject(schema) && 'if' in schema;
|
|
174
|
+
};
|
|
149
175
|
export { hasRef } from './has-ref.js';
|
package/dist/walk-ref-graph.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { buildDynamicRefMap } from './build-dynamic-ref-map.js';
|
|
2
|
-
import { extractDynamicAnchorDefs } from './extract-dynamic-anchor-defs.js';
|
|
3
2
|
import { extractRefs } from './extract-refs.js';
|
|
4
3
|
import { refToFilename } from './ref-to-filename.js';
|
|
5
4
|
import { refToName } from './ref-to-name.js';
|
|
@@ -134,10 +133,10 @@ export const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
|
134
133
|
rootSchema: upgraded,
|
|
135
134
|
isRoot: true,
|
|
136
135
|
});
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
];
|
|
136
|
+
// Seed dynamic-anchor targets from the memoized map instead of calling
|
|
137
|
+
// extractDynamicAnchorDefs (which would re-walk the whole document — the map
|
|
138
|
+
// already holds exactly those refs as its values).
|
|
139
|
+
const queue = [...cachedExtractRefs(cache, upgraded), ...Object.values(dynamicRefMap)];
|
|
141
140
|
// Advance a read cursor instead of `queue.shift()`, whose O(n) element move
|
|
142
141
|
// makes draining a large ref graph quadratic.
|
|
143
142
|
for (let head = 0; head < queue.length; head++) {
|
package/package.json
CHANGED
|
@@ -2,6 +2,16 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
|
2
2
|
|
|
3
3
|
import { isSchemaObject } from './schema-guards'
|
|
4
4
|
|
|
5
|
+
/** Escapes a JSON Pointer segment (RFC 6901): `~` → `~0`, `/` → `~1`. */
|
|
6
|
+
const escapeSegment = (segment: string): string =>
|
|
7
|
+
segment.indexOf('~') !== -1 || segment.indexOf('/') !== -1
|
|
8
|
+
? segment.replace(/~/g, '~0').replace(/\//g, '~1')
|
|
9
|
+
: segment
|
|
10
|
+
|
|
11
|
+
// Keywords whose values are data, not subschemas — a `$dynamicAnchor` key inside
|
|
12
|
+
// an enum member or example value is instance data and must not register.
|
|
13
|
+
const NON_SCHEMA_KEYWORDS = new Set(['enum', 'const', 'default', 'examples'])
|
|
14
|
+
|
|
5
15
|
/**
|
|
6
16
|
* Builds a map of $dynamicRef anchor values to their corresponding $ref paths.
|
|
7
17
|
*
|
|
@@ -9,8 +19,11 @@ import { isSchemaObject } from './schema-guards'
|
|
|
9
19
|
* In the OpenAPI spec, $dynamicAnchor: "meta" on the schema definition allows properties
|
|
10
20
|
* like media-type.schema to reference it via $dynamicRef: "#meta".
|
|
11
21
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
22
|
+
* The whole document is scanned — a `$dynamicAnchor` may sit anywhere, not just
|
|
23
|
+
* on a direct `$defs` entry — and each anchor maps to the JSON Pointer of the
|
|
24
|
+
* subschema that declares it. When the same anchor name appears more than once,
|
|
25
|
+
* the first occurrence in document order wins, matching how the resolvers bind
|
|
26
|
+
* a name to a single document-global target.
|
|
14
27
|
*
|
|
15
28
|
* @example
|
|
16
29
|
* // Given a schema with $defs.schema having $dynamicAnchor: "meta"
|
|
@@ -19,24 +32,30 @@ import { isSchemaObject } from './schema-guards'
|
|
|
19
32
|
*/
|
|
20
33
|
export const buildDynamicRefMap = (rootSchema: JSONSchema): Record<string, string> => {
|
|
21
34
|
const map: Record<string, string> = {}
|
|
35
|
+
if (!isSchemaObject(rootSchema)) return map
|
|
22
36
|
|
|
23
|
-
|
|
24
|
-
return
|
|
25
|
-
|
|
37
|
+
const walk = (node: unknown, pointer: string): void => {
|
|
38
|
+
if (node === null || typeof node !== 'object') return
|
|
39
|
+
if (Array.isArray(node)) {
|
|
40
|
+
for (let i = 0; i < node.length; i++) walk(node[i], `${pointer}/${i}`)
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const record = node as Record<string, unknown>
|
|
45
|
+
const anchor = record['$dynamicAnchor']
|
|
46
|
+
// The document root is skipped: mapping it would rewrite `$dynamicRef` to
|
|
47
|
+
// `$ref: "#"`, a self-reference the file-per-definition generators cannot
|
|
48
|
+
// name an output file for. Every nested subschema gets a real pointer.
|
|
49
|
+
if (pointer !== '' && typeof anchor === 'string' && !(`#${anchor}` in map)) {
|
|
50
|
+
map[`#${anchor}`] = `#${pointer}`
|
|
51
|
+
}
|
|
26
52
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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}`
|
|
53
|
+
for (const key of Object.keys(record)) {
|
|
54
|
+
if (NON_SCHEMA_KEYWORDS.has(key)) continue
|
|
55
|
+
walk(record[key], `${pointer}/${escapeSegment(key)}`)
|
|
38
56
|
}
|
|
39
57
|
}
|
|
40
58
|
|
|
59
|
+
walk(rootSchema, '')
|
|
41
60
|
return map
|
|
42
61
|
}
|
|
@@ -67,6 +67,15 @@ export const escapeRegexPattern = (pattern: string): string => {
|
|
|
67
67
|
return match
|
|
68
68
|
})
|
|
69
69
|
|
|
70
|
+
// The empty pattern is valid JSON Schema (it matches everything), but an empty
|
|
71
|
+
// regex body would emit `//` \u2014 a line comment, not a literal \u2014 and break the
|
|
72
|
+
// generated file. Emit `(?:)` instead, exactly what `new RegExp('').source`
|
|
73
|
+
// yields: an empty non-capturing group that still matches everything.
|
|
74
|
+
if (escaped === '') {
|
|
75
|
+
escapeCache.set(pattern, '(?:)')
|
|
76
|
+
return '(?:)'
|
|
77
|
+
}
|
|
78
|
+
|
|
70
79
|
if (escapeCache.size >= ESCAPE_CACHE_LIMIT) {
|
|
71
80
|
// Evict the single oldest entry (Maps iterate in insertion order) instead
|
|
72
81
|
// of clearing wholesale — a corpus slightly over the limit keeps its hot
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
2
|
|
|
3
|
+
import { buildDynamicRefMap } from './build-dynamic-ref-map'
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
|
-
* Collects
|
|
5
|
-
* `$dynamicAnchor
|
|
6
|
+
* Collects a `#/...` ref for every subschema in the document that carries a
|
|
7
|
+
* `$dynamicAnchor` — anywhere in the tree, not just direct `$defs` entries.
|
|
6
8
|
*
|
|
7
9
|
* These definitions are reachable only through `$dynamicRef`, which the ref
|
|
8
10
|
* walker does not follow directly (a `$dynamicRef` is rewritten to a concrete
|
|
@@ -11,23 +13,13 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
|
11
13
|
* generated for each dynamic-anchor target — without this, a generator would
|
|
12
14
|
* emit code that imports a type whose file was never produced.
|
|
13
15
|
*
|
|
16
|
+
* Delegates to {@link buildDynamicRefMap} so the set of seeded refs and the
|
|
17
|
+
* `$dynamicRef` rewrite targets can never disagree.
|
|
18
|
+
*
|
|
14
19
|
* @example
|
|
15
20
|
* ```ts
|
|
16
21
|
* // $defs.schema has $dynamicAnchor: "meta"
|
|
17
22
|
* extractDynamicAnchorDefs(rootSchema) // ['#/$defs/schema']
|
|
18
23
|
* ```
|
|
19
24
|
*/
|
|
20
|
-
export const extractDynamicAnchorDefs = (schema: JSONSchema): string[] =>
|
|
21
|
-
const refs: string[] = []
|
|
22
|
-
|
|
23
|
-
if (typeof schema !== 'object' || schema === null) return refs
|
|
24
|
-
if (!('$defs' in schema) || typeof schema['$defs'] !== 'object' || schema['$defs'] === null) return refs
|
|
25
|
-
|
|
26
|
-
for (const [key, value] of Object.entries(schema['$defs'])) {
|
|
27
|
-
if (typeof value === 'object' && value !== null && '$dynamicAnchor' in value) {
|
|
28
|
-
refs.push(`#/$defs/${key}`)
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return refs
|
|
33
|
-
}
|
|
25
|
+
export const extractDynamicAnchorDefs = (schema: JSONSchema): string[] => Object.values(buildDynamicRefMap(schema))
|
package/src/schema-guards.ts
CHANGED
|
@@ -192,4 +192,43 @@ export const hasPropertyNames = (schema: JSONSchema): schema is SchemaObject & {
|
|
|
192
192
|
return isSchemaObject(schema) && 'propertyNames' in schema
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
/** Type guard to check if schema has dependentSchemas (2020-12). */
|
|
196
|
+
export const hasDependentSchemas = (
|
|
197
|
+
schema: JSONSchema,
|
|
198
|
+
): schema is SchemaObject & { dependentSchemas: Record<string, JSONSchema> } => {
|
|
199
|
+
return (
|
|
200
|
+
isSchemaObject(schema) &&
|
|
201
|
+
'dependentSchemas' in schema &&
|
|
202
|
+
typeof schema.dependentSchemas === 'object' &&
|
|
203
|
+
schema.dependentSchemas !== null
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Type guard to check if schema has patternProperties. */
|
|
208
|
+
export const hasPatternProperties = (
|
|
209
|
+
schema: JSONSchema,
|
|
210
|
+
): schema is SchemaObject & { patternProperties: Record<string, JSONSchema> } => {
|
|
211
|
+
return (
|
|
212
|
+
isSchemaObject(schema) &&
|
|
213
|
+
'patternProperties' in schema &&
|
|
214
|
+
typeof schema.patternProperties === 'object' &&
|
|
215
|
+
schema.patternProperties !== null
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Type guard to check if schema has a `contains` subschema (array). */
|
|
220
|
+
export const hasContains = (schema: JSONSchema): schema is SchemaObject & { contains: JSONSchema } => {
|
|
221
|
+
return isSchemaObject(schema) && 'contains' in schema
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Type guard to check if schema has a `not` subschema. */
|
|
225
|
+
export const hasNot = (schema: JSONSchema): schema is SchemaObject & { not: JSONSchema } => {
|
|
226
|
+
return isSchemaObject(schema) && 'not' in schema
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Type guard to check if schema has an `if` subschema (with optional then/else). */
|
|
230
|
+
export const hasIf = (schema: JSONSchema): schema is SchemaObject & { if: JSONSchema } => {
|
|
231
|
+
return isSchemaObject(schema) && 'if' in schema
|
|
232
|
+
}
|
|
233
|
+
|
|
195
234
|
export { hasRef } from './has-ref'
|
package/src/walk-ref-graph.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
2
|
|
|
3
3
|
import { buildDynamicRefMap } from './build-dynamic-ref-map'
|
|
4
|
-
import { extractDynamicAnchorDefs } from './extract-dynamic-anchor-defs'
|
|
5
4
|
import { extractRefs } from './extract-refs'
|
|
6
5
|
import { refToFilename } from './ref-to-filename'
|
|
7
6
|
import { refToName } from './ref-to-name'
|
|
@@ -200,10 +199,10 @@ export const walkRefGraph = (
|
|
|
200
199
|
isRoot: true,
|
|
201
200
|
})
|
|
202
201
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
]
|
|
202
|
+
// Seed dynamic-anchor targets from the memoized map instead of calling
|
|
203
|
+
// extractDynamicAnchorDefs (which would re-walk the whole document — the map
|
|
204
|
+
// already holds exactly those refs as its values).
|
|
205
|
+
const queue: string[] = [...cachedExtractRefs(cache, upgraded as JSONSchema), ...Object.values(dynamicRefMap)]
|
|
207
206
|
|
|
208
207
|
// Advance a read cursor instead of `queue.shift()`, whose O(n) element move
|
|
209
208
|
// makes draining a large ref graph quadratic.
|