@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.
- package/CHANGELOG.md +29 -1
- package/README.md +12 -2
- package/dist/assert.cjs +99 -63
- package/dist/assert.d.cts +37 -0
- package/dist/assert.d.mts +37 -0
- package/dist/assert.d.ts +24 -3
- package/dist/assert.mjs +94 -64
- package/dist/assertions.cjs +283 -178
- package/dist/assertions.d.cts +56 -0
- package/dist/assertions.d.mts +56 -0
- package/dist/assertions.d.ts +43 -45
- package/dist/assertions.mjs +274 -201
- package/dist/checkers.cjs +23 -0
- package/dist/checkers.d.cts +8 -0
- package/dist/checkers.d.mts +8 -0
- package/dist/checkers.d.ts +1 -1
- package/dist/checkers.mjs +16 -0
- package/dist/combinators.cjs +305 -304
- package/dist/combinators.d.cts +16 -0
- package/dist/combinators.d.mts +16 -0
- package/dist/combinators.d.ts +15 -16
- package/dist/combinators.mjs +305 -314
- package/dist/constraints.cjs +23 -0
- package/dist/constraints.d.cts +4 -0
- package/dist/constraints.d.mts +4 -0
- package/dist/constraints.d.ts +2 -2
- package/dist/constraints.mjs +21 -0
- package/dist/extractors.cjs +6 -0
- package/dist/extractors.d.cts +2 -0
- package/dist/extractors.d.mts +2 -0
- package/dist/extractors.mjs +5 -0
- package/dist/index.cjs +140 -219
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.mjs +93 -222
- package/dist/json-schema.cjs +364 -489
- package/dist/json-schema.d.cts +14 -0
- package/dist/json-schema.d.mts +14 -0
- package/dist/json-schema.d.ts +3 -3
- package/dist/json-schema.mjs +365 -492
- package/dist/metadata.cjs +98 -7
- package/dist/metadata.d.cts +8 -0
- package/dist/metadata.d.mts +8 -0
- package/dist/metadata.d.ts +2 -2
- package/dist/metadata.mjs +93 -7
- package/dist/predicates.cjs +98 -41
- package/dist/predicates.d.cts +77 -0
- package/dist/predicates.d.mts +77 -0
- package/dist/predicates.d.ts +25 -4
- package/dist/predicates.mjs +92 -66
- package/dist/types/index.d.cts +984 -0
- package/dist/types/index.d.mts +984 -0
- package/dist/types/index.d.ts +984 -0
- package/dist/types/json-schema.d.cts +75 -0
- package/dist/types/json-schema.d.mts +75 -0
- package/dist/types/json-schema.d.ts +75 -0
- package/dist/violations.cjs +81 -0
- package/dist/violations.d.cts +29 -0
- package/dist/violations.d.mts +29 -0
- package/dist/violations.d.ts +1 -1
- package/dist/violations.mjs +80 -0
- package/docs/RELEASING.md +66 -0
- package/docs/en/00-index.md +2 -0
- package/docs/en/01-shape-api.md +35 -10
- package/docs/en/02-metadata-and-introspection.md +2 -1
- package/docs/en/03-violations.md +1 -1
- package/docs/en/04-json-schema-export.md +5 -0
- package/docs/en/05-public-api.md +35 -3
- package/docs/en/06-common-recipes.md +2 -1
- package/docs/en/07-ai-reference.md +5 -2
- package/docs/en/08-violation-code-types.md +28 -2
- package/docs/en/09-migration.md +75 -0
- package/docs/ru/00-index.md +2 -0
- package/docs/ru/01-shape-api.md +35 -10
- package/docs/ru/02-metadata-and-introspection.md +2 -1
- package/docs/ru/03-violations.md +1 -1
- package/docs/ru/04-json-schema-export.md +5 -0
- package/docs/ru/05-public-api.md +35 -3
- package/docs/ru/06-common-recipes.md +2 -1
- package/docs/ru/07-ai-reference.md +5 -2
- package/docs/ru/08-violation-code-types.md +28 -2
- package/docs/ru/09-migration.md +76 -0
- package/docs/ru/README.md +10 -2
- package/package.json +63 -37
- package/types/index.d.ts +275 -115
- package/dist/metadata.cjs.js +0 -130
- package/dist/metadata.es.js +0 -131
package/dist/json-schema.cjs
CHANGED
|
@@ -1,514 +1,389 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
return String(key);
|
|
80
|
+
var mergeNullable = (schema) => {
|
|
81
|
+
if (isEmptySchema(schema)) return schema;
|
|
82
|
+
return { anyOf: [schema, { type: "null" }] };
|
|
108
83
|
};
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
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
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
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
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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;
|