@skmtc/core 0.34.0 → 0.35.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 (131) hide show
  1. package/esm/context/generateTypes.d.ts +2 -2
  2. package/esm/context/generateTypes.js +5 -0
  3. package/esm/parse/descriptiveFields.d.ts +36 -0
  4. package/esm/parse/descriptiveFields.js +56 -0
  5. package/esm/parse/isPlainObject.d.ts +2 -0
  6. package/esm/parse/isPlainObject.js +2 -0
  7. package/esm/parse/toComponentEntries.d.ts +29 -0
  8. package/esm/parse/toComponentEntries.js +31 -0
  9. package/esm/parse/toPresentInfo.d.ts +30 -0
  10. package/esm/parse/toPresentInfo.js +40 -0
  11. package/esm/parse/v3-0/_merge-all-of/merge-bounds.d.ts +7 -0
  12. package/esm/parse/v3-0/_merge-all-of/merge-bounds.js +46 -0
  13. package/esm/parse/v3-0/_merge-all-of/merge-integer-constraints.js +2 -20
  14. package/esm/parse/v3-0/_merge-all-of/merge-number-constraints.js +2 -20
  15. package/esm/parse/v3-0/components/toComponentsV3.js +2 -2
  16. package/esm/parse/v3-0/externalDocs/toExternalDocs.d.ts +3 -1
  17. package/esm/parse/v3-0/externalDocs/toExternalDocs.js +9 -8
  18. package/esm/parse/v3-0/header/toHeadersV3.d.ts +12 -0
  19. package/esm/parse/v3-0/header/toHeadersV3.js +20 -0
  20. package/esm/parse/v3-0/info/toInfoV3.d.ts +2 -1
  21. package/esm/parse/v3-0/info/toInfoV3.js +15 -4
  22. package/esm/parse/v3-0/requestBody/toRequestBodiesV3.js +9 -9
  23. package/esm/parse/v3-0/tag/toTagsV3.js +7 -2
  24. package/esm/parse/v3-1/_merge-all-of/decompose-union.d.ts +2 -0
  25. package/esm/parse/v3-1/_merge-all-of/decompose-union.js +3 -1
  26. package/esm/parse/v3-1/_merge-all-of/merge-bounds.d.ts +24 -0
  27. package/esm/parse/v3-1/_merge-all-of/merge-bounds.js +57 -0
  28. package/esm/parse/v3-1/_merge-all-of/merge-integer-constraints.js +2 -20
  29. package/esm/parse/v3-1/_merge-all-of/merge-number-constraints.js +2 -20
  30. package/esm/parse/v3-1/_merge-all-of/merge-union.d.ts +7 -0
  31. package/esm/parse/v3-1/_merge-all-of/merge-union.js +65 -7
  32. package/esm/parse/v3-1/_merge-all-of/merge.js +7 -3
  33. package/esm/parse/v3-1/array/toArray.js +4 -15
  34. package/esm/parse/v3-1/components/toComponentsV3.js +2 -2
  35. package/esm/parse/v3-1/externalDocs/toExternalDocs.d.ts +3 -1
  36. package/esm/parse/v3-1/externalDocs/toExternalDocs.js +9 -8
  37. package/esm/parse/v3-1/header/toHeadersV3.d.ts +12 -0
  38. package/esm/parse/v3-1/header/toHeadersV3.js +20 -0
  39. package/esm/parse/v3-1/info/toInfoV3.d.ts +2 -1
  40. package/esm/parse/v3-1/info/toInfoV3.js +15 -4
  41. package/esm/parse/v3-1/requestBody/toRequestBodiesV3.js +9 -9
  42. package/esm/parse/v3-1/schema/normalize-schema-shape.js +10 -8
  43. package/esm/parse/v3-1/schema/toSchemasV3.js +7 -1
  44. package/esm/parse/v3-1/tag/toTagsV3.js +7 -2
  45. package/esm/parse/v3-2/_merge-all-of/decompose-union.d.ts +2 -0
  46. package/esm/parse/v3-2/_merge-all-of/decompose-union.js +3 -1
  47. package/esm/parse/v3-2/_merge-all-of/merge-bounds.d.ts +24 -0
  48. package/esm/parse/v3-2/_merge-all-of/merge-bounds.js +57 -0
  49. package/esm/parse/v3-2/_merge-all-of/merge-integer-constraints.js +2 -20
  50. package/esm/parse/v3-2/_merge-all-of/merge-number-constraints.js +2 -20
  51. package/esm/parse/v3-2/_merge-all-of/merge-union.d.ts +7 -0
  52. package/esm/parse/v3-2/_merge-all-of/merge-union.js +65 -7
  53. package/esm/parse/v3-2/_merge-all-of/merge.js +7 -3
  54. package/esm/parse/v3-2/array/toArray.js +4 -15
  55. package/esm/parse/v3-2/components/toComponentsV3.js +2 -2
  56. package/esm/parse/v3-2/externalDocs/toExternalDocs.d.ts +3 -1
  57. package/esm/parse/v3-2/externalDocs/toExternalDocs.js +9 -8
  58. package/esm/parse/v3-2/header/toHeadersV3.d.ts +12 -0
  59. package/esm/parse/v3-2/header/toHeadersV3.js +20 -0
  60. package/esm/parse/v3-2/info/toInfoV3.d.ts +2 -1
  61. package/esm/parse/v3-2/info/toInfoV3.js +15 -4
  62. package/esm/parse/v3-2/requestBody/toRequestBodiesV3.js +9 -9
  63. package/esm/parse/v3-2/schema/normalize-schema-shape.js +10 -8
  64. package/esm/parse/v3-2/schema/toSchemasV3.js +7 -1
  65. package/esm/parse/v3-2/tag/toTagsV3.js +14 -7
  66. package/package.json +1 -1
  67. package/script/context/generateTypes.d.ts +2 -2
  68. package/script/context/generateTypes.js +5 -0
  69. package/script/parse/descriptiveFields.d.ts +36 -0
  70. package/script/parse/descriptiveFields.js +95 -0
  71. package/script/parse/isPlainObject.d.ts +2 -0
  72. package/script/parse/isPlainObject.js +6 -0
  73. package/script/parse/toComponentEntries.d.ts +29 -0
  74. package/script/parse/toComponentEntries.js +35 -0
  75. package/script/parse/toPresentInfo.d.ts +30 -0
  76. package/script/parse/toPresentInfo.js +45 -0
  77. package/script/parse/v3-0/_merge-all-of/merge-bounds.d.ts +7 -0
  78. package/script/parse/v3-0/_merge-all-of/merge-bounds.js +50 -0
  79. package/script/parse/v3-0/_merge-all-of/merge-integer-constraints.js +2 -20
  80. package/script/parse/v3-0/_merge-all-of/merge-number-constraints.js +2 -20
  81. package/script/parse/v3-0/components/toComponentsV3.js +1 -1
  82. package/script/parse/v3-0/externalDocs/toExternalDocs.d.ts +3 -1
  83. package/script/parse/v3-0/externalDocs/toExternalDocs.js +9 -41
  84. package/script/parse/v3-0/header/toHeadersV3.d.ts +12 -0
  85. package/script/parse/v3-0/header/toHeadersV3.js +22 -1
  86. package/script/parse/v3-0/info/toInfoV3.d.ts +2 -1
  87. package/script/parse/v3-0/info/toInfoV3.js +15 -4
  88. package/script/parse/v3-0/requestBody/toRequestBodiesV3.js +9 -9
  89. package/script/parse/v3-0/tag/toTagsV3.js +7 -2
  90. package/script/parse/v3-1/_merge-all-of/decompose-union.d.ts +2 -0
  91. package/script/parse/v3-1/_merge-all-of/decompose-union.js +5 -2
  92. package/script/parse/v3-1/_merge-all-of/merge-bounds.d.ts +24 -0
  93. package/script/parse/v3-1/_merge-all-of/merge-bounds.js +66 -0
  94. package/script/parse/v3-1/_merge-all-of/merge-integer-constraints.js +2 -20
  95. package/script/parse/v3-1/_merge-all-of/merge-number-constraints.js +2 -20
  96. package/script/parse/v3-1/_merge-all-of/merge-union.d.ts +7 -0
  97. package/script/parse/v3-1/_merge-all-of/merge-union.js +66 -7
  98. package/script/parse/v3-1/_merge-all-of/merge.js +6 -2
  99. package/script/parse/v3-1/array/toArray.js +4 -15
  100. package/script/parse/v3-1/components/toComponentsV3.js +1 -1
  101. package/script/parse/v3-1/externalDocs/toExternalDocs.d.ts +3 -1
  102. package/script/parse/v3-1/externalDocs/toExternalDocs.js +9 -41
  103. package/script/parse/v3-1/header/toHeadersV3.d.ts +12 -0
  104. package/script/parse/v3-1/header/toHeadersV3.js +22 -1
  105. package/script/parse/v3-1/info/toInfoV3.d.ts +2 -1
  106. package/script/parse/v3-1/info/toInfoV3.js +15 -4
  107. package/script/parse/v3-1/requestBody/toRequestBodiesV3.js +9 -9
  108. package/script/parse/v3-1/schema/normalize-schema-shape.js +10 -8
  109. package/script/parse/v3-1/schema/toSchemasV3.js +6 -0
  110. package/script/parse/v3-1/tag/toTagsV3.js +7 -2
  111. package/script/parse/v3-2/_merge-all-of/decompose-union.d.ts +2 -0
  112. package/script/parse/v3-2/_merge-all-of/decompose-union.js +5 -2
  113. package/script/parse/v3-2/_merge-all-of/merge-bounds.d.ts +24 -0
  114. package/script/parse/v3-2/_merge-all-of/merge-bounds.js +66 -0
  115. package/script/parse/v3-2/_merge-all-of/merge-integer-constraints.js +2 -20
  116. package/script/parse/v3-2/_merge-all-of/merge-number-constraints.js +2 -20
  117. package/script/parse/v3-2/_merge-all-of/merge-union.d.ts +7 -0
  118. package/script/parse/v3-2/_merge-all-of/merge-union.js +66 -7
  119. package/script/parse/v3-2/_merge-all-of/merge.js +6 -2
  120. package/script/parse/v3-2/array/toArray.js +4 -15
  121. package/script/parse/v3-2/components/toComponentsV3.js +1 -1
  122. package/script/parse/v3-2/externalDocs/toExternalDocs.d.ts +3 -1
  123. package/script/parse/v3-2/externalDocs/toExternalDocs.js +9 -41
  124. package/script/parse/v3-2/header/toHeadersV3.d.ts +12 -0
  125. package/script/parse/v3-2/header/toHeadersV3.js +22 -1
  126. package/script/parse/v3-2/info/toInfoV3.d.ts +2 -1
  127. package/script/parse/v3-2/info/toInfoV3.js +15 -4
  128. package/script/parse/v3-2/requestBody/toRequestBodiesV3.js +9 -9
  129. package/script/parse/v3-2/schema/normalize-schema-shape.js +10 -8
  130. package/script/parse/v3-2/schema/toSchemasV3.js +6 -0
  131. package/script/parse/v3-2/tag/toTagsV3.js +14 -7
@@ -0,0 +1,24 @@
1
+ import type { SchemaObject } from './types.js';
2
+ export type Bound = {
3
+ value: number;
4
+ exclusive: boolean;
5
+ };
6
+ /** The tighter of two lower bounds. */
7
+ export declare const toTighterMinimum: (first: Bound | undefined, second: Bound | undefined) => Bound | undefined;
8
+ /** The tighter of two upper bounds. */
9
+ export declare const toTighterMaximum: (first: Bound | undefined, second: Bound | undefined) => Bound | undefined;
10
+ /** The lower bound of `schema`; exclusive only when `exclusiveMinimum` is `true`. */
11
+ export declare const toMinimum: (schema: SchemaObject) => Bound | undefined;
12
+ /** The upper bound of `schema`; exclusive only when `exclusiveMaximum` is `true`. */
13
+ export declare const toMaximum: (schema: SchemaObject) => Bound | undefined;
14
+ /**
15
+ * Write the merged `minimum`/`exclusiveMinimum` and `maximum`/`exclusiveMaximum`
16
+ * of `first` and `second` onto `result`. Members are in the normalized form,
17
+ * where an exclusive bound is `minimum` plus `exclusiveMinimum: true`.
18
+ */
19
+ export declare const mergeBounds: (result: SchemaObject, first: SchemaObject, second: SchemaObject) => void;
20
+ /**
21
+ * Write `minimum` and `maximum` onto `result` in the normalized form. An
22
+ * undefined bound leaves `result` unchanged.
23
+ */
24
+ export declare const writeBounds: (result: SchemaObject, minimum: Bound | undefined, maximum: Bound | undefined) => void;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Pick the tighter of two bounds. The tighter bound keeps its own
3
+ * exclusivity; when both bounds are equal, exclusive wins.
4
+ */
5
+ const toTighterBound = (first, second, isTighter) => {
6
+ if (first === undefined || second === undefined) {
7
+ return first ?? second;
8
+ }
9
+ if (first.value === second.value) {
10
+ return { value: first.value, exclusive: first.exclusive || second.exclusive };
11
+ }
12
+ return isTighter(first.value, second.value) ? first : second;
13
+ };
14
+ /** The tighter of two lower bounds. */
15
+ export const toTighterMinimum = (first, second) => toTighterBound(first, second, (a, b) => a > b);
16
+ /** The tighter of two upper bounds. */
17
+ export const toTighterMaximum = (first, second) => toTighterBound(first, second, (a, b) => a < b);
18
+ /** The lower bound of `schema`; exclusive only when `exclusiveMinimum` is `true`. */
19
+ export const toMinimum = (schema) => schema.minimum === undefined
20
+ ? undefined
21
+ : { value: schema.minimum, exclusive: schema.exclusiveMinimum === true };
22
+ /** The upper bound of `schema`; exclusive only when `exclusiveMaximum` is `true`. */
23
+ export const toMaximum = (schema) => schema.maximum === undefined
24
+ ? undefined
25
+ : { value: schema.maximum, exclusive: schema.exclusiveMaximum === true };
26
+ /**
27
+ * Write the merged `minimum`/`exclusiveMinimum` and `maximum`/`exclusiveMaximum`
28
+ * of `first` and `second` onto `result`. Members are in the normalized form,
29
+ * where an exclusive bound is `minimum` plus `exclusiveMinimum: true`.
30
+ */
31
+ export const mergeBounds = (result, first, second) => {
32
+ writeBounds(result, toTighterMinimum(toMinimum(first), toMinimum(second)), toTighterMaximum(toMaximum(first), toMaximum(second)));
33
+ };
34
+ /**
35
+ * Write `minimum` and `maximum` onto `result` in the normalized form. An
36
+ * undefined bound leaves `result` unchanged.
37
+ */
38
+ export const writeBounds = (result, minimum, maximum) => {
39
+ if (minimum !== undefined) {
40
+ result.minimum = minimum.value;
41
+ if (minimum.exclusive) {
42
+ result.exclusiveMinimum = true;
43
+ }
44
+ else {
45
+ delete result.exclusiveMinimum;
46
+ }
47
+ }
48
+ if (maximum !== undefined) {
49
+ result.maximum = maximum.value;
50
+ if (maximum.exclusive) {
51
+ result.exclusiveMaximum = true;
52
+ }
53
+ else {
54
+ delete result.exclusiveMaximum;
55
+ }
56
+ }
57
+ };
@@ -1,32 +1,14 @@
1
1
  import { genericMerge } from './generic-merge.js';
2
2
  import * as v from 'valibot';
3
3
  import { checkTypeConflicts } from './check-type-conflicts.js';
4
+ import { mergeBounds } from './merge-bounds.js';
4
5
  export function mergeIntegerConstraints(first, second, getRef, normalize) {
5
6
  checkTypeConflicts(first, second);
6
7
  // First merge enum values if present
7
8
  const result = genericMerge(first, second, getRef, normalize, v.number());
8
9
  // Then merge other constraints
9
10
  result.type = 'integer';
10
- // Merge minimum and exclusiveMinimum
11
- if (first.minimum !== undefined || second.minimum !== undefined) {
12
- const minA = first.minimum ?? -Infinity;
13
- const minB = second.minimum ?? -Infinity;
14
- result.minimum = Math.max(minA, minB);
15
- // If either minimum is exclusive, the result is exclusive
16
- if (first.exclusiveMinimum || second.exclusiveMinimum) {
17
- result.exclusiveMinimum = true;
18
- }
19
- }
20
- // Merge maximum and exclusiveMaximum
21
- if (first.maximum !== undefined || second.maximum !== undefined) {
22
- const maxA = first.maximum ?? Infinity;
23
- const maxB = second.maximum ?? Infinity;
24
- result.maximum = Math.min(maxA, maxB);
25
- // If either maximum is exclusive, the result is exclusive
26
- if (first.exclusiveMaximum || second.exclusiveMaximum) {
27
- result.exclusiveMaximum = true;
28
- }
29
- }
11
+ mergeBounds(result, first, second);
30
12
  // Merge multipleOf
31
13
  if (first.multipleOf !== undefined && second.multipleOf !== undefined) {
32
14
  result.multipleOf = first.multipleOf * second.multipleOf;
@@ -1,32 +1,14 @@
1
1
  import { genericMerge } from './generic-merge.js';
2
2
  import * as v from 'valibot';
3
3
  import { checkTypeConflicts } from './check-type-conflicts.js';
4
+ import { mergeBounds } from './merge-bounds.js';
4
5
  export function mergeNumberConstraints(first, second, getRef, normalize) {
5
6
  checkTypeConflicts(first, second);
6
7
  // First merge enum values if present
7
8
  const result = genericMerge(first, second, getRef, normalize, v.number());
8
9
  // Then merge other constraints
9
10
  result.type = 'number';
10
- // Merge minimum and exclusiveMinimum
11
- if (first.minimum !== undefined || second.minimum !== undefined) {
12
- const minA = first.minimum ?? -Infinity;
13
- const minB = second.minimum ?? -Infinity;
14
- result.minimum = Math.max(minA, minB);
15
- // If either minimum is exclusive, the result is exclusive
16
- if (first.exclusiveMinimum || second.exclusiveMinimum) {
17
- result.exclusiveMinimum = true;
18
- }
19
- }
20
- // Merge maximum and exclusiveMaximum
21
- if (first.maximum !== undefined || second.maximum !== undefined) {
22
- const maxA = first.maximum ?? Infinity;
23
- const maxB = second.maximum ?? Infinity;
24
- result.maximum = Math.min(maxA, maxB);
25
- // If either maximum is exclusive, the result is exclusive
26
- if (first.exclusiveMaximum || second.exclusiveMaximum) {
27
- result.exclusiveMaximum = true;
28
- }
29
- }
11
+ mergeBounds(result, first, second);
30
12
  // Merge multipleOf
31
13
  if (first.multipleOf !== undefined && second.multipleOf !== undefined) {
32
14
  result.multipleOf = first.multipleOf * second.multipleOf;
@@ -14,4 +14,11 @@ type MergeCrossProductArgs = {
14
14
  groupType: 'oneOf' | 'anyOf';
15
15
  };
16
16
  export declare const mergeCrossProduct: ({ first, second, getRef, normalize, groupType }: MergeCrossProductArgs) => SchemaObject;
17
+ /**
18
+ * A `oneOf`/`anyOf` whose every member is null-only (for example
19
+ * `oneOf: [{ type: 'null' }]`) reads as nullable with no other type, not
20
+ * as an empty union: the group is replaced by `nullable: true`. Any other
21
+ * schema is returned unchanged.
22
+ */
23
+ export declare const collapseNullOnlyUnion: (schema: SchemaObject, normalize: NormalizeFn) => SchemaObject;
17
24
  export {};
@@ -1,4 +1,4 @@
1
- import { decomposeUnion } from './decompose-union.js';
1
+ import { decomposeUnion, isUnionLevelKey } from './decompose-union.js';
2
2
  import { mergeSchemasOrRefs } from './merge.js';
3
3
  import { crossProduct } from './cross-product.js';
4
4
  import { isRef } from '../../../helpers/refFns.js';
@@ -37,15 +37,16 @@ export const mergeUnion = ({ schema, getRef, normalize, groupType }) => {
37
37
  export const mergeCrossProduct = ({ first, second, getRef, normalize, groupType }) => {
38
38
  const firstMembers = Array.isArray(first[groupType]) ? first[groupType] : [first];
39
39
  const secondMembers = Array.isArray(second[groupType]) ? second[groupType] : [second];
40
- // A member that normalizes to exactly `{ nullable: true }` is a null-only
41
- // member: it makes the union nullable instead of joining the cross product.
40
+ // A member that normalizes to `{ nullable: true }`, with at most union-level
41
+ // keys beside it, is a null-only member: it makes the union nullable
42
+ // instead of joining the cross product.
42
43
  const firstGroup = firstMembers.filter(member => !isNullableOnly(member, normalize));
43
44
  const secondGroup = secondMembers.filter(member => !isNullableOnly(member, normalize));
44
45
  const nullable = isNullableGroup(first, groupType) ||
45
46
  isNullableGroup(second, groupType) ||
46
47
  firstGroup.length !== firstMembers.length ||
47
48
  secondGroup.length !== secondMembers.length;
48
- const mergedGroup = crossProduct(firstGroup, secondGroup)
49
+ const mergedGroup = crossProduct(withGroupSiblings({ schema: first, members: firstGroup, getRef, normalize, groupType }), withGroupSiblings({ schema: second, members: secondGroup, getRef, normalize, groupType }))
49
50
  .map(([firstItem, secondItem]) => {
50
51
  try {
51
52
  const result = mergeSchemasOrRefs(firstItem, secondItem, getRef, normalize);
@@ -56,6 +57,8 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, groupType
56
57
  }
57
58
  })
58
59
  .filter(item => item !== undefined);
60
+ // A nested group is spread into this one below; its `nullable` carries over.
61
+ const nestedNullable = mergedGroup.some(item => !isRef(item) && Array.isArray(item[groupType]) && item.nullable === true);
59
62
  return {
60
63
  [groupType]: mergedGroup.flatMap(item => {
61
64
  if (!isRef(item) && Array.isArray(item[groupType])) {
@@ -63,15 +66,70 @@ export const mergeCrossProduct = ({ first, second, getRef, normalize, groupType
63
66
  }
64
67
  return [item];
65
68
  }),
66
- ...(nullable ? { nullable: true } : {})
69
+ ...(nullable || nestedNullable ? { nullable: true } : {})
67
70
  };
68
71
  };
72
+ /**
73
+ * Merge the sibling keys of a `oneOf`/`anyOf` wrapper into each of its
74
+ * `members`. A 3.1 `type: ['string', 'number']` with `minLength` normalizes
75
+ * to a `oneOf` with `minLength` beside it. Union-level keys (`nullable`,
76
+ * `description`, …) are not merged in, and a `$ref` member is returned
77
+ * unchanged. A member that conflicts with the
78
+ * siblings is dropped, as in the cross product. A schema with no group is
79
+ * its own only member and is returned unchanged.
80
+ */
81
+ const withGroupSiblings = ({ schema, members, getRef, normalize, groupType }) => {
82
+ if (!Array.isArray(schema[groupType])) {
83
+ return members;
84
+ }
85
+ const siblings = Object.entries(schema).filter(([key]) => key !== groupType && !isUnionLevelKey(key));
86
+ if (siblings.length === 0) {
87
+ return members;
88
+ }
89
+ const siblingSchema = Object.fromEntries(siblings);
90
+ return members
91
+ .map(member => {
92
+ // Merging into a `$ref` member would resolve it and lose its name.
93
+ if (isRef(member)) {
94
+ return member;
95
+ }
96
+ try {
97
+ return mergeSchemasOrRefs(siblingSchema, member, getRef, normalize);
98
+ }
99
+ catch (_error) {
100
+ return undefined;
101
+ }
102
+ })
103
+ .filter(member => member !== undefined);
104
+ };
69
105
  const isNullableOnly = (member, normalize) => {
70
106
  if (isRef(member)) {
71
107
  return false;
72
108
  }
73
- const normalized = normalize(member);
74
- return normalized.nullable === true && Object.keys(normalized).length === 1;
109
+ const normalized = collapseNullOnlyUnion(normalize(member), normalize);
110
+ // Union-level keys (`description`, `title`, …) do not change what a
111
+ // null-only member allows.
112
+ return normalized.nullable === true && Object.keys(normalized).every(isUnionLevelKey);
113
+ };
114
+ /**
115
+ * A `oneOf`/`anyOf` whose every member is null-only (for example
116
+ * `oneOf: [{ type: 'null' }]`) reads as nullable with no other type, not
117
+ * as an empty union: the group is replaced by `nullable: true`. Any other
118
+ * schema is returned unchanged.
119
+ */
120
+ export const collapseNullOnlyUnion = (schema, normalize) => {
121
+ const isNullOnlyGroup = (members) => Array.isArray(members) &&
122
+ members.length > 0 &&
123
+ members.every(member => isNullableOnly(member, normalize));
124
+ if (isNullOnlyGroup(schema.oneOf)) {
125
+ const { oneOf: _oneOf, ...rest } = schema;
126
+ return { ...rest, nullable: true };
127
+ }
128
+ if (isNullOnlyGroup(schema.anyOf)) {
129
+ const { anyOf: _anyOf, ...rest } = schema;
130
+ return { ...rest, nullable: true };
131
+ }
132
+ return schema;
75
133
  };
76
134
  /** Is this a `oneOf`/`anyOf` group wrapper that already carries `nullable`? */
77
135
  const isNullableGroup = (schema, groupType) => Array.isArray(schema[groupType]) && schema.nullable === true;
@@ -14,12 +14,16 @@ import { mergeBooleanConstraints } from './merge-boolean-constraints.js';
14
14
  import { genericMerge } from './generic-merge.js';
15
15
  import { checkAtLeastOneTypeMatch } from './check-at-least-one-type-match.js';
16
16
  import { mergeIntersection } from './merge-intersection.js';
17
- import { mergeCrossProduct } from './merge-union.js';
17
+ import { collapseNullOnlyUnion, mergeCrossProduct } from './merge-union.js';
18
18
  import { isEmpty } from '../../../helpers/isEmpty.js';
19
19
  import { isNullOnly, mergeNullOnly } from './nullable-merge.js';
20
20
  export const mergeSchemasOrRefs = (inputFirst, inputSecond, getRef, normalize) => {
21
- const first = isRef(inputFirst) ? inputFirst : normalize(inputFirst);
22
- const second = isRef(inputSecond) ? inputSecond : normalize(inputSecond);
21
+ const first = isRef(inputFirst)
22
+ ? inputFirst
23
+ : collapseNullOnlyUnion(normalize(inputFirst), normalize);
24
+ const second = isRef(inputSecond)
25
+ ? inputSecond
26
+ : collapseNullOnlyUnion(normalize(inputSecond), normalize);
23
27
  if (containsRef(first, second)) {
24
28
  return mergeWithRef(first, second, getRef, normalize);
25
29
  }
@@ -133,21 +133,10 @@ export const toParsedArray = ({ context, stackTrail, nullable, example, enums, d
133
133
  nullable,
134
134
  defaultValue,
135
135
  items: stackTrail.trace('items', st => {
136
- // An array schema with no `items` is invalid OAS but must not
137
- // kill the run (fail open): log the issue and treat the items
138
- // as unknown — `{}` falls through toSchemaV3 to OasUnknown.
139
- if (items === undefined) {
140
- context.logIssue({
141
- key: 'items',
142
- level: 'warning',
143
- message: 'Array schema has no "items" — treating as an array of unknown values',
144
- parent: value,
145
- stackTrail: st,
146
- type: 'INVALID_SCHEMA'
147
- });
148
- return toSchemaV3({ schema: {}, stackTrail: st, context });
149
- }
150
- return toSchemaV3({ schema: items, stackTrail: st, context });
136
+ // `items` is optional in JSON Schema 2020-12: an array schema
137
+ // without it has unknown items — `{}` falls through toSchemaV3
138
+ // to OasUnknown.
139
+ return toSchemaV3({ schema: items ?? {}, stackTrail: st, context });
151
140
  }),
152
141
  extensionFields,
153
142
  example,
@@ -1,5 +1,5 @@
1
1
  import { toOptionalResponsesV3 } from '../response/toResponseV3.js';
2
- import { toHeadersV3 } from '../header/toHeadersV3.js';
2
+ import { toComponentHeadersV3 } from '../header/toHeadersV3.js';
3
3
  import { toLinksV3 } from '../link/toLinksV3.js';
4
4
  import { toOptionalSchemasV3 } from '../schema/toSchemasV3.js';
5
5
  import { toOptionalParametersV3 } from '../parameter/toParameterV3.js';
@@ -34,7 +34,7 @@ export const toComponentsV3 = ({ components, stackTrail, context }) => {
34
34
  context
35
35
  }),
36
36
  requestBodies: stackTrail.trace('requestBodies', st => toRequestBodiesV3({ requestBodies, stackTrail: st, context })),
37
- headers: stackTrail.trace('headers', st => toHeadersV3({ headers, stackTrail: st, context })),
37
+ headers: stackTrail.trace('headers', st => toComponentHeadersV3({ headers, stackTrail: st, context })),
38
38
  securitySchemes: stackTrail.trace('securitySchemes', st => toSecuritySchemesV3({ securitySchemes, stackTrail: st, context })),
39
39
  links: stackTrail.trace('links', st => toLinksV3({ links, stackTrail: st, context })),
40
40
  extensionFields
@@ -4,7 +4,9 @@ import type { ParseContextType } from '../../../context/parseTypes.js';
4
4
  import type { StackTrail } from '../../../context/StackTrail.js';
5
5
  export type ToExternalDocsArgs = {
6
6
  externalDocs: OpenAPIV3.ExternalDocumentationObject | undefined;
7
+ /** Trail that ends in the `externalDocs` key. */
7
8
  stackTrail: StackTrail;
8
9
  context: ParseContextType;
9
10
  };
10
- export declare const toExternalDocs: ({ externalDocs, stackTrail }: ToExternalDocsArgs) => OasExternalDocs | undefined;
11
+ /** An invalid `externalDocs` is left out with a warning; its parent is kept. */
12
+ export declare const toExternalDocs: ({ externalDocs, stackTrail, context }: ToExternalDocsArgs) => OasExternalDocs | undefined;
@@ -1,15 +1,16 @@
1
1
  import { OasExternalDocs } from '../../../oas/externalDocs/ExternalDocs.js';
2
- import * as v from 'valibot';
3
- import { oasExternalDocsData } from '../../../oas/externalDocs/externalDocsTypes.js';
4
- export const toExternalDocs = ({ externalDocs, stackTrail }) => {
5
- if (!externalDocs) {
2
+ import { toValidExternalDocs } from '../../descriptiveFields.js';
3
+ /** An invalid `externalDocs` is left out with a warning; its parent is kept. */
4
+ export const toExternalDocs = ({ externalDocs, stackTrail, context }) => {
5
+ if (externalDocs === undefined) {
6
6
  return undefined;
7
7
  }
8
- if (!v.is(oasExternalDocsData, externalDocs)) {
9
- v.parse(oasExternalDocsData, externalDocs);
8
+ const valid = toValidExternalDocs({ externalDocs, stackTrail, context });
9
+ if (!valid) {
10
+ return undefined;
10
11
  }
11
12
  return new OasExternalDocs({
12
- url: externalDocs.url,
13
- description: externalDocs.description
13
+ url: valid.url,
14
+ description: valid.description
14
15
  });
15
16
  };
@@ -9,6 +9,18 @@ export type ToHeadersV3Args = {
9
9
  context: ParseContextType;
10
10
  };
11
11
  export declare const toHeadersV3: ({ headers, stackTrail, context }: ToHeadersV3Args) => Record<string, OasHeader | OasRef<"header">> | undefined;
12
+ export type ToComponentHeadersV3Args = {
13
+ headers: Record<string, OpenAPIV3.ReferenceObject | OpenAPIV3.HeaderObject> | undefined;
14
+ stackTrail: StackTrail;
15
+ context: ParseContextType;
16
+ };
17
+ /**
18
+ * Parse `components.headers`. Each entry parses inside its own boundary: a
19
+ * broken one is dropped with an INVALID_HEADER error, and the others are
20
+ * kept. Headers inside a response stay with {@link toHeadersV3}, where a
21
+ * broken header drops the response.
22
+ */
23
+ export declare const toComponentHeadersV3: ({ headers, stackTrail, context }: ToComponentHeadersV3Args) => Record<string, OasHeader | OasRef<"header">> | undefined;
12
24
  export type ToHeaderV3Args = {
13
25
  header: OpenAPIV3.ReferenceObject | OpenAPIV3.HeaderObject;
14
26
  /** The header's name, used to key a singular `example` */
@@ -5,6 +5,7 @@ import { isRef } from '../../../helpers/refFns.js';
5
5
  import { toOptionalMediaTypeItemsV3 } from '../mediaType/toMediaTypeItemV3.js';
6
6
  import { OasHeader } from '../../../oas/header/Header.js';
7
7
  import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
8
+ import { toComponentEntries } from '../../toComponentEntries.js';
8
9
  export const toHeadersV3 = ({ headers, stackTrail, context }) => {
9
10
  if (!headers) {
10
11
  return undefined;
@@ -16,6 +17,25 @@ export const toHeadersV3 = ({ headers, stackTrail, context }) => {
16
17
  }
17
18
  return output;
18
19
  };
20
+ /**
21
+ * Parse `components.headers`. Each entry parses inside its own boundary: a
22
+ * broken one is dropped with an INVALID_HEADER error, and the others are
23
+ * kept. Headers inside a response stay with {@link toHeadersV3}, where a
24
+ * broken header drops the response.
25
+ */
26
+ export const toComponentHeadersV3 = ({ headers, stackTrail, context }) => {
27
+ if (!headers) {
28
+ return undefined;
29
+ }
30
+ return toComponentEntries({
31
+ entries: headers,
32
+ stackTrail,
33
+ context,
34
+ type: 'INVALID_HEADER',
35
+ label: 'Header',
36
+ parse: ({ key, entry, stackTrail: st }) => toHeaderV3({ header: entry, name: key, stackTrail: st, context })
37
+ });
38
+ };
19
39
  const toHeaderV3 = ({ header, name, stackTrail, context }) => {
20
40
  if (isRef(header)) {
21
41
  return toRefV31({ ref: header, refType: 'header', stackTrail, context });
@@ -3,7 +3,8 @@ import type { ParseContextType } from '../../../context/parseTypes.js';
3
3
  import { OasInfo } from '../../../oas/info/Info.js';
4
4
  import type { StackTrail } from '../../../context/StackTrail.js';
5
5
  export type ToInfoV3Args = {
6
- info: OpenAPIV3.InfoObject;
6
+ /** Required by the spec; a document without it still parses (see {@link toPresentInfo}). */
7
+ info: OpenAPIV3.InfoObject | null | undefined;
7
8
  stackTrail: StackTrail;
8
9
  context: ParseContextType;
9
10
  };
@@ -2,22 +2,33 @@ import { OasInfo } from '../../../oas/info/Info.js';
2
2
  import { toContactV3 } from '../contact/toContactV3.js';
3
3
  import { toLicenseV3 } from '../license/toLicenseV3.js';
4
4
  import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
5
+ import { toInfoString, toPresentInfo } from '../../toPresentInfo.js';
5
6
  export const toInfoV3 = ({ info, stackTrail, context }) => {
6
- const { title, description, termsOfService, contact, license, version, ...skipped } = info;
7
+ const presentInfo = toPresentInfo({ info, stackTrail, context });
8
+ if (presentInfo === undefined) {
9
+ return new OasInfo({ title: '', version: '' });
10
+ }
11
+ const { title, description, termsOfService, contact, license, version, ...skipped } = presentInfo;
7
12
  const extensionFields = toSpecificationExtensionsV3({
8
13
  skipped,
9
- parent: info,
14
+ parent: presentInfo,
10
15
  context,
11
16
  stackTrail,
12
17
  parentType: 'info'
13
18
  });
14
19
  return new OasInfo({
15
- title,
20
+ title: toInfoString({ value: title, key: 'title', parent: presentInfo, stackTrail, context }),
16
21
  description,
17
22
  termsOfService,
18
23
  contact: contact ? toContactV3(contact, stackTrail, context) : undefined,
19
24
  license: license ? toLicenseV3(license, stackTrail, context) : undefined,
20
- version,
25
+ version: toInfoString({
26
+ value: version,
27
+ key: 'version',
28
+ parent: presentInfo,
29
+ stackTrail,
30
+ context
31
+ }),
21
32
  extensionFields
22
33
  });
23
34
  };
@@ -4,6 +4,7 @@ import { toMediaTypeItemsV3 } from '../mediaType/toMediaTypeItemV3.js';
4
4
  import { OasRequestBody } from '../../../oas/requestBody/RequestBody.js';
5
5
  import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
6
6
  import { logBodyMediaTypes } from '../../logBodyMediaTypes.js';
7
+ import { toComponentEntries } from '../../toComponentEntries.js';
7
8
  export const toRequestBodyV3 = ({ requestBody, stackTrail, context }) => {
8
9
  if (!requestBody) {
9
10
  return undefined;
@@ -35,13 +36,12 @@ export const toRequestBodiesV3 = ({ requestBodies, stackTrail, context }) => {
35
36
  if (!requestBodies) {
36
37
  return undefined;
37
38
  }
38
- const entries = Object.entries(requestBodies)
39
- .map(([key, value]) => {
40
- return [
41
- key,
42
- stackTrail.trace(key, st => toRequestBodyV3({ requestBody: value, stackTrail: st, context }))
43
- ];
44
- })
45
- .filter(([, value]) => Boolean(value));
46
- return Object.fromEntries(entries);
39
+ return toComponentEntries({
40
+ entries: requestBodies,
41
+ stackTrail,
42
+ context,
43
+ type: 'INVALID_REQUEST_BODY',
44
+ label: 'Request body',
45
+ parse: ({ entry, stackTrail: st }) => toRequestBodyV3({ requestBody: entry, stackTrail: st, context })
46
+ });
47
47
  };
@@ -1,3 +1,4 @@
1
+ import { toMaximum, toMinimum, toTighterMaximum, toTighterMinimum, writeBounds } from '../_merge-all-of/merge-bounds.js';
1
2
  /**
2
3
  * OpenAPI 3.1 lets a schema's `type` be an ARRAY (e.g. `['string', 'null']`),
3
4
  * which the shared IR — modelling a single `type` plus a `nullable` flag —
@@ -94,15 +95,16 @@ const normalizeExclusiveBounds = (schema) => {
94
95
  if (typeof exclusiveMinimum !== 'number' && typeof exclusiveMaximum !== 'number') {
95
96
  return schema;
96
97
  }
98
+ // A schema may carry both `minimum` and a numeric `exclusiveMinimum`:
99
+ // keep the tighter of the two (the same for maximum).
100
+ const minimum = typeof exclusiveMinimum === 'number'
101
+ ? toTighterMinimum(toMinimum(schema), { value: exclusiveMinimum, exclusive: true })
102
+ : undefined;
103
+ const maximum = typeof exclusiveMaximum === 'number'
104
+ ? toTighterMaximum(toMaximum(schema), { value: exclusiveMaximum, exclusive: true })
105
+ : undefined;
97
106
  const next = { ...schema };
98
- if (typeof exclusiveMinimum === 'number') {
99
- next.minimum = exclusiveMinimum;
100
- next.exclusiveMinimum = true;
101
- }
102
- if (typeof exclusiveMaximum === 'number') {
103
- next.maximum = exclusiveMaximum;
104
- next.exclusiveMaximum = true;
105
- }
107
+ writeBounds(next, minimum, maximum);
106
108
  return next;
107
109
  };
108
110
  /**
@@ -10,7 +10,7 @@ import { toUnknown } from '../unknown/toUnknown.js';
10
10
  import { toUnion } from '../union/toUnion.js';
11
11
  import { toGetRef } from '../../../helpers/refFns.js';
12
12
  import { mergeIntersection } from '../_merge-all-of/merge-intersection.js';
13
- import { mergeUnion } from '../_merge-all-of/merge-union.js';
13
+ import { collapseNullOnlyUnion, mergeUnion } from '../_merge-all-of/merge-union.js';
14
14
  import { tryParseAt } from '../../../context/tryParseAt.js';
15
15
  import { isNullOnlyType, normalizeMergeMember, normalizeSchemaShape } from './normalize-schema-shape.js';
16
16
  export const toSchemasV3 = ({ schemas, stackTrail, context }) => {
@@ -166,6 +166,12 @@ export const toSchemaV3 = ({ schema, stackTrail, context }) => {
166
166
  return toSchemaV3({ schema: merged, stackTrail: st, context });
167
167
  });
168
168
  }
169
+ // A `oneOf`/`anyOf` of only null members reads as nullable, not as an
170
+ // empty union.
171
+ const collapsed = collapseNullOnlyUnion(schema, normalizeMergeMember);
172
+ if (collapsed !== schema) {
173
+ return toSchemaV3({ schema: collapsed, stackTrail, context });
174
+ }
169
175
  if ('oneOf' in schema && Array.isArray(schema.oneOf)) {
170
176
  const wrapper = withoutWrapperUnevaluatedProperties({
171
177
  schema,
@@ -1,26 +1,33 @@
1
1
  import { OasTag } from '../../../oas/tag/Tag.js';
2
2
  import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
3
3
  import { toExternalDocs } from '../externalDocs/toExternalDocs.js';
4
+ import { isValidTag, toTagList } from '../../descriptiveFields.js';
4
5
  export const toTagsV3 = ({ tags, stackTrail, context }) => {
5
- if (!tags) {
6
+ if (tags === undefined) {
6
7
  return undefined;
7
8
  }
8
- const parents = new Map(tags.map(({ name, parent }) => [name, parent]));
9
- return tags.map(tag => {
9
+ // A malformed entry, or a `tags` that is not an array, is left out with a
10
+ // warning: tags do not change how an operation renders.
11
+ const validTags = toTagList({ tags, stackTrail, context })?.flatMap((tag, index) => stackTrail.trace(`${index}`, st => isValidTag({ tag, stackTrail: st, context }) ? [{ tag, index }] : []));
12
+ if (!validTags) {
13
+ return undefined;
14
+ }
15
+ const parents = new Map(validTags.map(({ tag: { name, parent } }) => [name, parent]));
16
+ return validTags.map(({ tag, index }) => stackTrail.trace(`${index}`, st => {
10
17
  const parentError = toParentError({ name: tag.name, parent: tag.parent, parents });
11
18
  if (!parentError) {
12
- return toTagV3({ tag, stackTrail, context });
19
+ return toTagV3({ tag, stackTrail: st, context });
13
20
  }
14
21
  context.logIssue({
15
22
  key: 'parent',
16
23
  level: 'warning',
17
24
  message: `Tag '${tag.name}' ${parentError}; dropping 'parent'`,
18
25
  parent: tag,
19
- stackTrail,
26
+ stackTrail: st,
20
27
  type: 'INVALID_TAG_PARENT'
21
28
  });
22
- return toTagV3({ tag: { ...tag, parent: undefined }, stackTrail, context });
23
- });
29
+ return toTagV3({ tag: { ...tag, parent: undefined }, stackTrail: st, context });
30
+ }));
24
31
  };
25
32
  /** Describes why the tag's `parent` is invalid, or returns undefined when it is valid. */
26
33
  const toParentError = ({ name, parent, parents }) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.34.0",
3
+ "version": "0.35.1",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",