@skmtc/core 0.35.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 (57) hide show
  1. package/esm/parse/v3-0/_merge-all-of/merge-bounds.d.ts +7 -0
  2. package/esm/parse/v3-0/_merge-all-of/merge-bounds.js +46 -0
  3. package/esm/parse/v3-0/_merge-all-of/merge-integer-constraints.js +2 -20
  4. package/esm/parse/v3-0/_merge-all-of/merge-number-constraints.js +2 -20
  5. package/esm/parse/v3-1/_merge-all-of/decompose-union.d.ts +2 -0
  6. package/esm/parse/v3-1/_merge-all-of/decompose-union.js +3 -1
  7. package/esm/parse/v3-1/_merge-all-of/merge-bounds.d.ts +24 -0
  8. package/esm/parse/v3-1/_merge-all-of/merge-bounds.js +57 -0
  9. package/esm/parse/v3-1/_merge-all-of/merge-integer-constraints.js +2 -20
  10. package/esm/parse/v3-1/_merge-all-of/merge-number-constraints.js +2 -20
  11. package/esm/parse/v3-1/_merge-all-of/merge-union.d.ts +7 -0
  12. package/esm/parse/v3-1/_merge-all-of/merge-union.js +65 -7
  13. package/esm/parse/v3-1/_merge-all-of/merge.js +7 -3
  14. package/esm/parse/v3-1/array/toArray.js +4 -15
  15. package/esm/parse/v3-1/schema/normalize-schema-shape.js +10 -8
  16. package/esm/parse/v3-1/schema/toSchemasV3.js +7 -1
  17. package/esm/parse/v3-2/_merge-all-of/decompose-union.d.ts +2 -0
  18. package/esm/parse/v3-2/_merge-all-of/decompose-union.js +3 -1
  19. package/esm/parse/v3-2/_merge-all-of/merge-bounds.d.ts +24 -0
  20. package/esm/parse/v3-2/_merge-all-of/merge-bounds.js +57 -0
  21. package/esm/parse/v3-2/_merge-all-of/merge-integer-constraints.js +2 -20
  22. package/esm/parse/v3-2/_merge-all-of/merge-number-constraints.js +2 -20
  23. package/esm/parse/v3-2/_merge-all-of/merge-union.d.ts +7 -0
  24. package/esm/parse/v3-2/_merge-all-of/merge-union.js +65 -7
  25. package/esm/parse/v3-2/_merge-all-of/merge.js +7 -3
  26. package/esm/parse/v3-2/array/toArray.js +4 -15
  27. package/esm/parse/v3-2/schema/normalize-schema-shape.js +10 -8
  28. package/esm/parse/v3-2/schema/toSchemasV3.js +7 -1
  29. package/package.json +1 -1
  30. package/script/parse/v3-0/_merge-all-of/merge-bounds.d.ts +7 -0
  31. package/script/parse/v3-0/_merge-all-of/merge-bounds.js +50 -0
  32. package/script/parse/v3-0/_merge-all-of/merge-integer-constraints.js +2 -20
  33. package/script/parse/v3-0/_merge-all-of/merge-number-constraints.js +2 -20
  34. package/script/parse/v3-1/_merge-all-of/decompose-union.d.ts +2 -0
  35. package/script/parse/v3-1/_merge-all-of/decompose-union.js +5 -2
  36. package/script/parse/v3-1/_merge-all-of/merge-bounds.d.ts +24 -0
  37. package/script/parse/v3-1/_merge-all-of/merge-bounds.js +66 -0
  38. package/script/parse/v3-1/_merge-all-of/merge-integer-constraints.js +2 -20
  39. package/script/parse/v3-1/_merge-all-of/merge-number-constraints.js +2 -20
  40. package/script/parse/v3-1/_merge-all-of/merge-union.d.ts +7 -0
  41. package/script/parse/v3-1/_merge-all-of/merge-union.js +66 -7
  42. package/script/parse/v3-1/_merge-all-of/merge.js +6 -2
  43. package/script/parse/v3-1/array/toArray.js +4 -15
  44. package/script/parse/v3-1/schema/normalize-schema-shape.js +10 -8
  45. package/script/parse/v3-1/schema/toSchemasV3.js +6 -0
  46. package/script/parse/v3-2/_merge-all-of/decompose-union.d.ts +2 -0
  47. package/script/parse/v3-2/_merge-all-of/decompose-union.js +5 -2
  48. package/script/parse/v3-2/_merge-all-of/merge-bounds.d.ts +24 -0
  49. package/script/parse/v3-2/_merge-all-of/merge-bounds.js +66 -0
  50. package/script/parse/v3-2/_merge-all-of/merge-integer-constraints.js +2 -20
  51. package/script/parse/v3-2/_merge-all-of/merge-number-constraints.js +2 -20
  52. package/script/parse/v3-2/_merge-all-of/merge-union.d.ts +7 -0
  53. package/script/parse/v3-2/_merge-all-of/merge-union.js +66 -7
  54. package/script/parse/v3-2/_merge-all-of/merge.js +6 -2
  55. package/script/parse/v3-2/array/toArray.js +4 -15
  56. package/script/parse/v3-2/schema/normalize-schema-shape.js +10 -8
  57. package/script/parse/v3-2/schema/toSchemasV3.js +6 -0
@@ -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,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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.35.0",
3
+ "version": "0.35.1",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -0,0 +1,7 @@
1
+ import type { SchemaObject } from './types.js';
2
+ /**
3
+ * Write the merged `minimum`/`exclusiveMinimum` and `maximum`/`exclusiveMaximum`
4
+ * of `first` and `second` onto `result`. Members are in the normalized form,
5
+ * where an exclusive bound is `minimum` plus `exclusiveMinimum: true`.
6
+ */
7
+ export declare const mergeBounds: (result: SchemaObject, first: SchemaObject, second: SchemaObject) => void;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeBounds = void 0;
4
+ /**
5
+ * Pick the tighter of two bounds. The tighter bound keeps its own
6
+ * exclusivity; when both bounds are equal, exclusive wins.
7
+ */
8
+ const toTighterBound = (first, second, isTighter) => {
9
+ if (first === undefined || second === undefined) {
10
+ return first ?? second;
11
+ }
12
+ if (first.value === second.value) {
13
+ return { value: first.value, exclusive: first.exclusive || second.exclusive };
14
+ }
15
+ return isTighter(first.value, second.value) ? first : second;
16
+ };
17
+ const toMinimum = (schema) => schema.minimum === undefined
18
+ ? undefined
19
+ : { value: schema.minimum, exclusive: schema.exclusiveMinimum === true };
20
+ const toMaximum = (schema) => schema.maximum === undefined
21
+ ? undefined
22
+ : { value: schema.maximum, exclusive: schema.exclusiveMaximum === true };
23
+ /**
24
+ * Write the merged `minimum`/`exclusiveMinimum` and `maximum`/`exclusiveMaximum`
25
+ * of `first` and `second` onto `result`. Members are in the normalized form,
26
+ * where an exclusive bound is `minimum` plus `exclusiveMinimum: true`.
27
+ */
28
+ const mergeBounds = (result, first, second) => {
29
+ const minimum = toTighterBound(toMinimum(first), toMinimum(second), (a, b) => a > b);
30
+ const maximum = toTighterBound(toMaximum(first), toMaximum(second), (a, b) => a < b);
31
+ if (minimum !== undefined) {
32
+ result.minimum = minimum.value;
33
+ if (minimum.exclusive) {
34
+ result.exclusiveMinimum = true;
35
+ }
36
+ else {
37
+ delete result.exclusiveMinimum;
38
+ }
39
+ }
40
+ if (maximum !== undefined) {
41
+ result.maximum = maximum.value;
42
+ if (maximum.exclusive) {
43
+ result.exclusiveMaximum = true;
44
+ }
45
+ else {
46
+ delete result.exclusiveMaximum;
47
+ }
48
+ }
49
+ };
50
+ exports.mergeBounds = mergeBounds;
@@ -37,32 +37,14 @@ exports.mergeIntegerConstraints = mergeIntegerConstraints;
37
37
  const generic_merge_js_1 = require("./generic-merge.js");
38
38
  const v = __importStar(require("valibot"));
39
39
  const check_type_conflicts_js_1 = require("./check-type-conflicts.js");
40
+ const merge_bounds_js_1 = require("./merge-bounds.js");
40
41
  function mergeIntegerConstraints(first, second, getRef) {
41
42
  (0, check_type_conflicts_js_1.checkTypeConflicts)(first, second);
42
43
  // First merge enum values if present
43
44
  const result = (0, generic_merge_js_1.genericMerge)(first, second, getRef, v.number());
44
45
  // Then merge other constraints
45
46
  result.type = 'integer';
46
- // Merge minimum and exclusiveMinimum
47
- if (first.minimum !== undefined || second.minimum !== undefined) {
48
- const minA = first.minimum ?? -Infinity;
49
- const minB = second.minimum ?? -Infinity;
50
- result.minimum = Math.max(minA, minB);
51
- // If either minimum is exclusive, the result is exclusive
52
- if (first.exclusiveMinimum || second.exclusiveMinimum) {
53
- result.exclusiveMinimum = true;
54
- }
55
- }
56
- // Merge maximum and exclusiveMaximum
57
- if (first.maximum !== undefined || second.maximum !== undefined) {
58
- const maxA = first.maximum ?? Infinity;
59
- const maxB = second.maximum ?? Infinity;
60
- result.maximum = Math.min(maxA, maxB);
61
- // If either maximum is exclusive, the result is exclusive
62
- if (first.exclusiveMaximum || second.exclusiveMaximum) {
63
- result.exclusiveMaximum = true;
64
- }
65
- }
47
+ (0, merge_bounds_js_1.mergeBounds)(result, first, second);
66
48
  // Merge multipleOf
67
49
  if (first.multipleOf !== undefined && second.multipleOf !== undefined) {
68
50
  result.multipleOf = first.multipleOf * second.multipleOf;
@@ -37,32 +37,14 @@ exports.mergeNumberConstraints = mergeNumberConstraints;
37
37
  const generic_merge_js_1 = require("./generic-merge.js");
38
38
  const v = __importStar(require("valibot"));
39
39
  const check_type_conflicts_js_1 = require("./check-type-conflicts.js");
40
+ const merge_bounds_js_1 = require("./merge-bounds.js");
40
41
  function mergeNumberConstraints(first, second, getRef) {
41
42
  (0, check_type_conflicts_js_1.checkTypeConflicts)(first, second);
42
43
  // First merge enum values if present
43
44
  const result = (0, generic_merge_js_1.genericMerge)(first, second, getRef, v.number());
44
45
  // Then merge other constraints
45
46
  result.type = 'number';
46
- // Merge minimum and exclusiveMinimum
47
- if (first.minimum !== undefined || second.minimum !== undefined) {
48
- const minA = first.minimum ?? -Infinity;
49
- const minB = second.minimum ?? -Infinity;
50
- result.minimum = Math.max(minA, minB);
51
- // If either minimum is exclusive, the result is exclusive
52
- if (first.exclusiveMinimum || second.exclusiveMinimum) {
53
- result.exclusiveMinimum = true;
54
- }
55
- }
56
- // Merge maximum and exclusiveMaximum
57
- if (first.maximum !== undefined || second.maximum !== undefined) {
58
- const maxA = first.maximum ?? Infinity;
59
- const maxB = second.maximum ?? Infinity;
60
- result.maximum = Math.min(maxA, maxB);
61
- // If either maximum is exclusive, the result is exclusive
62
- if (first.exclusiveMaximum || second.exclusiveMaximum) {
63
- result.exclusiveMaximum = true;
64
- }
65
- }
47
+ (0, merge_bounds_js_1.mergeBounds)(result, first, second);
66
48
  // Merge multipleOf
67
49
  if (first.multipleOf !== undefined && second.multipleOf !== undefined) {
68
50
  result.multipleOf = first.multipleOf * second.multipleOf;
@@ -9,4 +9,6 @@ type DecomposeUnionReturn = {
9
9
  afterExcluded: SchemaObject;
10
10
  };
11
11
  export declare const decomposeUnion: ({ schema, groupType }: DecomposeUnionArgs) => DecomposeUnionReturn;
12
+ /** Is `key` a union-level key, kept on the union rather than merged into each member? */
13
+ export declare const isUnionLevelKey: (key: string) => boolean;
12
14
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decomposeUnion = void 0;
3
+ exports.isUnionLevelKey = exports.decomposeUnion = void 0;
4
4
  // Convert a schema object into an array of schemas that can be merged iteratively into a new schema
5
5
  const decomposeUnion = ({ schema, groupType }) => {
6
6
  if (!schema[groupType]) {
@@ -60,9 +60,12 @@ const excludedProperties = [
60
60
  'writeOnly',
61
61
  'deprecated'
62
62
  ];
63
+ /** Is `key` a union-level key, kept on the union rather than merged into each member? */
64
+ const isUnionLevelKey = (key) => excludedProperties.includes(key);
65
+ exports.isUnionLevelKey = isUnionLevelKey;
63
66
  const exclude = (entries) => {
64
67
  return entries.reduce((acc, [key, value]) => {
65
- if (excludedProperties.includes(key)) {
68
+ if ((0, exports.isUnionLevelKey)(key)) {
66
69
  acc.excluded.push([key, value]);
67
70
  }
68
71
  else {
@@ -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,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeBounds = exports.mergeBounds = exports.toMaximum = exports.toMinimum = exports.toTighterMaximum = exports.toTighterMinimum = void 0;
4
+ /**
5
+ * Pick the tighter of two bounds. The tighter bound keeps its own
6
+ * exclusivity; when both bounds are equal, exclusive wins.
7
+ */
8
+ const toTighterBound = (first, second, isTighter) => {
9
+ if (first === undefined || second === undefined) {
10
+ return first ?? second;
11
+ }
12
+ if (first.value === second.value) {
13
+ return { value: first.value, exclusive: first.exclusive || second.exclusive };
14
+ }
15
+ return isTighter(first.value, second.value) ? first : second;
16
+ };
17
+ /** The tighter of two lower bounds. */
18
+ const toTighterMinimum = (first, second) => toTighterBound(first, second, (a, b) => a > b);
19
+ exports.toTighterMinimum = toTighterMinimum;
20
+ /** The tighter of two upper bounds. */
21
+ const toTighterMaximum = (first, second) => toTighterBound(first, second, (a, b) => a < b);
22
+ exports.toTighterMaximum = toTighterMaximum;
23
+ /** The lower bound of `schema`; exclusive only when `exclusiveMinimum` is `true`. */
24
+ const toMinimum = (schema) => schema.minimum === undefined
25
+ ? undefined
26
+ : { value: schema.minimum, exclusive: schema.exclusiveMinimum === true };
27
+ exports.toMinimum = toMinimum;
28
+ /** The upper bound of `schema`; exclusive only when `exclusiveMaximum` is `true`. */
29
+ const toMaximum = (schema) => schema.maximum === undefined
30
+ ? undefined
31
+ : { value: schema.maximum, exclusive: schema.exclusiveMaximum === true };
32
+ exports.toMaximum = toMaximum;
33
+ /**
34
+ * Write the merged `minimum`/`exclusiveMinimum` and `maximum`/`exclusiveMaximum`
35
+ * of `first` and `second` onto `result`. Members are in the normalized form,
36
+ * where an exclusive bound is `minimum` plus `exclusiveMinimum: true`.
37
+ */
38
+ const mergeBounds = (result, first, second) => {
39
+ (0, exports.writeBounds)(result, (0, exports.toTighterMinimum)((0, exports.toMinimum)(first), (0, exports.toMinimum)(second)), (0, exports.toTighterMaximum)((0, exports.toMaximum)(first), (0, exports.toMaximum)(second)));
40
+ };
41
+ exports.mergeBounds = mergeBounds;
42
+ /**
43
+ * Write `minimum` and `maximum` onto `result` in the normalized form. An
44
+ * undefined bound leaves `result` unchanged.
45
+ */
46
+ const writeBounds = (result, minimum, maximum) => {
47
+ if (minimum !== undefined) {
48
+ result.minimum = minimum.value;
49
+ if (minimum.exclusive) {
50
+ result.exclusiveMinimum = true;
51
+ }
52
+ else {
53
+ delete result.exclusiveMinimum;
54
+ }
55
+ }
56
+ if (maximum !== undefined) {
57
+ result.maximum = maximum.value;
58
+ if (maximum.exclusive) {
59
+ result.exclusiveMaximum = true;
60
+ }
61
+ else {
62
+ delete result.exclusiveMaximum;
63
+ }
64
+ }
65
+ };
66
+ exports.writeBounds = writeBounds;
@@ -37,32 +37,14 @@ exports.mergeIntegerConstraints = mergeIntegerConstraints;
37
37
  const generic_merge_js_1 = require("./generic-merge.js");
38
38
  const v = __importStar(require("valibot"));
39
39
  const check_type_conflicts_js_1 = require("./check-type-conflicts.js");
40
+ const merge_bounds_js_1 = require("./merge-bounds.js");
40
41
  function mergeIntegerConstraints(first, second, getRef, normalize) {
41
42
  (0, check_type_conflicts_js_1.checkTypeConflicts)(first, second);
42
43
  // First merge enum values if present
43
44
  const result = (0, generic_merge_js_1.genericMerge)(first, second, getRef, normalize, v.number());
44
45
  // Then merge other constraints
45
46
  result.type = 'integer';
46
- // Merge minimum and exclusiveMinimum
47
- if (first.minimum !== undefined || second.minimum !== undefined) {
48
- const minA = first.minimum ?? -Infinity;
49
- const minB = second.minimum ?? -Infinity;
50
- result.minimum = Math.max(minA, minB);
51
- // If either minimum is exclusive, the result is exclusive
52
- if (first.exclusiveMinimum || second.exclusiveMinimum) {
53
- result.exclusiveMinimum = true;
54
- }
55
- }
56
- // Merge maximum and exclusiveMaximum
57
- if (first.maximum !== undefined || second.maximum !== undefined) {
58
- const maxA = first.maximum ?? Infinity;
59
- const maxB = second.maximum ?? Infinity;
60
- result.maximum = Math.min(maxA, maxB);
61
- // If either maximum is exclusive, the result is exclusive
62
- if (first.exclusiveMaximum || second.exclusiveMaximum) {
63
- result.exclusiveMaximum = true;
64
- }
65
- }
47
+ (0, merge_bounds_js_1.mergeBounds)(result, first, second);
66
48
  // Merge multipleOf
67
49
  if (first.multipleOf !== undefined && second.multipleOf !== undefined) {
68
50
  result.multipleOf = first.multipleOf * second.multipleOf;
@@ -37,32 +37,14 @@ exports.mergeNumberConstraints = mergeNumberConstraints;
37
37
  const generic_merge_js_1 = require("./generic-merge.js");
38
38
  const v = __importStar(require("valibot"));
39
39
  const check_type_conflicts_js_1 = require("./check-type-conflicts.js");
40
+ const merge_bounds_js_1 = require("./merge-bounds.js");
40
41
  function mergeNumberConstraints(first, second, getRef, normalize) {
41
42
  (0, check_type_conflicts_js_1.checkTypeConflicts)(first, second);
42
43
  // First merge enum values if present
43
44
  const result = (0, generic_merge_js_1.genericMerge)(first, second, getRef, normalize, v.number());
44
45
  // Then merge other constraints
45
46
  result.type = 'number';
46
- // Merge minimum and exclusiveMinimum
47
- if (first.minimum !== undefined || second.minimum !== undefined) {
48
- const minA = first.minimum ?? -Infinity;
49
- const minB = second.minimum ?? -Infinity;
50
- result.minimum = Math.max(minA, minB);
51
- // If either minimum is exclusive, the result is exclusive
52
- if (first.exclusiveMinimum || second.exclusiveMinimum) {
53
- result.exclusiveMinimum = true;
54
- }
55
- }
56
- // Merge maximum and exclusiveMaximum
57
- if (first.maximum !== undefined || second.maximum !== undefined) {
58
- const maxA = first.maximum ?? Infinity;
59
- const maxB = second.maximum ?? Infinity;
60
- result.maximum = Math.min(maxA, maxB);
61
- // If either maximum is exclusive, the result is exclusive
62
- if (first.exclusiveMaximum || second.exclusiveMaximum) {
63
- result.exclusiveMaximum = true;
64
- }
65
- }
47
+ (0, merge_bounds_js_1.mergeBounds)(result, first, second);
66
48
  // Merge multipleOf
67
49
  if (first.multipleOf !== undefined && second.multipleOf !== undefined) {
68
50
  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 {};