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