@amartus/oas-utils 0.2.0 → 0.3.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/README.md +70 -3
- package/dist/cli.js +34 -1
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/cleanupDiscriminatorMappings.d.ts +21 -0
- package/dist/lib/cleanupDiscriminatorMappings.d.ts.map +1 -0
- package/dist/lib/cleanupDiscriminatorMappings.js +64 -0
- package/dist/lib/cleanupDiscriminatorMappings.js.map +1 -0
- package/dist/lib/cliActions.d.ts +24 -2
- package/dist/lib/cliActions.d.ts.map +1 -1
- package/dist/lib/cliActions.js +76 -17
- package/dist/lib/cliActions.js.map +1 -1
- package/dist/lib/removeFromOneOfByName.d.ts +1 -0
- package/dist/lib/removeFromOneOfByName.d.ts.map +1 -1
- package/dist/lib/removeFromOneOfByName.js +10 -48
- package/dist/lib/removeFromOneOfByName.js.map +1 -1
- package/dist/lib/schemaTransformUtils.d.ts +83 -0
- package/dist/lib/schemaTransformUtils.d.ts.map +1 -0
- package/dist/lib/schemaTransformUtils.js +192 -0
- package/dist/lib/schemaTransformUtils.js.map +1 -0
- package/dist/lib/sealSchema.d.ts +22 -0
- package/dist/lib/sealSchema.d.ts.map +1 -0
- package/dist/lib/sealSchema.js +420 -0
- package/dist/lib/sealSchema.js.map +1 -0
- package/dist/redocly/cleanup-discriminator-decorator.d.ts +6 -0
- package/dist/redocly/cleanup-discriminator-decorator.d.ts.map +1 -0
- package/dist/redocly/cleanup-discriminator-decorator.js +17 -0
- package/dist/redocly/cleanup-discriminator-decorator.js.map +1 -0
- package/dist/redocly/plugin.d.ts +6 -0
- package/dist/redocly/plugin.d.ts.map +1 -1
- package/dist/redocly/plugin.js +6 -0
- package/dist/redocly/plugin.js.map +1 -1
- package/dist/redocly/seal-schema-decorator.d.ts +6 -0
- package/dist/redocly/seal-schema-decorator.d.ts.map +1 -0
- package/dist/redocly/seal-schema-decorator.js +13 -0
- package/dist/redocly/seal-schema-decorator.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common patterns and utilities for schema traversal and transformation algorithms.
|
|
3
|
+
* Provides reusable building blocks for schema manipulation operations.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Callback for schema transformation during traversal.
|
|
7
|
+
* Return true if schema was modified.
|
|
8
|
+
*/
|
|
9
|
+
export type SchemaTransformer = (node: any) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Predicate to filter schemas during traversal
|
|
12
|
+
*/
|
|
13
|
+
export type SchemaPredicate = (node: any) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Traverses all nodes in an OAS document recursively (depth-first).
|
|
16
|
+
* Applies transformer callback to each node, counts modifications.
|
|
17
|
+
*
|
|
18
|
+
* @param node - Root node to traverse
|
|
19
|
+
* @param transformer - Function called on each node, returns true if modified
|
|
20
|
+
* @returns Number of nodes modified
|
|
21
|
+
*/
|
|
22
|
+
export declare function traverseAndTransform(node: any, transformer: SchemaTransformer): number;
|
|
23
|
+
/**
|
|
24
|
+
* Collects all schemas matching a predicate during traversal.
|
|
25
|
+
*
|
|
26
|
+
* @param node - Root node to traverse
|
|
27
|
+
* @param predicate - Function to test each node
|
|
28
|
+
* @returns Array of matching nodes
|
|
29
|
+
*/
|
|
30
|
+
export declare function collectMatching(node: any, predicate: SchemaPredicate): any[];
|
|
31
|
+
/**
|
|
32
|
+
* Updates discriminator mappings in a schema node.
|
|
33
|
+
* Removes mapping entries where the predicate returns false.
|
|
34
|
+
*
|
|
35
|
+
* @param node - Schema node with discriminator
|
|
36
|
+
* @param keepPredicate - Returns true if mapping entry should be kept
|
|
37
|
+
* @returns Number of mappings removed
|
|
38
|
+
*/
|
|
39
|
+
export declare function updateDiscriminatorMappings(node: any, keepPredicate: (key: string, ref: string) => boolean): number;
|
|
40
|
+
/**
|
|
41
|
+
* Filters a collection within a schema node by predicate.
|
|
42
|
+
* Mutates the array in place.
|
|
43
|
+
*
|
|
44
|
+
* @param node - Schema node with collection property
|
|
45
|
+
* @param propertyName - Name of array property (e.g., 'oneOf', 'anyOf')
|
|
46
|
+
* @param keepPredicate - Returns true if item should be kept
|
|
47
|
+
* @returns Number of items removed
|
|
48
|
+
*/
|
|
49
|
+
export declare function filterSchemaCollection(node: any, propertyName: string, keepPredicate: (item: any) => boolean): number;
|
|
50
|
+
/**
|
|
51
|
+
* Extracts schema name from a $ref string in a collection item.
|
|
52
|
+
* Returns undefined if item is not a $ref or is invalid.
|
|
53
|
+
*
|
|
54
|
+
* @param item - Item from schema collection (e.g., from oneOf, anyOf)
|
|
55
|
+
* @returns Schema name or undefined
|
|
56
|
+
*/
|
|
57
|
+
export declare function getRefFromCollectionItem(item: any): string | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Removes items from a schema collection that reference specific schemas.
|
|
60
|
+
* Also updates discriminator mappings for the removed schema names.
|
|
61
|
+
*
|
|
62
|
+
* @param node - Parent schema node
|
|
63
|
+
* @param propertyName - Collection property name (e.g., 'oneOf')
|
|
64
|
+
* @param schemaNames - Set of schema names to remove
|
|
65
|
+
* @returns true if any modifications were made
|
|
66
|
+
*/
|
|
67
|
+
export declare function removeFromCollectionAndUpdateDiscriminator(node: any, propertyName: string, schemaNames: Set<string>): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Clones a schema object with all its nested properties.
|
|
70
|
+
*
|
|
71
|
+
* @param schema - Schema to clone
|
|
72
|
+
* @returns Deep clone of schema
|
|
73
|
+
*/
|
|
74
|
+
export declare function cloneSchema<T extends any>(schema: T): T;
|
|
75
|
+
/**
|
|
76
|
+
* Gets all schemas that reference a given schema name via $ref.
|
|
77
|
+
*
|
|
78
|
+
* @param schemas - Map of schemas
|
|
79
|
+
* @param targetName - Name of schema to find references to
|
|
80
|
+
* @returns Set of schema names that reference the target
|
|
81
|
+
*/
|
|
82
|
+
export declare function getSchemaReferencers(schemas: Record<string, any>, targetName: string): Set<string>;
|
|
83
|
+
//# sourceMappingURL=schemaTransformUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaTransformUtils.d.ts","sourceRoot":"","sources":["../../src/lib/schemaTransformUtils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC;AAErD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,GAAG,EAAE,WAAW,EAAE,iBAAiB,GAAG,MAAM,CAoBtF;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,eAAe,GAAG,GAAG,EAAE,CAmB5E;AAED;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CACzC,IAAI,EAAE,GAAG,EACT,aAAa,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,OAAO,GACnD,MAAM,CAgBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,GAAG,EACT,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,GACpC,MAAM,CAQR;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,GAAG,SAAS,CAKtE;AAED;;;;;;;;GAQG;AACH,wBAAgB,0CAA0C,CACxD,IAAI,EAAE,GAAG,EACT,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,GACvB,OAAO,CA8BT;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,GAAG,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAcvD;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAqBlG"}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common patterns and utilities for schema traversal and transformation algorithms.
|
|
3
|
+
* Provides reusable building blocks for schema manipulation operations.
|
|
4
|
+
*/
|
|
5
|
+
import { refToName } from "./oasUtils.js";
|
|
6
|
+
/**
|
|
7
|
+
* Traverses all nodes in an OAS document recursively (depth-first).
|
|
8
|
+
* Applies transformer callback to each node, counts modifications.
|
|
9
|
+
*
|
|
10
|
+
* @param node - Root node to traverse
|
|
11
|
+
* @param transformer - Function called on each node, returns true if modified
|
|
12
|
+
* @returns Number of nodes modified
|
|
13
|
+
*/
|
|
14
|
+
export function traverseAndTransform(node, transformer) {
|
|
15
|
+
if (!node || typeof node !== "object")
|
|
16
|
+
return 0;
|
|
17
|
+
let modified = 0;
|
|
18
|
+
if (transformer(node)) {
|
|
19
|
+
modified++;
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(node)) {
|
|
22
|
+
for (const item of node) {
|
|
23
|
+
modified += traverseAndTransform(item, transformer);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
for (const value of Object.values(node)) {
|
|
28
|
+
modified += traverseAndTransform(value, transformer);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return modified;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Collects all schemas matching a predicate during traversal.
|
|
35
|
+
*
|
|
36
|
+
* @param node - Root node to traverse
|
|
37
|
+
* @param predicate - Function to test each node
|
|
38
|
+
* @returns Array of matching nodes
|
|
39
|
+
*/
|
|
40
|
+
export function collectMatching(node, predicate) {
|
|
41
|
+
const results = [];
|
|
42
|
+
function traverse(n) {
|
|
43
|
+
if (!n || typeof n !== "object")
|
|
44
|
+
return;
|
|
45
|
+
if (predicate(n)) {
|
|
46
|
+
results.push(n);
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(n)) {
|
|
49
|
+
for (const item of n)
|
|
50
|
+
traverse(item);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
for (const value of Object.values(n))
|
|
54
|
+
traverse(value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
traverse(node);
|
|
58
|
+
return results;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Updates discriminator mappings in a schema node.
|
|
62
|
+
* Removes mapping entries where the predicate returns false.
|
|
63
|
+
*
|
|
64
|
+
* @param node - Schema node with discriminator
|
|
65
|
+
* @param keepPredicate - Returns true if mapping entry should be kept
|
|
66
|
+
* @returns Number of mappings removed
|
|
67
|
+
*/
|
|
68
|
+
export function updateDiscriminatorMappings(node, keepPredicate) {
|
|
69
|
+
if (!node?.discriminator?.mapping || typeof node.discriminator.mapping !== "object") {
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
let removed = 0;
|
|
73
|
+
const mapping = node.discriminator.mapping;
|
|
74
|
+
for (const [key, ref] of Object.entries(mapping)) {
|
|
75
|
+
if (typeof ref === "string" && !keepPredicate(key, ref)) {
|
|
76
|
+
delete mapping[key];
|
|
77
|
+
removed++;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return removed;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Filters a collection within a schema node by predicate.
|
|
84
|
+
* Mutates the array in place.
|
|
85
|
+
*
|
|
86
|
+
* @param node - Schema node with collection property
|
|
87
|
+
* @param propertyName - Name of array property (e.g., 'oneOf', 'anyOf')
|
|
88
|
+
* @param keepPredicate - Returns true if item should be kept
|
|
89
|
+
* @returns Number of items removed
|
|
90
|
+
*/
|
|
91
|
+
export function filterSchemaCollection(node, propertyName, keepPredicate) {
|
|
92
|
+
if (!node || !Array.isArray(node[propertyName])) {
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
const before = node[propertyName].length;
|
|
96
|
+
node[propertyName] = node[propertyName].filter(keepPredicate);
|
|
97
|
+
return before - node[propertyName].length;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Extracts schema name from a $ref string in a collection item.
|
|
101
|
+
* Returns undefined if item is not a $ref or is invalid.
|
|
102
|
+
*
|
|
103
|
+
* @param item - Item from schema collection (e.g., from oneOf, anyOf)
|
|
104
|
+
* @returns Schema name or undefined
|
|
105
|
+
*/
|
|
106
|
+
export function getRefFromCollectionItem(item) {
|
|
107
|
+
if (item && typeof item === "object" && typeof item.$ref === "string") {
|
|
108
|
+
return refToName(item.$ref);
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Removes items from a schema collection that reference specific schemas.
|
|
114
|
+
* Also updates discriminator mappings for the removed schema names.
|
|
115
|
+
*
|
|
116
|
+
* @param node - Parent schema node
|
|
117
|
+
* @param propertyName - Collection property name (e.g., 'oneOf')
|
|
118
|
+
* @param schemaNames - Set of schema names to remove
|
|
119
|
+
* @returns true if any modifications were made
|
|
120
|
+
*/
|
|
121
|
+
export function removeFromCollectionAndUpdateDiscriminator(node, propertyName, schemaNames) {
|
|
122
|
+
let changed = false;
|
|
123
|
+
// Remove from collection
|
|
124
|
+
if (Array.isArray(node[propertyName])) {
|
|
125
|
+
const before = node[propertyName].length;
|
|
126
|
+
node[propertyName] = node[propertyName].filter((item) => {
|
|
127
|
+
const refName = getRefFromCollectionItem(item);
|
|
128
|
+
return !refName || !schemaNames.has(refName);
|
|
129
|
+
});
|
|
130
|
+
if (before !== node[propertyName].length) {
|
|
131
|
+
changed = true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Update discriminator mappings
|
|
135
|
+
if (node.discriminator?.mapping && typeof node.discriminator.mapping === "object") {
|
|
136
|
+
const mapping = node.discriminator.mapping;
|
|
137
|
+
for (const schemaName of schemaNames) {
|
|
138
|
+
for (const [key, ref] of Object.entries(mapping)) {
|
|
139
|
+
const refName = refToName(ref);
|
|
140
|
+
if (refName === schemaName) {
|
|
141
|
+
delete mapping[key];
|
|
142
|
+
changed = true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return changed;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Clones a schema object with all its nested properties.
|
|
151
|
+
*
|
|
152
|
+
* @param schema - Schema to clone
|
|
153
|
+
* @returns Deep clone of schema
|
|
154
|
+
*/
|
|
155
|
+
export function cloneSchema(schema) {
|
|
156
|
+
if (schema === null || typeof schema !== "object") {
|
|
157
|
+
return schema;
|
|
158
|
+
}
|
|
159
|
+
if (Array.isArray(schema)) {
|
|
160
|
+
return schema.map(cloneSchema);
|
|
161
|
+
}
|
|
162
|
+
const clone = {};
|
|
163
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
164
|
+
clone[key] = cloneSchema(value);
|
|
165
|
+
}
|
|
166
|
+
return clone;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Gets all schemas that reference a given schema name via $ref.
|
|
170
|
+
*
|
|
171
|
+
* @param schemas - Map of schemas
|
|
172
|
+
* @param targetName - Name of schema to find references to
|
|
173
|
+
* @returns Set of schema names that reference the target
|
|
174
|
+
*/
|
|
175
|
+
export function getSchemaReferencers(schemas, targetName) {
|
|
176
|
+
const targetRef = `#/components/schemas/${targetName}`;
|
|
177
|
+
const referencers = new Set();
|
|
178
|
+
for (const [schemaName, schema] of Object.entries(schemas)) {
|
|
179
|
+
if (!schema || typeof schema !== "object")
|
|
180
|
+
continue;
|
|
181
|
+
const hasRef = collectMatching(schema, (node) => {
|
|
182
|
+
return (node &&
|
|
183
|
+
typeof node === "object" &&
|
|
184
|
+
(node.$ref === targetRef || (Array.isArray(node) && node.some((item) => item?.$ref === targetRef))));
|
|
185
|
+
}).length > 0;
|
|
186
|
+
if (hasRef) {
|
|
187
|
+
referencers.add(schemaName);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return referencers;
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=schemaTransformUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaTransformUtils.js","sourceRoot":"","sources":["../../src/lib/schemaTransformUtils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAa1C;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAAS,EAAE,WAA8B;IAC5E,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,CAAC,CAAC;IAEhD,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;QACtB,QAAQ,EAAE,CAAC;IACb,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC;YACxB,QAAQ,IAAI,oBAAoB,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;QACtD,CAAC;IACH,CAAC;SAAM,CAAC;QACN,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;YACxC,QAAQ,IAAI,oBAAoB,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,IAAS,EAAE,SAA0B;IACnE,MAAM,OAAO,GAAU,EAAE,CAAC;IAE1B,SAAS,QAAQ,CAAC,CAAM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ED;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAA4B,EAAE,UAAkB;IACnF,MAAM,SAAS,GAAG,wBAAwB,UAAU,EAAE,CAAC;IACvD,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAC;IAEtC,KAAK,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3D,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ;YAAE,SAAS;QAEpD,MAAM,MAAM,GAAG,eAAe,CAAC,MAAM,EAAE,CAAC,IAAS,EAAE,EAAE;YACnD,OAAO,CACL,IAAI;gBACJ,OAAO,IAAI,KAAK,QAAQ;gBACxB,CAAE,IAAY,CAAC,IAAI,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,CAClH,CAAC;QACJ,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;QAEd,IAAI,MAAM,EAAE,CAAC;YACX,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface SealSchemaOptions {
|
|
2
|
+
/** If true, use unevaluatedProperties: false instead of additionalProperties: false (default: true) */
|
|
3
|
+
useUnevaluatedProperties?: boolean;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Seal OpenAPI/JSON Schema objects to prevent additional properties.
|
|
7
|
+
*
|
|
8
|
+
* This ensures every final object shape exposed in the API is sealed (no additional properties allowed),
|
|
9
|
+
* without breaking schemas that are extended via allOf.
|
|
10
|
+
*
|
|
11
|
+
* Algorithm:
|
|
12
|
+
* 1. Index the schema and identify all $ref usages (extension vs direct)
|
|
13
|
+
* 2. Classify object-type schemas (pre-sealed, core-candidate, direct-only)
|
|
14
|
+
* 3. For core-candidates: create Core variant + sealed wrapper
|
|
15
|
+
* 4. Rewrite refs inside allOf to point to Core variants
|
|
16
|
+
* 5. Seal composition roots (allOf/anyOf/oneOf) and direct objects
|
|
17
|
+
*
|
|
18
|
+
* @param doc - OpenAPI document or standalone JSON Schema to transform
|
|
19
|
+
* @param opts - Optional configuration
|
|
20
|
+
*/
|
|
21
|
+
export declare function sealSchema(doc: any, opts?: SealSchemaOptions): any;
|
|
22
|
+
//# sourceMappingURL=sealSchema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sealSchema.d.ts","sourceRoot":"","sources":["../../src/lib/sealSchema.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,iBAAiB;IAChC,uGAAuG;IACvG,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACpC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,iBAAsB,GAAG,GAAG,CAqItE"}
|