@skmtc/core 0.46.0 → 0.47.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.
Files changed (108) hide show
  1. package/README.md +7 -1
  2. package/esm/context/ParseContext.d.ts +3 -0
  3. package/esm/context/ParseContext.js +17 -7
  4. package/esm/context/generateTypes.d.ts +2 -2
  5. package/esm/helpers/collateExamples.js +35 -17
  6. package/esm/mod.d.ts +1 -0
  7. package/esm/mod.js +1 -0
  8. package/esm/oas/intersection/Intersection.d.ts +122 -0
  9. package/esm/oas/intersection/Intersection.js +266 -0
  10. package/esm/oas/mediaType/jsonMediaType.d.ts +1 -1
  11. package/esm/oas/mediaType/jsonMediaType.js +3 -3
  12. package/esm/oas/requestBody/RequestBody.d.ts +1 -1
  13. package/esm/oas/requestBody/RequestBody.js +1 -1
  14. package/esm/oas/response/Response.d.ts +1 -1
  15. package/esm/oas/response/Response.js +1 -1
  16. package/esm/oas/schema/Schema.d.ts +4 -2
  17. package/esm/oas/schema/siblings.d.ts +10 -2
  18. package/esm/oas/schema/siblings.js +87 -22
  19. package/esm/oas/schemaPath/traverseSchema.d.ts +2 -0
  20. package/esm/oas/schemaPath/traverseSchema.js +40 -0
  21. package/esm/parse/allOfSystem.d.ts +3 -0
  22. package/esm/parse/allOfSystem.js +2 -1
  23. package/esm/parse/keptIntersection.d.ts +53 -0
  24. package/esm/parse/keptIntersection.js +155 -0
  25. package/esm/parse/keptRefs.d.ts +23 -0
  26. package/esm/parse/keptRefs.js +59 -0
  27. package/esm/parse/toCyclicRefs.d.ts +8 -0
  28. package/esm/parse/toCyclicRefs.js +126 -0
  29. package/esm/parse/toDeclaredTypes.d.ts +20 -0
  30. package/esm/parse/toDeclaredTypes.js +43 -0
  31. package/esm/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
  32. package/esm/parse/v3-0/_merge-all-of/merge-union.js +7 -1
  33. package/esm/parse/v3-0/_merge-all-of/merge.js +13 -0
  34. package/esm/parse/v3-0/_merge-all-of/types.d.ts +3 -0
  35. package/esm/parse/v3-0/intersection/toIntersection.d.ts +12 -0
  36. package/esm/parse/v3-0/intersection/toIntersection.js +61 -0
  37. package/esm/parse/v3-0/schema/toSchemasV3.js +13 -1
  38. package/esm/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
  39. package/esm/parse/v3-1/_merge-all-of/merge-union.js +7 -1
  40. package/esm/parse/v3-1/_merge-all-of/merge.js +13 -0
  41. package/esm/parse/v3-1/_merge-all-of/types.d.ts +3 -0
  42. package/esm/parse/v3-1/intersection/toIntersection.d.ts +12 -0
  43. package/esm/parse/v3-1/intersection/toIntersection.js +61 -0
  44. package/esm/parse/v3-1/schema/toSchemasV3.js +15 -2
  45. package/esm/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
  46. package/esm/parse/v3-2/_merge-all-of/merge-union.js +7 -1
  47. package/esm/parse/v3-2/_merge-all-of/merge.js +13 -0
  48. package/esm/parse/v3-2/_merge-all-of/types.d.ts +3 -0
  49. package/esm/parse/v3-2/intersection/toIntersection.d.ts +12 -0
  50. package/esm/parse/v3-2/intersection/toIntersection.js +61 -0
  51. package/esm/parse/v3-2/schema/toSchemasV3.js +15 -2
  52. package/esm/types/Settings.d.ts +9 -0
  53. package/esm/types/Settings.js +2 -1
  54. package/esm/types/TypeSystem.d.ts +17 -2
  55. package/package.json +1 -1
  56. package/script/context/ParseContext.d.ts +3 -0
  57. package/script/context/ParseContext.js +17 -7
  58. package/script/context/generateTypes.d.ts +2 -2
  59. package/script/helpers/collateExamples.js +35 -17
  60. package/script/mod.d.ts +1 -0
  61. package/script/mod.js +1 -0
  62. package/script/oas/intersection/Intersection.d.ts +122 -0
  63. package/script/oas/intersection/Intersection.js +270 -0
  64. package/script/oas/mediaType/jsonMediaType.d.ts +1 -1
  65. package/script/oas/mediaType/jsonMediaType.js +3 -3
  66. package/script/oas/requestBody/RequestBody.d.ts +1 -1
  67. package/script/oas/requestBody/RequestBody.js +1 -1
  68. package/script/oas/response/Response.d.ts +1 -1
  69. package/script/oas/response/Response.js +1 -1
  70. package/script/oas/schema/Schema.d.ts +4 -2
  71. package/script/oas/schema/siblings.d.ts +10 -2
  72. package/script/oas/schema/siblings.js +89 -23
  73. package/script/oas/schemaPath/traverseSchema.d.ts +2 -0
  74. package/script/oas/schemaPath/traverseSchema.js +40 -0
  75. package/script/parse/allOfSystem.d.ts +3 -0
  76. package/script/parse/allOfSystem.js +2 -1
  77. package/script/parse/keptIntersection.d.ts +53 -0
  78. package/script/parse/keptIntersection.js +164 -0
  79. package/script/parse/keptRefs.d.ts +23 -0
  80. package/script/parse/keptRefs.js +63 -0
  81. package/script/parse/toCyclicRefs.d.ts +8 -0
  82. package/script/parse/toCyclicRefs.js +130 -0
  83. package/script/parse/toDeclaredTypes.d.ts +20 -0
  84. package/script/parse/toDeclaredTypes.js +48 -0
  85. package/script/parse/v3-0/_merge-all-of/merge-intersection.js +8 -4
  86. package/script/parse/v3-0/_merge-all-of/merge-union.js +7 -1
  87. package/script/parse/v3-0/_merge-all-of/merge.js +13 -0
  88. package/script/parse/v3-0/_merge-all-of/types.d.ts +3 -0
  89. package/script/parse/v3-0/intersection/toIntersection.d.ts +12 -0
  90. package/script/parse/v3-0/intersection/toIntersection.js +65 -0
  91. package/script/parse/v3-0/schema/toSchemasV3.js +13 -1
  92. package/script/parse/v3-1/_merge-all-of/merge-intersection.js +8 -4
  93. package/script/parse/v3-1/_merge-all-of/merge-union.js +7 -1
  94. package/script/parse/v3-1/_merge-all-of/merge.js +13 -0
  95. package/script/parse/v3-1/_merge-all-of/types.d.ts +3 -0
  96. package/script/parse/v3-1/intersection/toIntersection.d.ts +12 -0
  97. package/script/parse/v3-1/intersection/toIntersection.js +65 -0
  98. package/script/parse/v3-1/schema/toSchemasV3.js +15 -2
  99. package/script/parse/v3-2/_merge-all-of/merge-intersection.js +8 -4
  100. package/script/parse/v3-2/_merge-all-of/merge-union.js +7 -1
  101. package/script/parse/v3-2/_merge-all-of/merge.js +13 -0
  102. package/script/parse/v3-2/_merge-all-of/types.d.ts +3 -0
  103. package/script/parse/v3-2/intersection/toIntersection.d.ts +12 -0
  104. package/script/parse/v3-2/intersection/toIntersection.js +65 -0
  105. package/script/parse/v3-2/schema/toSchemasV3.js +15 -2
  106. package/script/types/Settings.d.ts +9 -0
  107. package/script/types/Settings.js +2 -1
  108. package/script/types/TypeSystem.d.ts +17 -2
@@ -0,0 +1,270 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var _OasIntersection_instances, _OasIntersection_toMembersInside;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.OasIntersection = void 0;
10
+ const traverseSchema_js_1 = require("../schemaPath/traverseSchema.js");
11
+ const Object_js_1 = require("../object/Object.js");
12
+ const siblings_js_1 = require("../schema/siblings.js");
13
+ const OasBase_js_1 = require("../../types/OasBase.js");
14
+ const DanglingRefError_js_1 = require("../ref/DanglingRefError.js");
15
+ /**
16
+ * An `allOf` that keeps at least one reference: a value matches every member.
17
+ *
18
+ * Core merges the members of an `allOf` into one schema. A reference that
19
+ * `client.json` `system.intersections` keeps is not merged: the schema is an
20
+ * `OasIntersection` of the kept references, and at most one other member, which
21
+ * the other members of the `allOf` merge into. By default those are the
22
+ * references that close a cycle.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * // LineString: { allOf: [{ $ref: Geometry }, { properties: { coordinates } }] },
27
+ * // where a property of Geometry leads back to LineString
28
+ * lineString.members // [OasRef Geometry, OasObject { coordinates }]
29
+ * ```
30
+ */
31
+ class OasIntersection extends OasBase_js_1.OasBase {
32
+ constructor(fields, context) {
33
+ super(context);
34
+ _OasIntersection_instances.add(this);
35
+ /**
36
+ * Object is part the 'schema' set which is used
37
+ * to define data types in an OpenAPI document.
38
+ */
39
+ Object.defineProperty(this, "oasType", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: 'schema'
44
+ });
45
+ /**
46
+ * Constant value 'intersection' useful for type narrowing and tagged unions.
47
+ */
48
+ Object.defineProperty(this, "type", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: 'intersection'
53
+ });
54
+ /** A short summary of the intersection. */
55
+ Object.defineProperty(this, "title", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ /** A description of the intersection. */
62
+ Object.defineProperty(this, "description", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
68
+ /** External documentation for the intersection. */
69
+ Object.defineProperty(this, "externalDocs", {
70
+ enumerable: true,
71
+ configurable: true,
72
+ writable: true,
73
+ value: void 0
74
+ });
75
+ /** Indicates whether the value can be null. */
76
+ Object.defineProperty(this, "nullable", {
77
+ enumerable: true,
78
+ configurable: true,
79
+ writable: true,
80
+ value: void 0
81
+ });
82
+ /** Whether the value is sent by the API only. */
83
+ Object.defineProperty(this, "readOnly", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ /** Whether the value is sent in a request only. */
90
+ Object.defineProperty(this, "writeOnly", {
91
+ enumerable: true,
92
+ configurable: true,
93
+ writable: true,
94
+ value: void 0
95
+ });
96
+ /** Whether the value is deprecated. */
97
+ Object.defineProperty(this, "deprecated", {
98
+ enumerable: true,
99
+ configurable: true,
100
+ writable: true,
101
+ value: void 0
102
+ });
103
+ /** The schemas, or references to schemas, that a value matches all of. */
104
+ Object.defineProperty(this, "members", {
105
+ enumerable: true,
106
+ configurable: true,
107
+ writable: true,
108
+ value: void 0
109
+ });
110
+ /** Specification Extension fields */
111
+ Object.defineProperty(this, "extensionFields", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: void 0
116
+ });
117
+ /** An example of the value. */
118
+ Object.defineProperty(this, "example", {
119
+ enumerable: true,
120
+ configurable: true,
121
+ writable: true,
122
+ value: void 0
123
+ });
124
+ /** The default value. */
125
+ Object.defineProperty(this, "default", {
126
+ enumerable: true,
127
+ configurable: true,
128
+ writable: true,
129
+ value: void 0
130
+ });
131
+ /**
132
+ * The schema's `not`, kept as an `OasUnknown` under `client.json`
133
+ * `system.not: unknown`. `undefined` otherwise.
134
+ */
135
+ Object.defineProperty(this, "not", {
136
+ enumerable: true,
137
+ configurable: true,
138
+ writable: true,
139
+ value: undefined
140
+ });
141
+ this.title = fields.title;
142
+ this.description = fields.description;
143
+ this.externalDocs = fields.externalDocs;
144
+ this.nullable = fields.nullable;
145
+ this.readOnly = fields.readOnly;
146
+ this.writeOnly = fields.writeOnly;
147
+ this.deprecated = fields.deprecated;
148
+ this.members = fields.members;
149
+ this.extensionFields = fields.extensionFields;
150
+ this.example = fields.example;
151
+ this.default = fields.default;
152
+ }
153
+ isRef() {
154
+ return false;
155
+ }
156
+ /** Whether the value is `readOnly`: the intersection or one of its members is. */
157
+ isReadOnly() {
158
+ return __classPrivateFieldGet(this, _OasIntersection_instances, "m", _OasIntersection_toMembersInside).call(this).some(member => member.isRef() || member.type === 'intersection' ? member.readOnly === true : member.isReadOnly());
159
+ }
160
+ /** Whether the value is `writeOnly`: the intersection or one of its members is. */
161
+ isWriteOnly() {
162
+ return __classPrivateFieldGet(this, _OasIntersection_instances, "m", _OasIntersection_toMembersInside).call(this).some(member => member.isRef() || member.type === 'intersection' ? member.writeOnly === true : member.isWriteOnly());
163
+ }
164
+ /**
165
+ * The schemas a value matches all of, with each reference resolved and each
166
+ * intersection inside replaced by its own members. Each is given once, so a
167
+ * member that leads back to an intersection ends. A reference that names
168
+ * nothing is left out.
169
+ */
170
+ toParts() {
171
+ return __classPrivateFieldGet(this, _OasIntersection_instances, "m", _OasIntersection_toMembersInside).call(this).filter(isPart);
172
+ }
173
+ /**
174
+ * {@link toParts} without the keywords next to the references. `clone` reads
175
+ * these while a reference to this intersection resolves, so no reference may
176
+ * be resolved with its keywords here.
177
+ */
178
+ toTargetParts() {
179
+ return __classPrivateFieldGet(this, _OasIntersection_instances, "m", _OasIntersection_toMembersInside).call(this, ref => ref.resolveOnce()).filter(isPart);
180
+ }
181
+ /**
182
+ * A copy of this schema with what `ref` adds at its site applied, as
183
+ * {@link OasUnion.clone} does: the keywords that this type takes, a
184
+ * sibling's value winning. The keywords of an object next to `ref`
185
+ * (`properties`, `required`) are one more member, unless the members say
186
+ * the same. This schema does not
187
+ * change, and the copy shares its members.
188
+ */
189
+ clone(ref) {
190
+ const copy = new OasIntersection({
191
+ ...(0, siblings_js_1.toSiblingAnnotations)(this, ref),
192
+ example: ref?.example ?? this.example,
193
+ externalDocs: this.externalDocs,
194
+ nullable: this.nullable || ref?.nullable || undefined,
195
+ readOnly: this.readOnly || ref?.readOnly || undefined,
196
+ writeOnly: this.writeOnly || ref?.writeOnly || undefined,
197
+ deprecated: ref?.deprecated ?? this.deprecated,
198
+ members: ref !== undefined && (0, siblings_js_1.toChangedObjectKeywords)(this, ref).length > 0 ? [...this.members, toSiblingMember(ref)] : this.members,
199
+ default: ref?.default ?? this.default
200
+ });
201
+ copy.stackTrail = this.stackTrail;
202
+ copy.not = this.not;
203
+ return copy;
204
+ }
205
+ traverse(path) {
206
+ return (0, traverseSchema_js_1.traverseSchema)(this, path);
207
+ }
208
+ resolve() {
209
+ return this;
210
+ }
211
+ resolveOnce() {
212
+ return this;
213
+ }
214
+ toJsonSchema(options) {
215
+ return {
216
+ title: this.title,
217
+ description: this.description,
218
+ nullable: this.nullable,
219
+ readOnly: this.readOnly,
220
+ writeOnly: this.writeOnly,
221
+ deprecated: this.deprecated,
222
+ example: this.example,
223
+ default: this.default,
224
+ allOf: this.members.map(member => member.toJsonSchema(options))
225
+ };
226
+ }
227
+ }
228
+ exports.OasIntersection = OasIntersection;
229
+ _OasIntersection_instances = new WeakSet(), _OasIntersection_toMembersInside = function _OasIntersection_toMembersInside(follow = ref => ref.resolve()) {
230
+ const found = new Set();
231
+ const visit = (item) => {
232
+ if (item === undefined || found.has(item)) {
233
+ return;
234
+ }
235
+ found.add(item);
236
+ if (item.isRef()) {
237
+ visit(toTarget(() => follow(item)));
238
+ }
239
+ else if (item.type === 'intersection') {
240
+ item.members.forEach(visit);
241
+ }
242
+ };
243
+ visit(this);
244
+ return [...found];
245
+ };
246
+ /** The keywords of an object next to a reference, as an object. */
247
+ const toSiblingMember = (ref) => {
248
+ const member = new Object_js_1.OasObject({
249
+ properties: ref.properties,
250
+ required: ref.required,
251
+ additionalProperties: ref.additionalProperties,
252
+ minProperties: ref.minProperties,
253
+ maxProperties: ref.maxProperties
254
+ });
255
+ member.stackTrail = ref.stackTrail;
256
+ return member;
257
+ };
258
+ const isPart = (member) => !member.isRef() && member.type !== 'intersection';
259
+ /** What following a reference gives, or `undefined` when it names nothing. */
260
+ const toTarget = (follow) => {
261
+ try {
262
+ return follow();
263
+ }
264
+ catch (error) {
265
+ if (DanglingRefError_js_1.DanglingRefError.is(error)) {
266
+ return undefined;
267
+ }
268
+ throw error;
269
+ }
270
+ };
@@ -40,7 +40,7 @@ export type JsonContent = {
40
40
  * Choose the media type a body is read from: `application/json` exactly when
41
41
  * it is a key, else the first JSON-like key in document order (see
42
42
  * {@link isJsonMediaType}), else the `*\/*` key when its schema is an object,
43
- * array, union or `$ref`. `*\/*` admits JSON, and documents use it for JSON
43
+ * array, union, intersection or `$ref`. `*\/*` admits JSON, and documents use it for JSON
44
44
  * bodies (springdoc writes it for every response); the parser logs
45
45
  * `WILDCARD_MEDIA_TYPE` for every body read this way. `undefined` otherwise:
46
46
  * there is no fall back to a specific non-JSON type, to a narrower range such
@@ -33,16 +33,16 @@ const isAnyMediaType = (mediaType) => (0, exports.toMediaTypeEssence)(mediaType)
33
33
  exports.isAnyMediaType = isAnyMediaType;
34
34
  /**
35
35
  * True when a schema under `*\/*` can be read as one JSON value: an object,
36
- * array, union or `$ref`. A scalar is not: under `*\/*` a `string` is usually
36
+ * array, union, intersection or `$ref`. A scalar is not: under `*\/*` a `string` is usually
37
37
  * a text body, and `string` with `format: binary` a file.
38
38
  */
39
39
  const isJsonShaped = (schema) => schema !== undefined &&
40
- (schema.isRef() || schema.type === 'object' || schema.type === 'array' || schema.type === 'union');
40
+ (schema.isRef() || ['object', 'array', 'union', 'intersection'].includes(schema.type));
41
41
  /**
42
42
  * Choose the media type a body is read from: `application/json` exactly when
43
43
  * it is a key, else the first JSON-like key in document order (see
44
44
  * {@link isJsonMediaType}), else the `*\/*` key when its schema is an object,
45
- * array, union or `$ref`. `*\/*` admits JSON, and documents use it for JSON
45
+ * array, union, intersection or `$ref`. `*\/*` admits JSON, and documents use it for JSON
46
46
  * bodies (springdoc writes it for every response); the parser logs
47
47
  * `WILDCARD_MEDIA_TYPE` for every body read this way. `undefined` otherwise:
48
48
  * there is no fall back to a specific non-JSON type, to a narrower range such
@@ -296,7 +296,7 @@ export declare class OasRequestBody extends OasBase {
296
296
  * `mediaType`, the schema of the JSON media type that {@link toJsonContent}
297
297
  * picks: `application/json`, else the first JSON-like type such as
298
298
  * `application/json;v=1.0` or `application/vnd.api+json`, else `*\/*`
299
- * when its schema is an object, array, union or `$ref`.
299
+ * when its schema is an object, array, union, intersection or `$ref`.
300
300
  * `undefined` when there is no such media type or it has no schema.
301
301
  */
302
302
  toSchema(mediaType?: string): OasSchema | OasRef<'schema'> | undefined;
@@ -322,7 +322,7 @@ class OasRequestBody extends OasBase_js_1.OasBase {
322
322
  * `mediaType`, the schema of the JSON media type that {@link toJsonContent}
323
323
  * picks: `application/json`, else the first JSON-like type such as
324
324
  * `application/json;v=1.0` or `application/vnd.api+json`, else `*\/*`
325
- * when its schema is an object, array, union or `$ref`.
325
+ * when its schema is an object, array, union, intersection or `$ref`.
326
326
  * `undefined` when there is no such media type or it has no schema.
327
327
  */
328
328
  toSchema(mediaType) {
@@ -230,7 +230,7 @@ export declare class OasResponse extends OasBase {
230
230
  * `mediaType`, the schema of the JSON media type that {@link toJsonContent}
231
231
  * picks: `application/json`, else the first JSON-like type such as
232
232
  * `application/json;v=1.0` or `application/vnd.api+json`, else `*\/*`
233
- * when its schema is an object, array, union or `$ref`.
233
+ * when its schema is an object, array, union, intersection or `$ref`.
234
234
  * `undefined` when there is no such media type or it has no schema.
235
235
  */
236
236
  toSchema(mediaType?: string): OasSchema | OasRef<'schema'> | undefined;
@@ -262,7 +262,7 @@ class OasResponse extends OasBase_js_1.OasBase {
262
262
  * `mediaType`, the schema of the JSON media type that {@link toJsonContent}
263
263
  * picks: `application/json`, else the first JSON-like type such as
264
264
  * `application/json;v=1.0` or `application/vnd.api+json`, else `*\/*`
265
- * when its schema is an object, array, union or `$ref`.
265
+ * when its schema is an object, array, union, intersection or `$ref`.
266
266
  * `undefined` when there is no such media type or it has no schema.
267
267
  */
268
268
  toSchema(mediaType) {
@@ -7,6 +7,7 @@ import type { OasObject } from '../object/Object.js';
7
7
  import type { OasString } from '../string/String.js';
8
8
  import type { OasUnknown } from '../unknown/Unknown.js';
9
9
  import type { OasUnion } from '../union/Union.js';
10
+ import type { OasIntersection } from '../intersection/Intersection.js';
10
11
  /**
11
12
  * Union type representing all possible OpenAPI Schema objects in the SKMTC system.
12
13
  *
@@ -28,7 +29,8 @@ import type { OasUnion } from '../union/Union.js';
28
29
  * - **{@link OasObject}**: Object schemas with properties, required fields, and constraints
29
30
  * - **{@link OasString}**: String schemas with length constraints, patterns, and formats
30
31
  * - **{@link OasUnknown}**: Schemas with unknown or mixed types
31
- * - **{@link OasUnion}**: Union schemas representing oneOf/anyOf/allOf constructs
32
+ * - **{@link OasUnion}**: Union schemas representing oneOf/anyOf constructs
33
+ * - **{@link OasIntersection}**: An allOf that keeps a reference
32
34
  *
33
35
  * @example Type checking and processing
34
36
  * ```typescript
@@ -108,7 +110,7 @@ import type { OasUnion } from '../union/Union.js';
108
110
  * }
109
111
  * ```
110
112
  */
111
- export type OasSchema = OasArray | OasBoolean | OasInteger | OasNumber | OasObject | OasString | OasUnknown | OasUnion;
113
+ export type OasSchema = OasArray | OasBoolean | OasInteger | OasNumber | OasObject | OasString | OasUnknown | OasUnion | OasIntersection;
112
114
  /**
113
115
  * Configuration options for JSON Schema conversion operations.
114
116
  *
@@ -1,5 +1,6 @@
1
1
  import type { OasSchema } from './Schema.js';
2
2
  import type { OasRef } from '../ref/Ref.js';
3
+ import type { OasIntersection } from '../intersection/Intersection.js';
3
4
  /**
4
5
  * The keywords next to a reference in a one-member `allOf`
5
6
  * (`{ allOf: [{ $ref }], minLength: 3, description: '…' }`), the use-site
@@ -68,7 +69,7 @@ export type SiblingKeywords = {
68
69
  };
69
70
  /** A keyword `clone` can apply. `_type` only has to agree. */
70
71
  export type SiblingKeyword = Exclude<keyof SiblingKeywords, '_type'>;
71
- /** The keywords each schema class's `clone` takes: the fields its type has. */
72
+ /** The keywords each schema class's `clone` takes: the fields its type has, and {@link OBJECT_KEYWORDS} for an intersection. */
72
73
  export declare const SIBLINGS_BY_TYPE: Record<OasSchema['type'], SiblingKeyword[]>;
73
74
  /**
74
75
  * The keywords of `over` merged over those of `base`, for a reference copied
@@ -98,6 +99,13 @@ export declare const changesValues: (copy: OasSchema, target: OasSchema, sibling
98
99
  * `OasRef.toSkippedSiblings`.
99
100
  */
100
101
  export declare const toSkippedSiblings: (target: OasSchema, siblings: SiblingKeywords, nullable: boolean | undefined) => string[];
102
+ /**
103
+ * The {@link OBJECT_KEYWORDS} next to a reference that say something the
104
+ * members of an intersection do not: a name none of them requires, a property
105
+ * none of them has the same. References in the members count by their target,
106
+ * without the keywords next to them.
107
+ */
108
+ export declare const toChangedObjectKeywords: (target: OasIntersection, siblings: SiblingKeywords) => SiblingKeyword[];
101
109
  /** The annotations of `target` with those of `siblings` applied: a sibling's value wins, and extension fields merge. */
102
110
  export declare const toSiblingAnnotations: (target: OasSchema, siblings: SiblingKeywords | undefined) => {
103
111
  title: string | undefined;
@@ -113,7 +121,7 @@ export declare const isIntegerValue: (value: unknown) => value is number;
113
121
  export declare const isBooleanValue: (value: unknown) => value is boolean;
114
122
  export declare const isArrayValue: (value: unknown) => value is unknown[];
115
123
  export declare const isObjectValue: (value: unknown) => value is Record<string, unknown>;
116
- /** {@link isStringValue} and the others, keyed by the type they check for. A union or unknown takes any value. */
124
+ /** {@link isStringValue} and the others, keyed by the type they check for. A union, intersection or unknown takes any value. */
117
125
  export declare const VALUE_CHECKS: Record<OasSchema['type'], (value: unknown) => boolean>;
118
126
  /** The formats a number and an integer take. A string takes any format. */
119
127
  export declare const isNumberFormat: (value: unknown) => value is "float" | "double";
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.toSiblingEnums = exports.toSiblingValue = exports.isIntegerFormat = exports.isNumberFormat = exports.VALUE_CHECKS = exports.isObjectValue = exports.isArrayValue = exports.isBooleanValue = exports.isIntegerValue = exports.isNumberValue = exports.isStringValue = exports.toSiblingAnnotations = exports.toSkippedSiblings = exports.changesValues = exports.changesTarget = exports.hasSiblingKeywords = exports.mergeSiblingKeywords = exports.SIBLINGS_BY_TYPE = void 0;
39
+ exports.toSiblingEnums = exports.toSiblingValue = exports.isIntegerFormat = exports.isNumberFormat = exports.VALUE_CHECKS = exports.isObjectValue = exports.isArrayValue = exports.isBooleanValue = exports.isIntegerValue = exports.isNumberValue = exports.isStringValue = exports.toSiblingAnnotations = exports.toChangedObjectKeywords = exports.toSkippedSiblings = exports.changesValues = exports.changesTarget = exports.hasSiblingKeywords = exports.mergeSiblingKeywords = exports.SIBLINGS_BY_TYPE = void 0;
40
40
  const v = __importStar(require("valibot"));
41
41
  const isEqual_js_1 = __importDefault(require("lodash-es/isEqual.js"));
42
42
  const DanglingRefError_js_1 = require("../ref/DanglingRefError.js");
@@ -61,7 +61,15 @@ const NUMERIC = [
61
61
  'exclusiveMinimum',
62
62
  'exclusiveMaximum'
63
63
  ];
64
- /** The keywords each schema class's `clone` takes: the fields its type has. */
64
+ /** The keywords of an object. An intersection's `clone` takes them as one more member. */
65
+ const OBJECT_KEYWORDS = [
66
+ 'properties',
67
+ 'required',
68
+ 'additionalProperties',
69
+ 'minProperties',
70
+ 'maxProperties'
71
+ ];
72
+ /** The keywords each schema class's `clone` takes: the fields its type has, and {@link OBJECT_KEYWORDS} for an intersection. */
65
73
  exports.SIBLINGS_BY_TYPE = {
66
74
  string: [...DESCRIBING, 'deprecated', 'default', 'enums', 'format', 'minLength', 'maxLength', 'pattern'],
67
75
  number: [...DESCRIBING, ...NUMERIC],
@@ -73,13 +81,10 @@ exports.SIBLINGS_BY_TYPE = {
73
81
  'deprecated',
74
82
  'default',
75
83
  'enums',
76
- 'properties',
77
- 'required',
78
- 'additionalProperties',
79
- 'minProperties',
80
- 'maxProperties'
84
+ ...OBJECT_KEYWORDS
81
85
  ],
82
86
  union: [...DESCRIBING, 'default'],
87
+ intersection: [...DESCRIBING, 'deprecated', 'default', ...OBJECT_KEYWORDS],
83
88
  unknown: DESCRIBING
84
89
  };
85
90
  /** Every keyword `clone` can apply, for any type. */
@@ -164,14 +169,21 @@ const BOOLEAN_KEYWORDS = [
164
169
  * a flag the target already has, and a value equal to the target's change
165
170
  * nothing.
166
171
  */
167
- const toChangedKeywords = (copy, target, siblings) => exports.SIBLINGS_BY_TYPE[target.type].filter(keyword => {
168
- if (siblings[keyword] === undefined) {
169
- return false;
170
- }
171
- const applied = Reflect.get(copy, keyword);
172
- const original = Reflect.get(target, keyword);
173
- return BOOLEAN_KEYWORDS.includes(keyword) ? (applied === true) !== (original === true) : !(0, isEqual_js_1.default)(applied, original);
174
- });
172
+ const toChangedKeywords = (copy, target, siblings) => {
173
+ // An intersection takes these as a member, so it has no field to compare.
174
+ const objectKeywords = target.type === 'intersection' ? (0, exports.toChangedObjectKeywords)(target, siblings) : [];
175
+ return exports.SIBLINGS_BY_TYPE[target.type].filter(keyword => {
176
+ if (siblings[keyword] === undefined) {
177
+ return false;
178
+ }
179
+ if (target.type === 'intersection' && OBJECT_KEYWORDS.includes(keyword)) {
180
+ return objectKeywords.includes(keyword);
181
+ }
182
+ const applied = Reflect.get(copy, keyword);
183
+ const original = Reflect.get(target, keyword);
184
+ return BOOLEAN_KEYWORDS.includes(keyword) ? (applied === true) !== (original === true) : !(0, isEqual_js_1.default)(applied, original);
185
+ });
186
+ };
175
187
  /** Whether the keywords next to a reference change `target` at that site. The engine behind `OasRef.resolve`. */
176
188
  const changesTarget = (copy, target, siblings) => toChangedKeywords(copy, target, siblings).length > 0;
177
189
  exports.changesTarget = changesTarget;
@@ -213,24 +225,74 @@ const toSkippedSiblings = (target, siblings, nullable) => {
213
225
  return false;
214
226
  }
215
227
  };
216
- const type = siblings._type !== undefined && !agrees(siblings._type, target, 0) ? ['type'] : [];
228
+ const type = siblings._type !== undefined && !agrees(siblings._type, target) ? ['type'] : [];
217
229
  const others = SIBLING_KEYWORDS.filter(keyword => isSkipped(keyword, siblings[keyword])).map(toDocumentKeyword);
218
230
  return [...type, ...others];
219
231
  };
220
232
  exports.toSkippedSiblings = toSkippedSiblings;
221
- /** How deep {@link agrees} follows union members, so a union that contains itself ends. */
222
- const MAX_DEPTH = 10;
233
+ /**
234
+ * The {@link OBJECT_KEYWORDS} next to a reference that say something the
235
+ * members of an intersection do not: a name none of them requires, a property
236
+ * none of them has the same. References in the members count by their target,
237
+ * without the keywords next to them.
238
+ */
239
+ const toChangedObjectKeywords = (target, siblings) => {
240
+ const objects = target.toTargetParts().filter(part => part.type === 'object');
241
+ const isSame = (keyword) => objects.some(object => (0, isEqual_js_1.default)(object[keyword], siblings[keyword]));
242
+ const isUnchanged = (keyword) => {
243
+ switch (keyword) {
244
+ case 'required':
245
+ return (siblings.required ?? []).every(name => objects.some(object => object.required?.includes(name)));
246
+ case 'properties':
247
+ return Object.keys(siblings.properties ?? {}).every(name => objects.some(object => (0, isEqual_js_1.default)(object.properties?.[name], siblings.properties?.[name])));
248
+ case 'additionalProperties':
249
+ case 'minProperties':
250
+ case 'maxProperties':
251
+ return isSame(keyword);
252
+ default:
253
+ return true;
254
+ }
255
+ };
256
+ return OBJECT_KEYWORDS.filter(keyword => siblings[keyword] !== undefined && !isUnchanged(keyword));
257
+ };
258
+ exports.toChangedObjectKeywords = toChangedObjectKeywords;
223
259
  /**
224
260
  * A `type` next to a reference agrees with the target's when it names it, or
225
261
  * `number` for an integer. Next to a union, it agrees when every member,
226
262
  * followed through references, has that type (`type: object` next to a
227
- * `oneOf` of objects): leaving it out then loses nothing.
263
+ * `oneOf` of objects): leaving it out then loses nothing. Next to an
264
+ * intersection, it agrees when one member has that type.
228
265
  */
229
- const agrees = (type, target, depth) => {
266
+ const agrees = (type, target) => {
267
+ const agreed = new Set();
268
+ // A schema that leads back to itself does not agree by that, so a pass reads
269
+ // it as `false` there. It can agree by another member, so a new pass reads
270
+ // the schemas that agreed, until a pass finds no more of them.
271
+ const runPass = () => {
272
+ const before = agreed.size;
273
+ return agreesInPass({ type, target, agreed, visited: new Set() }) || (agreed.size > before && runPass());
274
+ };
275
+ return runPass();
276
+ };
277
+ const agreesInPass = ({ type, target, agreed, visited }) => {
278
+ if (agreed.has(target)) {
279
+ return true;
280
+ }
281
+ if (visited.has(target)) {
282
+ return false;
283
+ }
284
+ visited.add(target);
285
+ const result = toAgreement(type, target, member => agreesInPass({ type, target: member, agreed, visited }));
286
+ if (result) {
287
+ agreed.add(target);
288
+ }
289
+ return result;
290
+ };
291
+ /** {@link agrees} for one schema, with `agreesInside` for its members. */
292
+ const toAgreement = (type, target, agreesInside) => {
230
293
  if (target.type === 'union') {
231
294
  try {
232
- return depth < MAX_DEPTH &&
233
- target.members.every(member => agrees(type, member.isRef() ? member.resolve() : member, depth + 1));
295
+ return target.members.every(member => agreesInside(member.isRef() ? member.resolve() : member));
234
296
  }
235
297
  catch (error) {
236
298
  // A member that is a dangling reference has no known type, so it does not agree.
@@ -240,6 +302,9 @@ const agrees = (type, target, depth) => {
240
302
  throw error;
241
303
  }
242
304
  }
305
+ if (target.type === 'intersection') {
306
+ return target.toParts().some(agreesInside);
307
+ }
243
308
  return type === target.type || (type === 'number' && target.type === 'integer');
244
309
  };
245
310
  /** Whether `format` fits a schema of `type`, as `clone` reads it: any string for a string. */
@@ -278,7 +343,7 @@ exports.isArrayValue = isArrayValue;
278
343
  const isObjectValue = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
279
344
  exports.isObjectValue = isObjectValue;
280
345
  const isAnyValue = (_value) => true;
281
- /** {@link isStringValue} and the others, keyed by the type they check for. A union or unknown takes any value. */
346
+ /** {@link isStringValue} and the others, keyed by the type they check for. A union, intersection or unknown takes any value. */
282
347
  exports.VALUE_CHECKS = {
283
348
  string: exports.isStringValue,
284
349
  number: exports.isNumberValue,
@@ -287,6 +352,7 @@ exports.VALUE_CHECKS = {
287
352
  array: exports.isArrayValue,
288
353
  object: exports.isObjectValue,
289
354
  union: isAnyValue,
355
+ intersection: isAnyValue,
290
356
  unknown: isAnyValue
291
357
  };
292
358
  /** The formats a number and an integer take. A string takes any format. */
@@ -18,6 +18,8 @@ import type { SchemaPath } from '../../types/SchemaPath.js';
18
18
  * element schema.
19
19
  * - **union** → the segment selects a member ({@link selectUnionMember}):
20
20
  * `'$Circle'` by refName, `'{type:circle}'` by discriminator, `'[0]'` by index.
21
+ * - **intersection** → the segment is read as for a member, references followed:
22
+ * the last member that has the whole path.
21
23
  *
22
24
  * A ref is resolved only when the path needs to descend *through* it. Anything
23
25
  * the path cannot navigate throws a descriptive, segment-named error: a missing
@@ -19,6 +19,8 @@ const refFns_js_1 = require("../../helpers/refFns.js");
19
19
  * element schema.
20
20
  * - **union** → the segment selects a member ({@link selectUnionMember}):
21
21
  * `'$Circle'` by refName, `'{type:circle}'` by discriminator, `'[0]'` by index.
22
+ * - **intersection** → the segment is read as for a member, references followed:
23
+ * the last member that has the whole path.
22
24
  *
23
25
  * A ref is resolved only when the path needs to descend *through* it. Anything
24
26
  * the path cannot navigate throws a descriptive, segment-named error: a missing
@@ -56,12 +58,50 @@ const traverseSchema = (value, path) => {
56
58
  case 'union': {
57
59
  return (0, exports.traverseSchema)(selectUnionMember(schema, segment), rest);
58
60
  }
61
+ case 'intersection': {
62
+ const members = schema.toParts().filter(part => takesSegment(part, segment));
63
+ if (members.length === 0) {
64
+ throw new Error(`Schema path segment "${segment}" does not match a member of the intersection`);
65
+ }
66
+ return traverseLast(members, path);
67
+ }
59
68
  default: {
60
69
  throw new Error(`Schema path segment "${segment}" cannot traverse a "${schema.type}" schema`);
61
70
  }
62
71
  }
63
72
  };
64
73
  exports.traverseSchema = traverseSchema;
74
+ /** The schema at `path` in the last of `members` that has the whole path. When none has, the last one's error. */
75
+ const traverseLast = (members, path) => {
76
+ const [last, ...others] = members.toReversed();
77
+ try {
78
+ return (0, exports.traverseSchema)(last, path);
79
+ }
80
+ catch (error) {
81
+ for (const member of others) {
82
+ try {
83
+ return (0, exports.traverseSchema)(member, path);
84
+ }
85
+ catch {
86
+ // The next member may have the path.
87
+ }
88
+ }
89
+ throw error;
90
+ }
91
+ };
92
+ /** Whether a member of an intersection is one that `segment` can descend into. */
93
+ const takesSegment = (schema, segment) => {
94
+ switch (schema.type) {
95
+ case 'object':
96
+ return schema.properties?.[segment] !== undefined;
97
+ case 'array':
98
+ return segment === 'items';
99
+ case 'union':
100
+ return /^(\$|\[\d+\]$|\{)/.test(segment);
101
+ default:
102
+ return false;
103
+ }
104
+ };
65
105
  /**
66
106
  * Pick one member of a union for an {@link SchemaPath} segment. The segment's
67
107
  * notation chooses the branching strategy: