@ivlev-dev/silent-validation 0.0.0-stage → 0.13.0

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 (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +573 -2
  3. package/dist/engine.d.ts +15 -0
  4. package/dist/engine.d.ts.map +1 -0
  5. package/dist/engine.js +967 -0
  6. package/dist/engine.js.map +1 -0
  7. package/dist/index.d.ts +15 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +13 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/messages.d.ts +17 -0
  12. package/dist/messages.d.ts.map +1 -0
  13. package/dist/messages.js +508 -0
  14. package/dist/messages.js.map +1 -0
  15. package/dist/model.d.ts +3 -0
  16. package/dist/model.d.ts.map +1 -0
  17. package/dist/model.js +3 -0
  18. package/dist/model.js.map +1 -0
  19. package/dist/modifiers.d.ts +7 -0
  20. package/dist/modifiers.d.ts.map +1 -0
  21. package/dist/modifiers.js +58 -0
  22. package/dist/modifiers.js.map +1 -0
  23. package/dist/property.d.ts +3 -0
  24. package/dist/property.d.ts.map +1 -0
  25. package/dist/property.js +42 -0
  26. package/dist/property.js.map +1 -0
  27. package/dist/registry.d.ts +11 -0
  28. package/dist/registry.d.ts.map +1 -0
  29. package/dist/registry.js +187 -0
  30. package/dist/registry.js.map +1 -0
  31. package/dist/rule.d.ts +7 -0
  32. package/dist/rule.d.ts.map +1 -0
  33. package/dist/rule.js +34 -0
  34. package/dist/rule.js.map +1 -0
  35. package/dist/rules/collections.d.ts +54 -0
  36. package/dist/rules/collections.d.ts.map +1 -0
  37. package/dist/rules/collections.js +116 -0
  38. package/dist/rules/collections.js.map +1 -0
  39. package/dist/rules/comparison.d.ts +36 -0
  40. package/dist/rules/comparison.d.ts.map +1 -0
  41. package/dist/rules/comparison.js +45 -0
  42. package/dist/rules/comparison.js.map +1 -0
  43. package/dist/rules/custom.d.ts +10 -0
  44. package/dist/rules/custom.d.ts.map +1 -0
  45. package/dist/rules/custom.js +12 -0
  46. package/dist/rules/custom.js.map +1 -0
  47. package/dist/rules/numbers.d.ts +61 -0
  48. package/dist/rules/numbers.d.ts.map +1 -0
  49. package/dist/rules/numbers.js +90 -0
  50. package/dist/rules/numbers.js.map +1 -0
  51. package/dist/rules/presence.d.ts +34 -0
  52. package/dist/rules/presence.d.ts.map +1 -0
  53. package/dist/rules/presence.js +66 -0
  54. package/dist/rules/presence.js.map +1 -0
  55. package/dist/rules/strings.d.ts +102 -0
  56. package/dist/rules/strings.d.ts.map +1 -0
  57. package/dist/rules/strings.js +234 -0
  58. package/dist/rules/strings.js.map +1 -0
  59. package/dist/rules/types.d.ts +76 -0
  60. package/dist/rules/types.d.ts.map +1 -0
  61. package/dist/rules/types.js +128 -0
  62. package/dist/rules/types.js.map +1 -0
  63. package/dist/schema.d.ts +11 -0
  64. package/dist/schema.d.ts.map +1 -0
  65. package/dist/schema.js +147 -0
  66. package/dist/schema.js.map +1 -0
  67. package/dist/thenable.d.ts +4 -0
  68. package/dist/thenable.d.ts.map +1 -0
  69. package/dist/thenable.js +16 -0
  70. package/dist/thenable.js.map +1 -0
  71. package/dist/type-validation.d.ts +44 -0
  72. package/dist/type-validation.d.ts.map +1 -0
  73. package/dist/type-validation.js +850 -0
  74. package/dist/type-validation.js.map +1 -0
  75. package/dist/types.d.ts +113 -0
  76. package/dist/types.d.ts.map +1 -0
  77. package/dist/types.js +22 -0
  78. package/dist/types.js.map +1 -0
  79. package/package.json +49 -3
@@ -0,0 +1,850 @@
1
+ import { DEFAULT_LOCALE, resolveMessage } from "./messages.js";
2
+ import { constructorOf, readFieldValue } from "./property.js";
3
+ import { readClassSchema, readInstanceSchema } from "./schema.js";
4
+ import { isThenable, suppress, suppressThenable } from "./thenable.js";
5
+ export const MAX_DEPTH = 256;
6
+ const PRIMITIVE_NAMES = {
7
+ string: "string",
8
+ number: "number",
9
+ boolean: "boolean",
10
+ bigint: "bigint",
11
+ symbol: "symbol",
12
+ object: "object",
13
+ function: "function",
14
+ undefined: "undefined",
15
+ null: "null",
16
+ void: "void",
17
+ never: "never",
18
+ date: "Date",
19
+ regexp: "RegExp",
20
+ map: "Map",
21
+ set: "Set",
22
+ weakmap: "WeakMap",
23
+ weakset: "WeakSet",
24
+ promise: "Promise",
25
+ error: "Error",
26
+ arraybuffer: "ArrayBuffer",
27
+ sharedarraybuffer: "SharedArrayBuffer",
28
+ dataview: "DataView",
29
+ url: "URL",
30
+ urlsearchparams: "URLSearchParams",
31
+ iterable: "Iterable",
32
+ asynciterable: "AsyncIterable",
33
+ iterator: "Iterator",
34
+ asynciterator: "AsyncIterator",
35
+ int8array: "Int8Array",
36
+ uint8array: "Uint8Array",
37
+ uint8clampedarray: "Uint8ClampedArray",
38
+ int16array: "Int16Array",
39
+ uint16array: "Uint16Array",
40
+ int32array: "Int32Array",
41
+ uint32array: "Uint32Array",
42
+ float32array: "Float32Array",
43
+ float64array: "Float64Array",
44
+ bigint64array: "BigInt64Array",
45
+ biguint64array: "BigUint64Array",
46
+ };
47
+ const TYPED_ARRAY_GLOBALS = {
48
+ int8array: "Int8Array",
49
+ uint8array: "Uint8Array",
50
+ uint8clampedarray: "Uint8ClampedArray",
51
+ int16array: "Int16Array",
52
+ uint16array: "Uint16Array",
53
+ int32array: "Int32Array",
54
+ uint32array: "Uint32Array",
55
+ float32array: "Float32Array",
56
+ float64array: "Float64Array",
57
+ bigint64array: "BigInt64Array",
58
+ biguint64array: "BigUint64Array",
59
+ };
60
+ function globalCtor(name) {
61
+ return globalThis[name];
62
+ }
63
+ function isInstanceOf(value, name) {
64
+ const ctor = globalCtor(name);
65
+ if (typeof ctor !== "function") {
66
+ return false;
67
+ }
68
+ return value instanceof ctor;
69
+ }
70
+ function isInstanceOfCtor(value, ctor) {
71
+ try {
72
+ return value instanceof ctor;
73
+ }
74
+ catch {
75
+ return false;
76
+ }
77
+ }
78
+ function isPlainObject(value) {
79
+ if (typeof value !== "object" || value === null) {
80
+ return false;
81
+ }
82
+ const prototype = Object.getPrototypeOf(value);
83
+ return prototype === Object.prototype || prototype === null;
84
+ }
85
+ function isIterable(value) {
86
+ if (value === null || value === undefined) {
87
+ return false;
88
+ }
89
+ return typeof value[Symbol.iterator] === "function";
90
+ }
91
+ function isAsyncIterable(value) {
92
+ if (value === null || (typeof value !== "object" && typeof value !== "function")) {
93
+ return false;
94
+ }
95
+ return typeof value[Symbol.asyncIterator] === "function";
96
+ }
97
+ function isIteratorLike(value) {
98
+ if (value === null || (typeof value !== "object" && typeof value !== "function")) {
99
+ return false;
100
+ }
101
+ return typeof value.next === "function";
102
+ }
103
+ function isAsyncIterator(value) {
104
+ if (!isIteratorLike(value)) {
105
+ return false;
106
+ }
107
+ if (typeof value[Symbol.asyncIterator] === "function") {
108
+ return true;
109
+ }
110
+ return typeof value[Symbol.iterator] !== "function";
111
+ }
112
+ function isValidDate(value) {
113
+ return isInstanceOf(value, "Date") && Number.isFinite(value.getTime());
114
+ }
115
+ function matchesPrimitive(value, schema) {
116
+ switch (schema) {
117
+ case "string":
118
+ return typeof value === "string";
119
+ case "number":
120
+ return typeof value === "number" && Number.isFinite(value);
121
+ case "boolean":
122
+ return typeof value === "boolean";
123
+ case "bigint":
124
+ return typeof value === "bigint";
125
+ case "symbol":
126
+ return typeof value === "symbol";
127
+ case "object":
128
+ return typeof value === "object" && value !== null;
129
+ case "function":
130
+ return typeof value === "function";
131
+ case "undefined":
132
+ case "void":
133
+ return value === undefined;
134
+ case "null":
135
+ return value === null;
136
+ case "never":
137
+ return false;
138
+ case "date":
139
+ return isValidDate(value);
140
+ case "regexp":
141
+ return isInstanceOf(value, "RegExp");
142
+ case "map":
143
+ return isInstanceOf(value, "Map");
144
+ case "set":
145
+ return isInstanceOf(value, "Set");
146
+ case "weakmap":
147
+ return isInstanceOf(value, "WeakMap");
148
+ case "weakset":
149
+ return isInstanceOf(value, "WeakSet");
150
+ case "promise":
151
+ return isThenable(value);
152
+ case "error":
153
+ return isInstanceOf(value, "Error");
154
+ case "arraybuffer":
155
+ return isInstanceOf(value, "ArrayBuffer");
156
+ case "sharedarraybuffer":
157
+ return isInstanceOf(value, "SharedArrayBuffer");
158
+ case "dataview":
159
+ return isInstanceOf(value, "DataView");
160
+ case "url":
161
+ return isInstanceOf(value, "URL");
162
+ case "urlsearchparams":
163
+ return isInstanceOf(value, "URLSearchParams");
164
+ case "iterable":
165
+ return isIterable(value);
166
+ case "asynciterable":
167
+ return isAsyncIterable(value);
168
+ case "iterator":
169
+ return isIteratorLike(value);
170
+ case "asynciterator":
171
+ return isAsyncIterator(value);
172
+ default: {
173
+ const globalName = TYPED_ARRAY_GLOBALS[schema];
174
+ return globalName !== undefined && isInstanceOf(value, globalName);
175
+ }
176
+ }
177
+ }
178
+ function isFastPrimitiveName(name) {
179
+ switch (name) {
180
+ case "string":
181
+ case "number":
182
+ case "boolean":
183
+ case "bigint":
184
+ case "symbol":
185
+ case "object":
186
+ case "function":
187
+ return true;
188
+ default:
189
+ return false;
190
+ }
191
+ }
192
+ export function validateFastPrimitive(value, schema, context) {
193
+ if (typeof schema !== "string" || !isFastPrimitiveName(schema)) {
194
+ return undefined;
195
+ }
196
+ if (matchesPrimitive(value, schema)) {
197
+ return null;
198
+ }
199
+ return createError(value, schema, {
200
+ instance: context.instance,
201
+ field: context.field,
202
+ label: context.label,
203
+ path: context.path,
204
+ config: context.config,
205
+ labels: context.labels,
206
+ locale: context.config.locale ?? DEFAULT_LOCALE,
207
+ index: undefined,
208
+ });
209
+ }
210
+ function formatLiteral(value) {
211
+ return typeof value === "string" ? JSON.stringify(value) : String(value);
212
+ }
213
+ function ctorName(getCtor) {
214
+ try {
215
+ const produced = getCtor();
216
+ if (isThenable(produced)) {
217
+ suppress(produced);
218
+ return "object";
219
+ }
220
+ return typeof produced === "function" && produced.name.length > 0
221
+ ? produced.name
222
+ : "object";
223
+ }
224
+ catch {
225
+ return "object";
226
+ }
227
+ }
228
+ function tupleName(items, rest, depth) {
229
+ const parts = items.map((item) => (item === null ? "?" : typeNameAt(item, depth + 1)));
230
+ if (rest !== undefined) {
231
+ parts.push(`...${typeNameAt(rest, depth + 1)}[]`);
232
+ }
233
+ return `[${parts.join(", ")}]`;
234
+ }
235
+ const MAX_NAME_DEPTH = 8;
236
+ function typeNameAt(schema, depth) {
237
+ if (typeof schema === "string") {
238
+ return PRIMITIVE_NAMES[schema] ?? schema;
239
+ }
240
+ if (!Array.isArray(schema)) {
241
+ return "unknown";
242
+ }
243
+ switch (schema[0]) {
244
+ case "array":
245
+ return `Array<${typeNameAt(schema[1], depth + 1)}>`;
246
+ case "tuple":
247
+ return tupleName(schema[1], schema[2], depth);
248
+ case "union":
249
+ return schema[1].map((branch) => typeNameAt(branch, depth + 1)).join(" | ");
250
+ case "intersection":
251
+ return schema[1].map((branch) => typeNameAt(branch, depth + 1)).join(" & ");
252
+ case "literal":
253
+ return formatLiteral(schema[1]);
254
+ case "enum":
255
+ return schema[1].map((item) => formatLiteral(item)).join(" | ");
256
+ case "class":
257
+ return ctorName(schema[1]);
258
+ case "lazy": {
259
+ if (depth >= MAX_NAME_DEPTH) {
260
+ return "object";
261
+ }
262
+ try {
263
+ const produced = schema[1]();
264
+ if (isThenable(produced)) {
265
+ suppress(produced);
266
+ return "unknown";
267
+ }
268
+ if (typeof produced !== "string" && !Array.isArray(produced)) {
269
+ return "unknown";
270
+ }
271
+ return typeNameAt(produced, depth + 1);
272
+ }
273
+ catch {
274
+ return "object";
275
+ }
276
+ }
277
+ case "object":
278
+ return "object";
279
+ case "record":
280
+ return `Record<string, ${typeNameAt(schema[1], depth + 1)}>`;
281
+ case "mapof":
282
+ return `Map<${typeNameAt(schema[1], depth + 1)}, ${typeNameAt(schema[2], depth + 1)}>`;
283
+ case "setof":
284
+ return `Set<${typeNameAt(schema[1], depth + 1)}>`;
285
+ case "optional":
286
+ case "nullable":
287
+ return typeNameAt(schema[1], depth + 1);
288
+ default:
289
+ return "unknown";
290
+ }
291
+ }
292
+ export function typeName(schema) {
293
+ return typeNameAt(schema, 0);
294
+ }
295
+ function labelFor(ctx) {
296
+ const labels = ctx.labels;
297
+ if (labels !== undefined && Object.hasOwn(labels, ctx.path)) {
298
+ const exact = labels[ctx.path];
299
+ if (typeof exact === "string") {
300
+ return exact;
301
+ }
302
+ }
303
+ return ctx.label;
304
+ }
305
+ function buildError(value, params, ctx, key = "validation.type") {
306
+ const label = labelFor(ctx);
307
+ const context = {
308
+ rule: "type",
309
+ key,
310
+ field: ctx.field,
311
+ label,
312
+ value,
313
+ params,
314
+ locale: ctx.locale,
315
+ instance: ctx.instance,
316
+ path: ctx.path,
317
+ ...(ctx.index === undefined ? {} : { index: ctx.index }),
318
+ };
319
+ return {
320
+ field: ctx.field,
321
+ rule: "type",
322
+ message: resolveMessage(context, ctx.config),
323
+ params,
324
+ value,
325
+ path: ctx.path,
326
+ label,
327
+ ...(ctx.index === undefined ? {} : { index: ctx.index }),
328
+ };
329
+ }
330
+ function createError(value, schema, ctx, name) {
331
+ const params = {
332
+ type: name ?? typeName(schema),
333
+ };
334
+ return buildError(value, params, ctx);
335
+ }
336
+ function depthError(value, ctx) {
337
+ return buildError(value, { type: "depth" }, ctx, "validation.depth");
338
+ }
339
+ function appendErrors(target, source) {
340
+ for (let index = 0; index < source.length; index += 1) {
341
+ const error = source[index];
342
+ if (error !== undefined) {
343
+ target.push(error);
344
+ }
345
+ }
346
+ }
347
+ const PATH_IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
348
+ function childPath(parent, key) {
349
+ return PATH_IDENTIFIER.test(key)
350
+ ? `${parent}.${key}`
351
+ : `${parent}[${JSON.stringify(key)}]`;
352
+ }
353
+ function mapKeyLabel(key, index) {
354
+ switch (typeof key) {
355
+ case "string":
356
+ return key;
357
+ case "number":
358
+ case "bigint":
359
+ case "boolean":
360
+ case "symbol":
361
+ case "undefined":
362
+ return String(key);
363
+ default:
364
+ return `#${index}`;
365
+ }
366
+ }
367
+ function childContext(ctx, path, label, index) {
368
+ return {
369
+ ...ctx,
370
+ path,
371
+ label: label ?? ctx.label,
372
+ index,
373
+ };
374
+ }
375
+ function cycleHit(runtime) {
376
+ runtime.cycleHits = (runtime.cycleHits ?? 0) + 1;
377
+ }
378
+ function enter(value, ctx) {
379
+ const visiting = ctx.runtime.visiting;
380
+ if (visiting.has(value)) {
381
+ cycleHit(ctx.runtime);
382
+ return false;
383
+ }
384
+ visiting.add(value);
385
+ return true;
386
+ }
387
+ function isLazyActive(runtime, schema, value) {
388
+ if (!Array.isArray(schema) || schema[0] !== "lazy") {
389
+ return false;
390
+ }
391
+ const active = runtime.lazyActive?.get(schema);
392
+ return active !== undefined && active.has(value);
393
+ }
394
+ function lookupSuccess(runtime, value, schema, depth) {
395
+ const stored = runtime.successCache?.get(value)?.get(schema);
396
+ return stored !== undefined && depth <= stored;
397
+ }
398
+ function recordSuccess(runtime, value, schema, depth) {
399
+ const cache = runtime.successCache ??= new WeakMap();
400
+ let entry = cache.get(value);
401
+ if (entry === undefined) {
402
+ entry = new Map();
403
+ cache.set(value, entry);
404
+ }
405
+ const stored = entry.get(schema);
406
+ if (stored === undefined || depth < stored) {
407
+ entry.set(schema, depth);
408
+ }
409
+ }
410
+ function leave(value, ctx) {
411
+ ctx.runtime.visiting.delete(value);
412
+ }
413
+ function classFields(value, ctor, runtime) {
414
+ const actual = constructorOf(value);
415
+ if (actual !== null) {
416
+ const cache = runtime.instanceFields ??= new WeakMap();
417
+ const cached = cache.get(actual);
418
+ const instanceSchema = cached !== undefined
419
+ ? cached
420
+ : readInstanceSchema(value);
421
+ if (cached === undefined) {
422
+ cache.set(actual, instanceSchema);
423
+ }
424
+ if (instanceSchema !== null) {
425
+ return instanceSchema;
426
+ }
427
+ }
428
+ const own = readClassSchema(ctor);
429
+ return own === null ? null : own.fields;
430
+ }
431
+ function validateFields(owner, fields, ctx, childDepth) {
432
+ const errors = [];
433
+ if (fields === undefined || fields === null) {
434
+ return errors;
435
+ }
436
+ for (const [key, fieldSchema] of Object.entries(fields)) {
437
+ if (fieldSchema === undefined) {
438
+ continue;
439
+ }
440
+ const path = childPath(ctx.path, key);
441
+ appendErrors(errors, validateSchema(readFieldValue(owner, key), fieldSchema, childContext(ctx, path, key, undefined), childDepth));
442
+ if (ctx.stopAtFirst && errors.length > 0) {
443
+ return errors;
444
+ }
445
+ }
446
+ return errors;
447
+ }
448
+ function validateArray(value, itemSchema, ctx, depth) {
449
+ if (!enter(value, ctx)) {
450
+ return [];
451
+ }
452
+ const errors = [];
453
+ try {
454
+ for (let index = 0; index < value.length; index += 1) {
455
+ const path = `${ctx.path}[${index}]`;
456
+ appendErrors(errors, validateSchema(value[index], itemSchema, childContext(ctx, path, undefined, index), depth + 1));
457
+ if (ctx.stopAtFirst && errors.length > 0) {
458
+ return errors;
459
+ }
460
+ }
461
+ }
462
+ finally {
463
+ leave(value, ctx);
464
+ }
465
+ return errors;
466
+ }
467
+ function validateMap(value, keySchema, valueSchema, ctx, depth) {
468
+ if (!enter(value, ctx)) {
469
+ return [];
470
+ }
471
+ const errors = [];
472
+ try {
473
+ let index = 0;
474
+ for (const [key, entryValue] of value) {
475
+ const path = `${ctx.path}[${mapKeyLabel(key, index)}]`;
476
+ appendErrors(errors, validateSchema(entryValue, valueSchema, childContext(ctx, path, undefined, index), depth + 1));
477
+ if (ctx.stopAtFirst && errors.length > 0) {
478
+ return errors;
479
+ }
480
+ appendErrors(errors, validateSchema(key, keySchema, childContext(ctx, `${path}.key`, "key", index), depth + 1));
481
+ if (ctx.stopAtFirst && errors.length > 0) {
482
+ return errors;
483
+ }
484
+ index += 1;
485
+ }
486
+ }
487
+ finally {
488
+ leave(value, ctx);
489
+ }
490
+ return errors;
491
+ }
492
+ function validateSet(value, itemSchema, ctx, depth) {
493
+ if (!enter(value, ctx)) {
494
+ return [];
495
+ }
496
+ const errors = [];
497
+ try {
498
+ let index = 0;
499
+ for (const item of value) {
500
+ const path = `${ctx.path}[${index}]`;
501
+ appendErrors(errors, validateSchema(item, itemSchema, childContext(ctx, path, undefined, index), depth + 1));
502
+ if (ctx.stopAtFirst && errors.length > 0) {
503
+ return errors;
504
+ }
505
+ index += 1;
506
+ }
507
+ }
508
+ finally {
509
+ leave(value, ctx);
510
+ }
511
+ return errors;
512
+ }
513
+ function isOptionalSchema(schema) {
514
+ return Array.isArray(schema) && schema[0] === "optional";
515
+ }
516
+ function validateTuple(value, items, rest, schema, ctx, depth) {
517
+ let required = 0;
518
+ for (const item of items) {
519
+ if (item === null || item === undefined || isOptionalSchema(item)) {
520
+ continue;
521
+ }
522
+ required += 1;
523
+ }
524
+ if (rest === undefined) {
525
+ if (value.length < required || value.length > items.length) {
526
+ return [createError(value, schema, ctx)];
527
+ }
528
+ }
529
+ else if (value.length < required) {
530
+ return [createError(value, schema, ctx)];
531
+ }
532
+ if (!enter(value, ctx)) {
533
+ return [];
534
+ }
535
+ const errors = [];
536
+ try {
537
+ const limit = Math.min(items.length, value.length);
538
+ for (let index = 0; index < limit; index += 1) {
539
+ const itemSchema = items[index];
540
+ if (itemSchema === null || itemSchema === undefined) {
541
+ continue;
542
+ }
543
+ const path = `${ctx.path}[${index}]`;
544
+ appendErrors(errors, validateSchema(value[index], itemSchema, childContext(ctx, path, undefined, index), depth + 1));
545
+ if (ctx.stopAtFirst && errors.length > 0) {
546
+ return errors;
547
+ }
548
+ }
549
+ if (rest !== undefined) {
550
+ for (let index = items.length; index < value.length; index += 1) {
551
+ const path = `${ctx.path}[${index}]`;
552
+ appendErrors(errors, validateSchema(value[index], rest, childContext(ctx, path, undefined, index), depth + 1));
553
+ if (ctx.stopAtFirst && errors.length > 0) {
554
+ return errors;
555
+ }
556
+ }
557
+ }
558
+ }
559
+ finally {
560
+ leave(value, ctx);
561
+ }
562
+ return errors;
563
+ }
564
+ function errorKey(error) {
565
+ return `${error.rule}\u0000${error.path}\u0000${String(error.index ?? "")}\u0000${String(error.params["type"] ?? "")}\u0000${error.message}`;
566
+ }
567
+ function dedupeErrors(errors) {
568
+ if (errors.length <= 1) {
569
+ return [...errors];
570
+ }
571
+ const seen = new Set();
572
+ const unique = [];
573
+ for (const error of errors) {
574
+ const key = errorKey(error);
575
+ if (seen.has(key)) {
576
+ continue;
577
+ }
578
+ seen.add(key);
579
+ unique.push(error);
580
+ }
581
+ return unique;
582
+ }
583
+ function resolveLazy(runtime, schema) {
584
+ const cache = runtime.lazyResolutions ??= new WeakMap();
585
+ const cached = cache.get(schema);
586
+ if (cached !== undefined) {
587
+ return cached;
588
+ }
589
+ const getSchema = schema[1];
590
+ let resolution;
591
+ try {
592
+ const produced = getSchema();
593
+ if (isThenable(produced)) {
594
+ suppressThenable(produced);
595
+ resolution = { schema: null, name: "unknown" };
596
+ }
597
+ else if (typeof produced === "string" || Array.isArray(produced)) {
598
+ resolution = { schema: produced, name: "" };
599
+ }
600
+ else {
601
+ resolution = { schema: null, name: "unknown" };
602
+ }
603
+ }
604
+ catch {
605
+ resolution = { schema: null, name: "object" };
606
+ }
607
+ cache.set(schema, resolution);
608
+ return resolution;
609
+ }
610
+ function resolveClass(runtime, schema) {
611
+ const cache = runtime.resolvedClasses ??= new WeakMap();
612
+ const cached = cache.get(schema);
613
+ if (cached !== undefined) {
614
+ return cached;
615
+ }
616
+ const getCtor = schema[1];
617
+ let ctor;
618
+ try {
619
+ const produced = getCtor();
620
+ if (isThenable(produced)) {
621
+ suppressThenable(produced);
622
+ ctor = null;
623
+ }
624
+ else {
625
+ ctor = typeof produced === "function" ? produced : null;
626
+ }
627
+ }
628
+ catch {
629
+ ctor = null;
630
+ }
631
+ cache.set(schema, ctor);
632
+ return ctor;
633
+ }
634
+ function validateClass(value, schema, ctx, depth) {
635
+ const ctor = resolveClass(ctx.runtime, schema);
636
+ if (ctor === null) {
637
+ return [createError(value, schema, ctx, "object")];
638
+ }
639
+ const name = ctor.name.length > 0 ? ctor.name : "object";
640
+ if (!isInstanceOfCtor(value, ctor)) {
641
+ return [createError(value, schema, ctx, name)];
642
+ }
643
+ const target = value;
644
+ if (ctx.nested !== undefined) {
645
+ ctx.nested.push({ instance: target, path: ctx.path });
646
+ return [];
647
+ }
648
+ const fields = classFields(target, ctor, ctx.runtime);
649
+ if (fields === null) {
650
+ return [];
651
+ }
652
+ if (!enter(target, ctx)) {
653
+ return [];
654
+ }
655
+ try {
656
+ return validateFields(target, fields, ctx, depth + 1);
657
+ }
658
+ finally {
659
+ leave(target, ctx);
660
+ }
661
+ }
662
+ function validateSchema(value, schema, ctx, depth) {
663
+ if (depth > MAX_DEPTH) {
664
+ return [depthError(value, ctx)];
665
+ }
666
+ const target = typeof value === "object" && value !== null ? value : undefined;
667
+ const runtime = ctx.runtime;
668
+ if (target !== undefined) {
669
+ // Значение уже в стеке (цикл) или lazy-ветка активна для него —
670
+ // кеш не используем: результат получен через защиту от циклов.
671
+ if (!runtime.visiting.has(target) && !isLazyActive(runtime, schema, value)) {
672
+ if (lookupSuccess(runtime, target, schema, depth)) {
673
+ return [];
674
+ }
675
+ }
676
+ }
677
+ const cyclesBefore = runtime.cycleHits ?? 0;
678
+ const nestedMark = ctx.nested?.length ?? 0;
679
+ const errors = validateSchemaCore(value, schema, ctx, depth);
680
+ if (target !== undefined
681
+ && errors.length === 0
682
+ && (runtime.cycleHits ?? 0) === cyclesBefore
683
+ && (ctx.nested === undefined || ctx.nested.length === nestedMark)) {
684
+ recordSuccess(runtime, target, schema, depth);
685
+ }
686
+ return errors;
687
+ }
688
+ function validateSchemaCore(value, schema, ctx, depth) {
689
+ if (typeof schema === "string") {
690
+ return matchesPrimitive(value, schema) ? [] : [createError(value, schema, ctx)];
691
+ }
692
+ switch (schema[0]) {
693
+ case "optional":
694
+ return value === undefined ? [] : validateSchema(value, schema[1], ctx, depth);
695
+ case "nullable":
696
+ return value === null ? [] : validateSchema(value, schema[1], ctx, depth);
697
+ case "array": {
698
+ const itemSchema = schema[1];
699
+ if (!Array.isArray(value)) {
700
+ return [createError(value, schema, ctx)];
701
+ }
702
+ return validateArray(value, itemSchema, ctx, depth);
703
+ }
704
+ case "tuple": {
705
+ const items = schema[1];
706
+ if (!Array.isArray(value)) {
707
+ return [createError(value, schema, ctx)];
708
+ }
709
+ return validateTuple(value, items, schema[2], schema, ctx, depth);
710
+ }
711
+ case "lazy": {
712
+ const resolution = resolveLazy(ctx.runtime, schema);
713
+ const resolved = resolution.schema;
714
+ if (resolved === null) {
715
+ return [createError(value, schema, ctx, resolution.name)];
716
+ }
717
+ const lazyActive = ctx.runtime.lazyActive ??= new Map();
718
+ const active = lazyActive.get(schema);
719
+ if (active !== undefined && active.has(value)) {
720
+ cycleHit(ctx.runtime);
721
+ return [];
722
+ }
723
+ if (active === undefined) {
724
+ lazyActive.set(schema, new Set([value]));
725
+ try {
726
+ return validateSchema(value, resolved, ctx, depth);
727
+ }
728
+ finally {
729
+ lazyActive.delete(schema);
730
+ }
731
+ }
732
+ active.add(value);
733
+ try {
734
+ return validateSchema(value, resolved, ctx, depth);
735
+ }
736
+ finally {
737
+ active.delete(value);
738
+ }
739
+ }
740
+ case "union": {
741
+ const branches = schema[1];
742
+ for (const branch of branches) {
743
+ const mark = ctx.nested?.length ?? 0;
744
+ if (validateSchema(value, branch, ctx, depth).length === 0) {
745
+ return [];
746
+ }
747
+ if (ctx.nested !== undefined && ctx.nested.length > mark) {
748
+ ctx.nested.length = mark;
749
+ }
750
+ }
751
+ return [createError(value, schema, ctx)];
752
+ }
753
+ case "intersection": {
754
+ const errors = [];
755
+ for (const branch of schema[1]) {
756
+ appendErrors(errors, validateSchema(value, branch, ctx, depth));
757
+ if (ctx.stopAtFirst && errors.length > 0) {
758
+ return errors;
759
+ }
760
+ }
761
+ return dedupeErrors(errors);
762
+ }
763
+ case "literal":
764
+ return Object.is(value, schema[1]) ? [] : [createError(value, schema, ctx)];
765
+ case "enum": {
766
+ const allowed = schema[1];
767
+ return allowed.includes(value) ? [] : [createError(value, schema, ctx)];
768
+ }
769
+ case "class":
770
+ return validateClass(value, schema, ctx, depth);
771
+ case "object": {
772
+ const fields = schema[1];
773
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
774
+ return [createError(value, schema, ctx)];
775
+ }
776
+ if (!enter(value, ctx)) {
777
+ return [];
778
+ }
779
+ try {
780
+ return validateFields(value, fields, ctx, depth + 1);
781
+ }
782
+ finally {
783
+ leave(value, ctx);
784
+ }
785
+ }
786
+ case "record": {
787
+ const fieldSchema = schema[1];
788
+ if (!isPlainObject(value)) {
789
+ return [createError(value, schema, ctx)];
790
+ }
791
+ if (!enter(value, ctx)) {
792
+ return [];
793
+ }
794
+ const errors = [];
795
+ try {
796
+ for (const [key, entryValue] of Object.entries(value)) {
797
+ appendErrors(errors, validateSchema(entryValue, fieldSchema, childContext(ctx, childPath(ctx.path, key), key, undefined), depth + 1));
798
+ if (ctx.stopAtFirst && errors.length > 0) {
799
+ return errors;
800
+ }
801
+ }
802
+ }
803
+ finally {
804
+ leave(value, ctx);
805
+ }
806
+ return errors;
807
+ }
808
+ case "mapof": {
809
+ if (!isInstanceOf(value, "Map")) {
810
+ return [createError(value, schema, ctx)];
811
+ }
812
+ return validateMap(value, schema[1], schema[2], ctx, depth);
813
+ }
814
+ case "setof": {
815
+ if (!isInstanceOf(value, "Set")) {
816
+ return [createError(value, schema, ctx)];
817
+ }
818
+ return validateSet(value, schema[1], ctx, depth);
819
+ }
820
+ default:
821
+ return [createError(value, schema, ctx)];
822
+ }
823
+ }
824
+ export function validateType(value, schema, context) {
825
+ const ctx = {
826
+ instance: context.instance,
827
+ field: context.field,
828
+ label: context.label,
829
+ path: context.path,
830
+ config: context.config,
831
+ labels: context.labels,
832
+ locale: context.config.locale ?? DEFAULT_LOCALE,
833
+ runtime: context.runtime ?? {
834
+ visiting: context.visiting ?? new WeakSet(),
835
+ },
836
+ nested: context.nested,
837
+ index: undefined,
838
+ stopAtFirst: context.stopAtFirst === true,
839
+ };
840
+ const errors = validateSchema(value, schema, ctx, 0);
841
+ if (errors.length === 0) {
842
+ return null;
843
+ }
844
+ const [first] = errors;
845
+ if (errors.length === 1 && first !== undefined) {
846
+ return first;
847
+ }
848
+ return errors;
849
+ }
850
+ //# sourceMappingURL=type-validation.js.map