@modulify/validator 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/README.md +12 -2
  3. package/dist/assert.cjs +99 -63
  4. package/dist/assert.d.cts +37 -0
  5. package/dist/assert.d.mts +37 -0
  6. package/dist/assert.d.ts +24 -3
  7. package/dist/assert.mjs +94 -64
  8. package/dist/assertions.cjs +283 -178
  9. package/dist/assertions.d.cts +56 -0
  10. package/dist/assertions.d.mts +56 -0
  11. package/dist/assertions.d.ts +43 -45
  12. package/dist/assertions.mjs +274 -201
  13. package/dist/checkers.cjs +23 -0
  14. package/dist/checkers.d.cts +8 -0
  15. package/dist/checkers.d.mts +8 -0
  16. package/dist/checkers.d.ts +1 -1
  17. package/dist/checkers.mjs +16 -0
  18. package/dist/combinators.cjs +305 -304
  19. package/dist/combinators.d.cts +16 -0
  20. package/dist/combinators.d.mts +16 -0
  21. package/dist/combinators.d.ts +15 -16
  22. package/dist/combinators.mjs +305 -314
  23. package/dist/constraints.cjs +23 -0
  24. package/dist/constraints.d.cts +4 -0
  25. package/dist/constraints.d.mts +4 -0
  26. package/dist/constraints.d.ts +2 -2
  27. package/dist/constraints.mjs +21 -0
  28. package/dist/extractors.cjs +6 -0
  29. package/dist/extractors.d.cts +2 -0
  30. package/dist/extractors.d.mts +2 -0
  31. package/dist/extractors.mjs +5 -0
  32. package/dist/index.cjs +140 -219
  33. package/dist/index.d.cts +12 -0
  34. package/dist/index.d.mts +12 -0
  35. package/dist/index.d.ts +9 -9
  36. package/dist/index.mjs +93 -222
  37. package/dist/json-schema.cjs +364 -489
  38. package/dist/json-schema.d.cts +14 -0
  39. package/dist/json-schema.d.mts +14 -0
  40. package/dist/json-schema.d.ts +3 -3
  41. package/dist/json-schema.mjs +365 -492
  42. package/dist/metadata.cjs +98 -7
  43. package/dist/metadata.d.cts +8 -0
  44. package/dist/metadata.d.mts +8 -0
  45. package/dist/metadata.d.ts +2 -2
  46. package/dist/metadata.mjs +93 -7
  47. package/dist/predicates.cjs +98 -41
  48. package/dist/predicates.d.cts +77 -0
  49. package/dist/predicates.d.mts +77 -0
  50. package/dist/predicates.d.ts +25 -4
  51. package/dist/predicates.mjs +92 -66
  52. package/dist/types/index.d.cts +984 -0
  53. package/dist/types/index.d.mts +984 -0
  54. package/dist/types/index.d.ts +984 -0
  55. package/dist/types/json-schema.d.cts +75 -0
  56. package/dist/types/json-schema.d.mts +75 -0
  57. package/dist/types/json-schema.d.ts +75 -0
  58. package/dist/violations.cjs +81 -0
  59. package/dist/violations.d.cts +29 -0
  60. package/dist/violations.d.mts +29 -0
  61. package/dist/violations.d.ts +1 -1
  62. package/dist/violations.mjs +80 -0
  63. package/docs/RELEASING.md +66 -0
  64. package/docs/en/00-index.md +2 -0
  65. package/docs/en/01-shape-api.md +35 -10
  66. package/docs/en/02-metadata-and-introspection.md +2 -1
  67. package/docs/en/03-violations.md +1 -1
  68. package/docs/en/04-json-schema-export.md +5 -0
  69. package/docs/en/05-public-api.md +35 -3
  70. package/docs/en/06-common-recipes.md +2 -1
  71. package/docs/en/07-ai-reference.md +5 -2
  72. package/docs/en/08-violation-code-types.md +28 -2
  73. package/docs/en/09-migration.md +75 -0
  74. package/docs/ru/00-index.md +2 -0
  75. package/docs/ru/01-shape-api.md +35 -10
  76. package/docs/ru/02-metadata-and-introspection.md +2 -1
  77. package/docs/ru/03-violations.md +1 -1
  78. package/docs/ru/04-json-schema-export.md +5 -0
  79. package/docs/ru/05-public-api.md +35 -3
  80. package/docs/ru/06-common-recipes.md +2 -1
  81. package/docs/ru/07-ai-reference.md +5 -2
  82. package/docs/ru/08-violation-code-types.md +28 -2
  83. package/docs/ru/09-migration.md +76 -0
  84. package/docs/ru/README.md +10 -2
  85. package/package.json +63 -37
  86. package/types/index.d.ts +275 -115
  87. package/dist/metadata.cjs.js +0 -130
  88. package/dist/metadata.es.js +0 -131
@@ -1,514 +1,389 @@
1
- "use strict";
2
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const metadata = require("./metadata.cjs.js");
4
- const jsonSchemaMetadataKeys = [
5
- "title",
6
- "description",
7
- "format",
8
- "default",
9
- "examples",
10
- "deprecated",
11
- "readOnly",
12
- "writeOnly"
2
+ const require_metadata = require("./metadata.cjs");
3
+ //#region src/json-schema.ts
4
+ var jsonSchemaMetadataKeys = [
5
+ "title",
6
+ "description",
7
+ "format",
8
+ "default",
9
+ "examples",
10
+ "deprecated",
11
+ "readOnly",
12
+ "writeOnly"
13
13
  ];
14
- const typeOf = (value) => Object.prototype.toString.call(value);
15
- const isEmptySchema = (schema) => Object.keys(schema).length === 0;
16
- const isFiniteJsonNumber = (value) => typeof value === "number" && Number.isFinite(value);
17
- const isJsonScalar = (value) => {
18
- return value === null || typeof value === "string" || typeof value === "boolean" || isFiniteJsonNumber(value);
14
+ var typeOf = (value) => Object.prototype.toString.call(value);
15
+ var isEmptySchema = (schema) => Object.keys(schema).length === 0;
16
+ var isFiniteJsonNumber = (value) => typeof value === "number" && Number.isFinite(value);
17
+ var isJsonScalar = (value) => {
18
+ return value === null || typeof value === "string" || typeof value === "boolean" || isFiniteJsonNumber(value);
19
19
  };
20
- const isLength = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
21
- const isFiniteNumber = (value) => typeof value === "number" && Number.isFinite(value);
22
- const isPositiveFiniteNumber = (value) => isFiniteNumber(value) && value > 0;
23
- const withPath = (context, segment) => ({
24
- ...context,
25
- path: [...context.path, segment]
20
+ var isLength = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
21
+ var isFiniteNumber = (value) => typeof value === "number" && Number.isFinite(value);
22
+ var isPositiveFiniteNumber = (value) => isFiniteNumber(value) && value > 0;
23
+ var withPath = (context, segment) => ({
24
+ ...context,
25
+ path: [...context.path, segment]
26
26
  });
27
- const formatPath = (path) => path.length === 0 ? "<root>" : path.map((segment) => typeof segment === "symbol" ? segment.toString() : String(segment)).join(".");
28
- const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
29
- const setSchemaProperty = (schema, key, value) => {
30
- schema[key] = value;
27
+ var formatPath = (path) => path.length === 0 ? "<root>" : path.map((segment) => typeof segment === "symbol" ? segment.toString() : String(segment)).join(".");
28
+ var escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
29
+ var setSchemaProperty = (schema, key, value) => {
30
+ schema[key] = value;
31
31
  };
32
- const pickMetadata = (metadata2) => {
33
- if (!metadata2) {
34
- return {};
35
- }
36
- const schemaMetadata = {};
37
- jsonSchemaMetadataKeys.forEach((key) => {
38
- if (!(key in metadata2)) {
39
- return;
40
- }
41
- const value = metadata2[key];
42
- if (key === "examples") {
43
- if (Array.isArray(value)) {
44
- setSchemaProperty(schemaMetadata, "examples", [...value]);
45
- }
46
- return;
47
- }
48
- if (key === "default") {
49
- setSchemaProperty(schemaMetadata, "default", value);
50
- return;
51
- }
52
- if ((key === "deprecated" || key === "readOnly" || key === "writeOnly") && typeof value === "boolean") {
53
- setSchemaProperty(schemaMetadata, key, value);
54
- return;
55
- }
56
- if (typeof value === "string") {
57
- setSchemaProperty(schemaMetadata, key, value);
58
- }
59
- });
60
- return schemaMetadata;
32
+ var pickMetadata = (metadata) => {
33
+ if (!metadata) return {};
34
+ const schemaMetadata = {};
35
+ jsonSchemaMetadataKeys.forEach((key) => {
36
+ if (!(key in metadata)) return;
37
+ const value = metadata[key];
38
+ if (key === "examples") {
39
+ if (Array.isArray(value)) setSchemaProperty(schemaMetadata, "examples", [...value]);
40
+ return;
41
+ }
42
+ if (key === "default") {
43
+ setSchemaProperty(schemaMetadata, "default", value);
44
+ return;
45
+ }
46
+ if ((key === "deprecated" || key === "readOnly" || key === "writeOnly") && typeof value === "boolean") {
47
+ setSchemaProperty(schemaMetadata, key, value);
48
+ return;
49
+ }
50
+ if (typeof value === "string") setSchemaProperty(schemaMetadata, key, value);
51
+ });
52
+ return schemaMetadata;
61
53
  };
62
- const applyMetadata = (schema, metadata2) => {
63
- const schemaMetadata = pickMetadata(metadata2);
64
- const nextSchema = { ...schema };
65
- jsonSchemaMetadataKeys.forEach((key) => {
66
- if (!(key in schemaMetadata) || key in nextSchema) {
67
- return;
68
- }
69
- setSchemaProperty(nextSchema, key, schemaMetadata[key]);
70
- });
71
- return nextSchema;
54
+ var applyMetadata = (schema, metadata) => {
55
+ const schemaMetadata = pickMetadata(metadata);
56
+ const nextSchema = { ...schema };
57
+ jsonSchemaMetadataKeys.forEach((key) => {
58
+ if (!(key in schemaMetadata) || key in nextSchema) return;
59
+ setSchemaProperty(nextSchema, key, schemaMetadata[key]);
60
+ });
61
+ return nextSchema;
72
62
  };
73
- class JsonSchemaExportError extends Error {
74
- constructor(message, {
75
- descriptor,
76
- path = [],
77
- reason
78
- }) {
79
- super(message);
80
- this.name = "JsonSchemaExportError";
81
- this.descriptor = descriptor;
82
- this.reason = reason;
83
- this.path = [...path];
84
- }
85
- }
86
- const unsupported = (descriptor, context, reason) => {
87
- if (context.mode === "strict") {
88
- throw new JsonSchemaExportError(
89
- `Cannot export ${descriptor.kind} at ${formatPath(context.path)}: ${reason}`,
90
- { descriptor, path: context.path, reason }
91
- );
92
- }
93
- return {};
63
+ var JsonSchemaExportError = class extends Error {
64
+ constructor(message, { descriptor, path = [], reason }) {
65
+ super(message);
66
+ this.name = "JsonSchemaExportError";
67
+ this.descriptor = descriptor;
68
+ this.reason = reason;
69
+ this.path = [...path];
70
+ }
94
71
  };
95
- const mergeNullable = (schema) => {
96
- if (isEmptySchema(schema)) {
97
- return schema;
98
- }
99
- return {
100
- anyOf: [schema, { type: "null" }]
101
- };
72
+ var unsupported = (descriptor, context, reason) => {
73
+ if (context.mode === "strict") throw new JsonSchemaExportError(`Cannot export ${descriptor.kind} at ${formatPath(context.path)}: ${reason}`, {
74
+ descriptor,
75
+ path: context.path,
76
+ reason
77
+ });
78
+ return {};
102
79
  };
103
- const toPropertyName = (key, descriptor, context) => {
104
- if (typeof key === "symbol") {
105
- return unsupported(descriptor, context, "symbol keys cannot be represented in JSON Schema");
106
- }
107
- return String(key);
80
+ var mergeNullable = (schema) => {
81
+ if (isEmptySchema(schema)) return schema;
82
+ return { anyOf: [schema, { type: "null" }] };
108
83
  };
109
- const lengthSchema = (descriptor, context) => {
110
- const stringSchema2 = { type: "string" };
111
- const arraySchema = { type: "array" };
112
- for (const constraint of descriptor.constraints) {
113
- switch (constraint.code) {
114
- case "length.exact": {
115
- const exact = constraint.args[0];
116
- if (!isLength(exact)) {
117
- return unsupported(descriptor, context, "hasLength exact bounds must be non-negative integers");
118
- }
119
- setSchemaProperty(stringSchema2, "minLength", exact);
120
- setSchemaProperty(stringSchema2, "maxLength", exact);
121
- setSchemaProperty(arraySchema, "minItems", exact);
122
- setSchemaProperty(arraySchema, "maxItems", exact);
123
- break;
124
- }
125
- case "length.min": {
126
- const min = constraint.args[0];
127
- if (!isLength(min)) {
128
- return unsupported(descriptor, context, "hasLength minimum bounds must be non-negative integers");
129
- }
130
- setSchemaProperty(stringSchema2, "minLength", min);
131
- setSchemaProperty(arraySchema, "minItems", min);
132
- break;
133
- }
134
- case "length.max": {
135
- const max = constraint.args[0];
136
- if (!isLength(max)) {
137
- return unsupported(descriptor, context, "hasLength maximum bounds must be non-negative integers");
138
- }
139
- setSchemaProperty(stringSchema2, "maxLength", max);
140
- setSchemaProperty(arraySchema, "maxItems", max);
141
- break;
142
- }
143
- case "length.range": {
144
- const range = constraint.args[0];
145
- if (!Array.isArray(range) || range.length !== 2 || !isLength(range[0]) || !isLength(range[1])) {
146
- return unsupported(descriptor, context, "hasLength ranges must be `[min, max]` integer tuples");
147
- }
148
- setSchemaProperty(stringSchema2, "minLength", range[0]);
149
- setSchemaProperty(stringSchema2, "maxLength", range[1]);
150
- setSchemaProperty(arraySchema, "minItems", range[0]);
151
- setSchemaProperty(arraySchema, "maxItems", range[1]);
152
- break;
153
- }
154
- default:
155
- return unsupported(descriptor, context, `unsupported hasLength constraint "${constraint.code}"`);
156
- }
157
- }
158
- return {
159
- anyOf: [stringSchema2, arraySchema]
160
- };
84
+ var toPropertyName = (key, descriptor, context) => {
85
+ if (typeof key === "symbol") return unsupported(descriptor, context, "symbol keys cannot be represented in JSON Schema");
86
+ return String(key);
161
87
  };
162
- const exactSchema = (descriptor, context) => {
163
- const [value] = descriptor.args ?? [];
164
- if (!isJsonScalar(value)) {
165
- return unsupported(
166
- descriptor,
167
- context,
168
- `exact(...) supports only JSON scalar values, got ${typeOf(value)}`
169
- );
170
- }
171
- return { const: value };
88
+ var lengthSchema = (descriptor, context) => {
89
+ const stringSchema = { type: "string" };
90
+ const arraySchema = { type: "array" };
91
+ for (const constraint of descriptor.constraints) switch (constraint.code) {
92
+ case "length.exact": {
93
+ const exact = constraint.args[0];
94
+ if (!isLength(exact)) return unsupported(descriptor, context, "hasLength exact bounds must be non-negative integers");
95
+ setSchemaProperty(stringSchema, "minLength", exact);
96
+ setSchemaProperty(stringSchema, "maxLength", exact);
97
+ setSchemaProperty(arraySchema, "minItems", exact);
98
+ setSchemaProperty(arraySchema, "maxItems", exact);
99
+ break;
100
+ }
101
+ case "length.min": {
102
+ const min = constraint.args[0];
103
+ if (!isLength(min)) return unsupported(descriptor, context, "hasLength minimum bounds must be non-negative integers");
104
+ setSchemaProperty(stringSchema, "minLength", min);
105
+ setSchemaProperty(arraySchema, "minItems", min);
106
+ break;
107
+ }
108
+ case "length.max": {
109
+ const max = constraint.args[0];
110
+ if (!isLength(max)) return unsupported(descriptor, context, "hasLength maximum bounds must be non-negative integers");
111
+ setSchemaProperty(stringSchema, "maxLength", max);
112
+ setSchemaProperty(arraySchema, "maxItems", max);
113
+ break;
114
+ }
115
+ case "length.range": {
116
+ const range = constraint.args[0];
117
+ if (!Array.isArray(range) || range.length !== 2 || !isLength(range[0]) || !isLength(range[1])) return unsupported(descriptor, context, "hasLength ranges must be `[min, max]` integer tuples");
118
+ setSchemaProperty(stringSchema, "minLength", range[0]);
119
+ setSchemaProperty(stringSchema, "maxLength", range[1]);
120
+ setSchemaProperty(arraySchema, "minItems", range[0]);
121
+ setSchemaProperty(arraySchema, "maxItems", range[1]);
122
+ break;
123
+ }
124
+ default: return unsupported(descriptor, context, `unsupported hasLength constraint "${constraint.code}"`);
125
+ }
126
+ return { anyOf: [stringSchema, arraySchema] };
172
127
  };
173
- const enumSchema = (descriptor, context) => {
174
- const [values] = descriptor.args ?? [];
175
- if (!Array.isArray(values) || values.some((value) => !isJsonScalar(value))) {
176
- return unsupported(
177
- descriptor,
178
- context,
179
- "oneOf(...) supports only arrays of JSON scalar values"
180
- );
181
- }
182
- return { enum: [...new Set(values)] };
128
+ var exactSchema = (descriptor, context) => {
129
+ const [value] = descriptor.args ?? [];
130
+ if (!isJsonScalar(value)) return unsupported(descriptor, context, `exact(...) supports only JSON scalar values, got ${typeOf(value)}`);
131
+ return { const: value };
183
132
  };
184
- const stringSchema = (descriptor, context) => {
185
- if (descriptor.name === "hasPattern") {
186
- const [pattern] = descriptor.constraints[0]?.args ?? [];
187
- if (!(pattern instanceof RegExp)) {
188
- return unsupported(descriptor, context, "hasPattern(...) requires a RegExp pattern");
189
- }
190
- if (pattern.flags !== "") {
191
- return unsupported(descriptor, context, "hasPattern(...) supports only flagless regular expressions in JSON Schema");
192
- }
193
- return {
194
- type: "string",
195
- pattern: pattern.source
196
- };
197
- }
198
- if (descriptor.name === "startsWith") {
199
- const [prefix] = descriptor.constraints[0]?.args ?? [];
200
- if (typeof prefix !== "string") {
201
- return unsupported(descriptor, context, "startsWith(...) requires a string prefix");
202
- }
203
- return {
204
- type: "string",
205
- pattern: `^${escapeRegExp(prefix)}`
206
- };
207
- }
208
- const [suffix] = descriptor.constraints[0]?.args ?? [];
209
- if (typeof suffix !== "string") {
210
- return unsupported(descriptor, context, "endsWith(...) requires a string suffix");
211
- }
212
- return {
213
- type: "string",
214
- pattern: `${escapeRegExp(suffix)}$`
215
- };
133
+ var enumSchema = (descriptor, context) => {
134
+ const [values] = descriptor.args ?? [];
135
+ if (!Array.isArray(values) || values.some((value) => !isJsonScalar(value))) return unsupported(descriptor, context, "oneOf(...) supports only arrays of JSON scalar values");
136
+ return { enum: [...new Set(values)] };
216
137
  };
217
- const numberSchema = (descriptor, context) => {
218
- const schema = { type: "number" };
219
- if (descriptor.name === "multipleOf") {
220
- const [step] = descriptor.constraints[0]?.args ?? [];
221
- if (!isPositiveFiniteNumber(step)) {
222
- return unsupported(descriptor, context, "multipleOf(...) requires a positive finite divisor");
223
- }
224
- setSchemaProperty(schema, "multipleOf", step);
225
- return schema;
226
- }
227
- for (const constraint of descriptor.constraints) {
228
- switch (constraint.code) {
229
- case "number.exact": {
230
- const exact = constraint.args[0];
231
- if (!isFiniteNumber(exact)) {
232
- return unsupported(descriptor, context, "hasValue exact bounds must be finite numbers");
233
- }
234
- setSchemaProperty(schema, "const", exact);
235
- break;
236
- }
237
- case "number.min": {
238
- const min = constraint.args[0];
239
- if (!isFiniteNumber(min)) {
240
- return unsupported(descriptor, context, "hasValue minimum bounds must be finite numbers");
241
- }
242
- setSchemaProperty(schema, "minimum", min);
243
- break;
244
- }
245
- case "number.max": {
246
- const max = constraint.args[0];
247
- if (!isFiniteNumber(max)) {
248
- return unsupported(descriptor, context, "hasValue maximum bounds must be finite numbers");
249
- }
250
- setSchemaProperty(schema, "maximum", max);
251
- break;
252
- }
253
- case "number.range": {
254
- const range = constraint.args[0];
255
- if (!Array.isArray(range) || range.length !== 2 || !isFiniteNumber(range[0]) || !isFiniteNumber(range[1])) {
256
- return unsupported(descriptor, context, "hasValue ranges must be `[min, max]` finite number tuples");
257
- }
258
- setSchemaProperty(schema, "minimum", range[0]);
259
- setSchemaProperty(schema, "maximum", range[1]);
260
- break;
261
- }
262
- default:
263
- return unsupported(descriptor, context, `unsupported number assertion "${descriptor.name}"`);
264
- }
265
- }
266
- return schema;
138
+ var stringSchema = (descriptor, context) => {
139
+ if (descriptor.name === "hasPattern") {
140
+ const [pattern] = descriptor.constraints[0]?.args ?? [];
141
+ if (!(pattern instanceof RegExp)) return unsupported(descriptor, context, "hasPattern(...) requires a RegExp pattern");
142
+ if (pattern.flags !== "") return unsupported(descriptor, context, "hasPattern(...) supports only flagless regular expressions in JSON Schema");
143
+ return {
144
+ type: "string",
145
+ pattern: pattern.source
146
+ };
147
+ }
148
+ if (descriptor.name === "startsWith") {
149
+ const [prefix] = descriptor.constraints[0]?.args ?? [];
150
+ if (typeof prefix !== "string") return unsupported(descriptor, context, "startsWith(...) requires a string prefix");
151
+ return {
152
+ type: "string",
153
+ pattern: `^${escapeRegExp(prefix)}`
154
+ };
155
+ }
156
+ const [suffix] = descriptor.constraints[0]?.args ?? [];
157
+ if (typeof suffix !== "string") return unsupported(descriptor, context, "endsWith(...) requires a string suffix");
158
+ return {
159
+ type: "string",
160
+ pattern: `${escapeRegExp(suffix)}$`
161
+ };
267
162
  };
268
- const assertionAcceptsUndefined = (descriptor) => {
269
- switch (descriptor.name) {
270
- case "exact":
271
- return descriptor.args?.[0] === void 0;
272
- case "oneOf": {
273
- const [values] = descriptor.args ?? [];
274
- return Array.isArray(values) && values.includes(void 0);
275
- }
276
- default:
277
- return false;
278
- }
163
+ var numberSchema = (descriptor, context) => {
164
+ const schema = { type: "number" };
165
+ if (descriptor.name === "isInteger") {
166
+ setSchemaProperty(schema, "type", "integer");
167
+ return schema;
168
+ }
169
+ if (descriptor.name === "isSafeInteger") {
170
+ setSchemaProperty(schema, "type", "integer");
171
+ setSchemaProperty(schema, "minimum", Number.MIN_SAFE_INTEGER);
172
+ setSchemaProperty(schema, "maximum", Number.MAX_SAFE_INTEGER);
173
+ return schema;
174
+ }
175
+ if (descriptor.name === "multipleOf") {
176
+ const [step] = descriptor.constraints[0]?.args ?? [];
177
+ if (!isPositiveFiniteNumber(step)) return unsupported(descriptor, context, "multipleOf(...) requires a positive finite divisor");
178
+ setSchemaProperty(schema, "multipleOf", step);
179
+ return schema;
180
+ }
181
+ for (const constraint of descriptor.constraints) switch (constraint.code) {
182
+ case "number.exact": {
183
+ const exact = constraint.args[0];
184
+ if (!isFiniteNumber(exact)) return unsupported(descriptor, context, "hasValue exact bounds must be finite numbers");
185
+ setSchemaProperty(schema, "const", exact);
186
+ break;
187
+ }
188
+ case "number.min": {
189
+ const min = constraint.args[0];
190
+ if (!isFiniteNumber(min)) return unsupported(descriptor, context, "hasValue minimum bounds must be finite numbers");
191
+ setSchemaProperty(schema, "minimum", min);
192
+ break;
193
+ }
194
+ case "number.max": {
195
+ const max = constraint.args[0];
196
+ if (!isFiniteNumber(max)) return unsupported(descriptor, context, "hasValue maximum bounds must be finite numbers");
197
+ setSchemaProperty(schema, "maximum", max);
198
+ break;
199
+ }
200
+ case "number.range": {
201
+ const range = constraint.args[0];
202
+ if (!Array.isArray(range) || range.length !== 2 || !isFiniteNumber(range[0]) || !isFiniteNumber(range[1])) return unsupported(descriptor, context, "hasValue ranges must be `[min, max]` finite number tuples");
203
+ setSchemaProperty(schema, "minimum", range[0]);
204
+ setSchemaProperty(schema, "maximum", range[1]);
205
+ break;
206
+ }
207
+ default: return unsupported(descriptor, context, `unsupported number assertion "${descriptor.name}"`);
208
+ }
209
+ return schema;
279
210
  };
280
- const acceptsUndefined = (descriptor) => {
281
- switch (descriptor.kind) {
282
- case "optional":
283
- case "nullish":
284
- return true;
285
- case "nullable":
286
- case "each":
287
- case "tuple":
288
- case "record":
289
- case "shape":
290
- case "discriminatedUnion":
291
- case "validator":
292
- return false;
293
- case "allOf":
294
- return descriptor.constraints.every(acceptsUndefined);
295
- case "union":
296
- return descriptor.branches.some(acceptsUndefined);
297
- case "assertion":
298
- return assertionAcceptsUndefined(descriptor);
299
- default:
300
- return false;
301
- }
211
+ var assertionAcceptsUndefined = (descriptor) => {
212
+ switch (descriptor.name) {
213
+ case "exact": return descriptor.args?.[0] === void 0;
214
+ case "oneOf": {
215
+ const [values] = descriptor.args ?? [];
216
+ return Array.isArray(values) && values.includes(void 0);
217
+ }
218
+ default: return false;
219
+ }
302
220
  };
303
- const exportShapeRules = (descriptor, context) => {
304
- if (descriptor.rules.length === 0 || context.mode === "bestEffort") {
305
- return;
306
- }
307
- const [rule] = descriptor.rules;
308
- throw new JsonSchemaExportError(
309
- `Cannot export shape at ${formatPath([...context.path, "rules"])}: object-level rule "${rule.kind}" has no JSON Schema mapping`,
310
- {
311
- descriptor,
312
- path: [...context.path, "rules"],
313
- reason: `object-level rule "${rule.kind}" has no JSON Schema mapping`
314
- }
315
- );
221
+ var acceptsUndefined = (descriptor) => {
222
+ switch (descriptor.kind) {
223
+ case "optional":
224
+ case "nullish": return true;
225
+ case "nullable":
226
+ case "each":
227
+ case "tuple":
228
+ case "record":
229
+ case "shape":
230
+ case "discriminatedUnion":
231
+ case "validator": return false;
232
+ case "allOf": return descriptor.constraints.every(acceptsUndefined);
233
+ case "union": return descriptor.branches.some(acceptsUndefined);
234
+ case "assertion": return assertionAcceptsUndefined(descriptor);
235
+ default: return false;
236
+ }
316
237
  };
317
- const exportAssertion = (descriptor, context) => {
318
- switch (descriptor.name) {
319
- case "isString":
320
- return { type: "string" };
321
- case "isNumber":
322
- return { type: "number" };
323
- case "isBoolean":
324
- return { type: "boolean" };
325
- case "isBigInt":
326
- return unsupported(descriptor, context, "bigint values cannot be represented in JSON Schema");
327
- case "isBlob":
328
- return unsupported(descriptor, context, "Blob instances do not have a stable JSON Schema representation");
329
- case "isNull":
330
- return { type: "null" };
331
- case "isEmail":
332
- return {
333
- type: "string",
334
- format: "email"
335
- };
336
- case "hasPattern":
337
- case "startsWith":
338
- case "endsWith":
339
- return stringSchema(descriptor, context);
340
- case "isFile":
341
- return unsupported(descriptor, context, "File instances do not have a stable JSON Schema representation");
342
- case "isFunction":
343
- return unsupported(descriptor, context, "functions cannot be represented in JSON Schema");
344
- case "isDefined":
345
- return {};
346
- case "isMap":
347
- return unsupported(descriptor, context, "Map instances do not have a stable JSON Schema representation");
348
- case "isNaN":
349
- return unsupported(descriptor, context, "NaN cannot be represented in JSON Schema");
350
- case "hasValue":
351
- case "multipleOf":
352
- return numberSchema(descriptor, context);
353
- case "exact":
354
- return exactSchema(descriptor, context);
355
- case "hasSize":
356
- return unsupported(descriptor, context, "Map and Set sizes do not have a stable JSON Schema representation");
357
- case "oneOf":
358
- return enumSchema(descriptor, context);
359
- case "hasLength":
360
- return lengthSchema(descriptor, context);
361
- case "isDate":
362
- return unsupported(descriptor, context, "Date instances do not have a stable JSON Schema representation");
363
- case "isSet":
364
- return unsupported(descriptor, context, "Set instances do not have a stable JSON Schema representation");
365
- case "isSymbol":
366
- return unsupported(descriptor, context, "symbols cannot be represented in JSON Schema");
367
- default:
368
- return unsupported(descriptor, context, `unsupported assertion "${descriptor.name}"`);
369
- }
238
+ var exportShapeRules = (descriptor, context) => {
239
+ if (descriptor.rules.length === 0 || context.mode === "bestEffort") return;
240
+ const [rule] = descriptor.rules;
241
+ throw new JsonSchemaExportError(`Cannot export shape at ${formatPath([...context.path, "rules"])}: object-level rule "${rule.kind}" has no JSON Schema mapping`, {
242
+ descriptor,
243
+ path: [...context.path, "rules"],
244
+ reason: `object-level rule "${rule.kind}" has no JSON Schema mapping`
245
+ });
370
246
  };
371
- const exportDiscriminatedUnion = (descriptor, context) => {
372
- if (typeof descriptor.key === "symbol") {
373
- return unsupported(descriptor, context, "symbol discriminators cannot be represented in JSON Schema");
374
- }
375
- const key = String(descriptor.key);
376
- const variants = descriptor.variants;
377
- const branches = Reflect.ownKeys(variants).map((variantKey) => {
378
- if (typeof variantKey === "symbol") {
379
- return unsupported(descriptor, withPath(context, variantKey), "symbol discriminator values cannot be represented in JSON Schema");
380
- }
381
- const variant = variants[variantKey];
382
- const branch = exportDescriptor(variant, withPath(context, variantKey));
383
- return {
384
- allOf: [{
385
- type: "object",
386
- properties: {
387
- [key]: {
388
- const: variantKey
389
- }
390
- },
391
- required: [key]
392
- }, branch]
393
- };
394
- });
395
- return {
396
- oneOf: branches
397
- };
247
+ var exportAssertion = (descriptor, context) => {
248
+ switch (descriptor.name) {
249
+ case "isString": return { type: "string" };
250
+ case "isNumber": return { type: "number" };
251
+ case "isFiniteNumber": return { type: "number" };
252
+ case "isInteger":
253
+ case "isSafeInteger": return numberSchema(descriptor, context);
254
+ case "isBoolean": return { type: "boolean" };
255
+ case "isBigInt": return unsupported(descriptor, context, "bigint values cannot be represented in JSON Schema");
256
+ case "isBlob": return unsupported(descriptor, context, "Blob instances do not have a stable JSON Schema representation");
257
+ case "isNull": return { type: "null" };
258
+ case "isEmail": return {
259
+ type: "string",
260
+ format: "email"
261
+ };
262
+ case "hasPattern":
263
+ case "startsWith":
264
+ case "endsWith": return stringSchema(descriptor, context);
265
+ case "isFile": return unsupported(descriptor, context, "File instances do not have a stable JSON Schema representation");
266
+ case "isFunction": return unsupported(descriptor, context, "functions cannot be represented in JSON Schema");
267
+ case "isDefined": return {};
268
+ case "isMap": return unsupported(descriptor, context, "Map instances do not have a stable JSON Schema representation");
269
+ case "isNaN": return unsupported(descriptor, context, "NaN cannot be represented in JSON Schema");
270
+ case "hasValue":
271
+ case "multipleOf": return numberSchema(descriptor, context);
272
+ case "exact": return exactSchema(descriptor, context);
273
+ case "hasSize": return unsupported(descriptor, context, "Map and Set sizes do not have a stable JSON Schema representation");
274
+ case "oneOf": return enumSchema(descriptor, context);
275
+ case "hasLength": return lengthSchema(descriptor, context);
276
+ case "isDate": return unsupported(descriptor, context, "Date instances do not have a stable JSON Schema representation");
277
+ case "isValidDate": return unsupported(descriptor, context, "Date instances do not have a stable JSON Schema representation");
278
+ case "isError": return unsupported(descriptor, context, "Error instances do not have a stable JSON Schema representation");
279
+ case "isPromiseLike": return unsupported(descriptor, context, "promise-like values do not have a stable JSON Schema representation");
280
+ case "isRegExp": return unsupported(descriptor, context, "regular expressions do not have a stable JSON Schema representation");
281
+ case "isSet": return unsupported(descriptor, context, "Set instances do not have a stable JSON Schema representation");
282
+ case "isSymbol": return unsupported(descriptor, context, "symbols cannot be represented in JSON Schema");
283
+ default: return unsupported(descriptor, context, `unsupported assertion "${descriptor.name}"`);
284
+ }
398
285
  };
399
- const exportRulesAsComment = (rules, schema) => ({
400
- ...schema,
401
- $comment: `Dropped ${rules.length} object rule(s) during best-effort JSON Schema export.`
286
+ var exportDiscriminatedUnion = (descriptor, context) => {
287
+ if (typeof descriptor.key === "symbol") return unsupported(descriptor, context, "symbol discriminators cannot be represented in JSON Schema");
288
+ const key = String(descriptor.key);
289
+ const variants = descriptor.variants;
290
+ return { oneOf: Reflect.ownKeys(variants).map((variantKey) => {
291
+ if (typeof variantKey === "symbol") return unsupported(descriptor, withPath(context, variantKey), "symbol discriminator values cannot be represented in JSON Schema");
292
+ const variant = variants[variantKey];
293
+ const branch = exportDescriptor(variant, withPath(context, variantKey));
294
+ return { allOf: [{
295
+ type: "object",
296
+ properties: { [key]: { const: variantKey } },
297
+ required: [key]
298
+ }, branch] };
299
+ }) };
300
+ };
301
+ var exportRulesAsComment = (rules, schema) => ({
302
+ ...schema,
303
+ $comment: `Dropped ${rules.length} object rule(s) during best-effort JSON Schema export.`
402
304
  });
403
- const exportDescriptor = (descriptor, context) => {
404
- let schema;
405
- switch (descriptor.kind) {
406
- case "assertion":
407
- schema = exportAssertion(descriptor, context);
408
- break;
409
- case "allOf": {
410
- const allOfDescriptor = descriptor;
411
- schema = {
412
- allOf: allOfDescriptor.constraints.map((child, index) => exportDescriptor(child, withPath(context, index)))
413
- };
414
- break;
415
- }
416
- case "optional": {
417
- const wrapperDescriptor = descriptor;
418
- schema = exportDescriptor(wrapperDescriptor.child, withPath(context, "optional"));
419
- break;
420
- }
421
- case "nullable":
422
- case "nullish": {
423
- const wrapperDescriptor = descriptor;
424
- schema = mergeNullable(exportDescriptor(wrapperDescriptor.child, withPath(context, descriptor.kind)));
425
- break;
426
- }
427
- case "each": {
428
- const eachDescriptor = descriptor;
429
- schema = {
430
- type: "array",
431
- items: exportDescriptor(eachDescriptor.item, withPath(context, "items"))
432
- };
433
- break;
434
- }
435
- case "tuple": {
436
- const tupleDescriptor = descriptor;
437
- schema = {
438
- type: "array",
439
- prefixItems: tupleDescriptor.items.map((item, index) => exportDescriptor(item, withPath(context, index))),
440
- minItems: tupleDescriptor.items.length,
441
- maxItems: tupleDescriptor.items.length
442
- };
443
- break;
444
- }
445
- case "union": {
446
- const unionDescriptor = descriptor;
447
- schema = {
448
- anyOf: unionDescriptor.branches.map((branch, index) => exportDescriptor(branch, withPath(context, index)))
449
- };
450
- break;
451
- }
452
- case "record": {
453
- const recordDescriptor = descriptor;
454
- schema = {
455
- type: "object",
456
- additionalProperties: exportDescriptor(recordDescriptor.values, withPath(context, "additionalProperties"))
457
- };
458
- break;
459
- }
460
- case "shape": {
461
- const shapeDescriptor = descriptor;
462
- const fields = shapeDescriptor.fields;
463
- const rules = shapeDescriptor.rules;
464
- exportShapeRules(shapeDescriptor, context);
465
- const properties = {};
466
- const required = [];
467
- Reflect.ownKeys(fields).forEach((key) => {
468
- const childContext = withPath(context, key);
469
- const propertyName = toPropertyName(key, shapeDescriptor, childContext);
470
- if (typeof propertyName !== "string") {
471
- return;
472
- }
473
- const child = fields[key];
474
- properties[propertyName] = exportDescriptor(child, childContext);
475
- if (!acceptsUndefined(child)) {
476
- required.push(propertyName);
477
- }
478
- });
479
- const shapeSchema = {
480
- type: "object",
481
- properties,
482
- additionalProperties: shapeDescriptor.unknownKeys === "strict" ? false : true
483
- };
484
- if (required.length > 0) {
485
- setSchemaProperty(shapeSchema, "required", required);
486
- }
487
- schema = shapeSchema;
488
- if (rules.length > 0 && context.mode === "bestEffort") {
489
- schema = exportRulesAsComment(rules, schema);
490
- }
491
- break;
492
- }
493
- case "discriminatedUnion":
494
- schema = exportDiscriminatedUnion(descriptor, context);
495
- break;
496
- case "validator":
497
- schema = unsupported(descriptor, context, "custom validators need a supported public descriptor");
498
- break;
499
- default:
500
- schema = unsupported(descriptor, context, `unsupported descriptor kind "${descriptor.kind}"`);
501
- break;
502
- }
503
- return applyMetadata(schema, descriptor.metadata);
305
+ var exportDescriptor = (descriptor, context) => {
306
+ let schema;
307
+ switch (descriptor.kind) {
308
+ case "assertion":
309
+ schema = exportAssertion(descriptor, context);
310
+ break;
311
+ case "allOf":
312
+ schema = { allOf: descriptor.constraints.map((child, index) => exportDescriptor(child, withPath(context, index))) };
313
+ break;
314
+ case "optional":
315
+ schema = exportDescriptor(descriptor.child, withPath(context, "optional"));
316
+ break;
317
+ case "nullable":
318
+ case "nullish":
319
+ schema = mergeNullable(exportDescriptor(descriptor.child, withPath(context, descriptor.kind)));
320
+ break;
321
+ case "each":
322
+ schema = {
323
+ type: "array",
324
+ items: exportDescriptor(descriptor.item, withPath(context, "items"))
325
+ };
326
+ break;
327
+ case "tuple": {
328
+ const tupleDescriptor = descriptor;
329
+ schema = {
330
+ type: "array",
331
+ prefixItems: tupleDescriptor.items.map((item, index) => exportDescriptor(item, withPath(context, index))),
332
+ minItems: tupleDescriptor.items.length,
333
+ maxItems: tupleDescriptor.items.length
334
+ };
335
+ break;
336
+ }
337
+ case "union":
338
+ schema = { anyOf: descriptor.branches.map((branch, index) => exportDescriptor(branch, withPath(context, index))) };
339
+ break;
340
+ case "record":
341
+ schema = {
342
+ type: "object",
343
+ additionalProperties: exportDescriptor(descriptor.values, withPath(context, "additionalProperties"))
344
+ };
345
+ break;
346
+ case "shape": {
347
+ const shapeDescriptor = descriptor;
348
+ const fields = shapeDescriptor.fields;
349
+ const rules = shapeDescriptor.rules;
350
+ exportShapeRules(shapeDescriptor, context);
351
+ const properties = {};
352
+ const required = [];
353
+ Reflect.ownKeys(fields).forEach((key) => {
354
+ const childContext = withPath(context, key);
355
+ const propertyName = toPropertyName(key, shapeDescriptor, childContext);
356
+ if (typeof propertyName !== "string") return;
357
+ const child = fields[key];
358
+ properties[propertyName] = exportDescriptor(child, childContext);
359
+ if (!acceptsUndefined(child)) required.push(propertyName);
360
+ });
361
+ const shapeSchema = {
362
+ type: "object",
363
+ properties,
364
+ additionalProperties: shapeDescriptor.unknownKeys === "strict" ? false : true
365
+ };
366
+ if (required.length > 0) setSchemaProperty(shapeSchema, "required", required);
367
+ schema = shapeSchema;
368
+ if (rules.length > 0 && context.mode === "bestEffort") schema = exportRulesAsComment(rules, schema);
369
+ break;
370
+ }
371
+ case "discriminatedUnion":
372
+ schema = exportDiscriminatedUnion(descriptor, context);
373
+ break;
374
+ case "validator":
375
+ schema = unsupported(descriptor, context, "custom validators need a supported public descriptor");
376
+ break;
377
+ default: schema = unsupported(descriptor, context, `unsupported descriptor kind "${descriptor.kind}"`);
378
+ }
379
+ return applyMetadata(schema, descriptor.metadata);
504
380
  };
505
- const toJsonSchema = (constraints, options = {}) => {
506
- const descriptor = metadata.describeConstraints(constraints);
507
- const context = {
508
- mode: options.mode ?? "bestEffort",
509
- path: []
510
- };
511
- return exportDescriptor(descriptor, context);
381
+ var toJsonSchema = (constraints, options = {}) => {
382
+ return exportDescriptor(require_metadata.describeConstraints(constraints), {
383
+ mode: options.mode ?? "bestEffort",
384
+ path: []
385
+ });
512
386
  };
387
+ //#endregion
513
388
  exports.JsonSchemaExportError = JsonSchemaExportError;
514
389
  exports.toJsonSchema = toJsonSchema;