zod 4.0.5 → 4.0.6

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 (86) hide show
  1. package/package.json +1 -1
  2. package/src/v3/tests/string.test.ts +2 -2
  3. package/src/v3/types.ts +3 -1
  4. package/src/v4/classic/errors.ts +9 -2
  5. package/src/v4/classic/schemas.ts +11 -9
  6. package/src/v4/classic/tests/catch.test.ts +4 -5
  7. package/src/v4/classic/tests/discriminated-unions.test.ts +12 -0
  8. package/src/v4/classic/tests/error-utils.test.ts +43 -0
  9. package/src/v4/classic/tests/literal.test.ts +25 -0
  10. package/src/v4/classic/tests/partial.test.ts +193 -0
  11. package/src/v4/classic/tests/pickomit.test.ts +5 -5
  12. package/src/v4/classic/tests/preprocess.test.ts +4 -15
  13. package/src/v4/classic/tests/record.test.ts +15 -1
  14. package/src/v4/classic/tests/recursive-types.test.ts +67 -0
  15. package/src/v4/classic/tests/string.test.ts +81 -4
  16. package/src/v4/classic/tests/template-literal.test.ts +3 -0
  17. package/src/v4/classic/tests/to-json-schema.test.ts +1 -0
  18. package/src/v4/classic/tests/transform.test.ts +110 -0
  19. package/src/v4/classic/tests/union.test.ts +45 -3
  20. package/src/v4/core/checks.ts +2 -2
  21. package/src/v4/core/errors.ts +8 -15
  22. package/src/v4/core/regexes.ts +1 -1
  23. package/src/v4/core/registries.ts +3 -2
  24. package/src/v4/core/schemas.ts +90 -98
  25. package/src/v4/core/to-json-schema.ts +1 -0
  26. package/src/v4/core/util.ts +175 -115
  27. package/src/v4/core/versions.ts +1 -1
  28. package/src/v4/locales/bg.ts +136 -0
  29. package/src/v4/locales/da.ts +141 -0
  30. package/src/v4/locales/index.ts +2 -0
  31. package/src/v4/locales/is.ts +127 -0
  32. package/src/v4/mini/schemas.ts +3 -1
  33. package/v3/types.cjs +2 -0
  34. package/v3/types.d.cts +4 -1
  35. package/v3/types.d.ts +4 -1
  36. package/v3/types.js +2 -0
  37. package/v4/classic/errors.cjs +9 -2
  38. package/v4/classic/errors.js +9 -2
  39. package/v4/classic/schemas.cjs +5 -3
  40. package/v4/classic/schemas.d.cts +3 -3
  41. package/v4/classic/schemas.d.ts +3 -3
  42. package/v4/classic/schemas.js +5 -3
  43. package/v4/core/checks.d.cts +2 -2
  44. package/v4/core/checks.d.ts +2 -2
  45. package/v4/core/errors.cjs +4 -9
  46. package/v4/core/errors.d.cts +4 -6
  47. package/v4/core/errors.d.ts +4 -6
  48. package/v4/core/errors.js +4 -9
  49. package/v4/core/regexes.cjs +1 -1
  50. package/v4/core/regexes.d.cts +1 -1
  51. package/v4/core/regexes.d.ts +1 -1
  52. package/v4/core/regexes.js +1 -1
  53. package/v4/core/registries.cjs +2 -1
  54. package/v4/core/registries.d.cts +1 -1
  55. package/v4/core/registries.d.ts +1 -1
  56. package/v4/core/registries.js +2 -1
  57. package/v4/core/schemas.cjs +47 -87
  58. package/v4/core/schemas.d.cts +9 -4
  59. package/v4/core/schemas.d.ts +9 -4
  60. package/v4/core/schemas.js +47 -87
  61. package/v4/core/to-json-schema.cjs +1 -0
  62. package/v4/core/to-json-schema.js +1 -0
  63. package/v4/core/util.cjs +163 -112
  64. package/v4/core/util.d.cts +1 -0
  65. package/v4/core/util.d.ts +1 -0
  66. package/v4/core/util.js +162 -112
  67. package/v4/core/versions.cjs +1 -1
  68. package/v4/core/versions.js +1 -1
  69. package/v4/locales/bg.cjs +156 -0
  70. package/v4/locales/bg.d.cts +5 -0
  71. package/v4/locales/bg.d.ts +5 -0
  72. package/v4/locales/bg.js +128 -0
  73. package/v4/locales/da.cjs +157 -0
  74. package/v4/locales/da.d.cts +4 -0
  75. package/v4/locales/da.d.ts +4 -0
  76. package/v4/locales/da.js +131 -0
  77. package/v4/locales/index.cjs +5 -1
  78. package/v4/locales/index.d.cts +2 -0
  79. package/v4/locales/index.d.ts +2 -0
  80. package/v4/locales/index.js +2 -0
  81. package/v4/locales/is.cjs +145 -0
  82. package/v4/locales/is.d.cts +5 -0
  83. package/v4/locales/is.d.ts +5 -0
  84. package/v4/locales/is.js +117 -0
  85. package/v4/mini/schemas.cjs +3 -1
  86. package/v4/mini/schemas.js +3 -1
@@ -283,6 +283,12 @@ export function assignProp<T extends object, K extends PropertyKey>(
283
283
  });
284
284
  }
285
285
 
286
+ export function cloneDef(schema: schemas.$ZodType): any {
287
+ const def: any = {};
288
+ Object.defineProperties(def, Object.getOwnPropertyDescriptors(schema._zod.def));
289
+ return def;
290
+ }
291
+
286
292
  export function getElementAtPath(obj: any, path: (string | number)[] | null | undefined): any {
287
293
  if (!path) return obj;
288
294
  return path.reduce((acc, key) => acc?.[key], obj);
@@ -314,15 +320,16 @@ export function esc(str: string): string {
314
320
  return JSON.stringify(str);
315
321
  }
316
322
 
317
- export const captureStackTrace: (targetObject: object, constructorOpt?: Function) => void = Error.captureStackTrace
318
- ? Error.captureStackTrace
319
- : (..._args) => {};
323
+ export const captureStackTrace: (targetObject: object, constructorOpt?: Function) => void = (
324
+ "captureStackTrace" in Error ? Error.captureStackTrace : (..._args: any[]) => {}
325
+ ) as any;
320
326
 
321
327
  export function isObject(data: any): data is Record<PropertyKey, unknown> {
322
328
  return typeof data === "object" && data !== null && !Array.isArray(data);
323
329
  }
324
330
 
325
331
  export const allowsEval: { value: boolean } = cached(() => {
332
+ // @ts-ignore
326
333
  if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) {
327
334
  return false;
328
335
  }
@@ -409,6 +416,7 @@ export const getParsedType = (data: any): ParsedTypes => {
409
416
  if (typeof Date !== "undefined" && data instanceof Date) {
410
417
  return "date";
411
418
  }
419
+ // @ts-ignore
412
420
  if (typeof File !== "undefined" && data instanceof File) {
413
421
  return "file";
414
422
  }
@@ -535,71 +543,103 @@ export const BIGINT_FORMAT_RANGES: Record<checks.$ZodBigIntFormats, [bigint, big
535
543
  };
536
544
 
537
545
  export function pick(schema: schemas.$ZodObject, mask: Record<string, unknown>): any {
538
- const newShape: Writeable<schemas.$ZodShape> = {};
539
- const currDef = schema._zod.def; //.shape;
540
-
541
- for (const key in mask) {
542
- if (!(key in currDef.shape)) {
543
- throw new Error(`Unrecognized key: "${key}"`);
544
- }
545
- if (!mask[key]) continue;
546
-
547
- // pick key
548
- newShape[key] = currDef.shape[key]!;
549
- }
546
+ const currDef = schema._zod.def;
547
+
548
+ const def = cloneDef(schema);
549
+ Object.defineProperties(def, {
550
+ shape: {
551
+ get() {
552
+ const newShape: Writeable<schemas.$ZodShape> = {};
553
+ for (const key in mask) {
554
+ if (!(key in currDef.shape)) {
555
+ throw new Error(`Unrecognized key: "${key}"`);
556
+ }
557
+ if (!mask[key]) continue;
558
+ newShape[key] = currDef.shape[key]!;
559
+ }
560
+
561
+ assignProp(this, "shape", newShape); // self-caching
562
+ return newShape;
563
+ },
564
+ },
565
+ checks: {
566
+ value: [],
567
+ },
568
+ });
550
569
 
551
- return clone(schema, {
552
- ...schema._zod.def,
553
- shape: newShape,
554
- checks: [],
555
- }) as any;
570
+ return clone(schema, def) as any;
556
571
  }
557
572
 
558
573
  export function omit(schema: schemas.$ZodObject, mask: object): any {
559
- const newShape: Writeable<schemas.$ZodShape> = { ...schema._zod.def.shape };
560
- const currDef = schema._zod.def; //.shape;
561
- for (const key in mask) {
562
- if (!(key in currDef.shape)) {
563
- throw new Error(`Unrecognized key: "${key}"`);
564
- }
565
- if (!(mask as any)[key]) continue;
566
-
567
- delete newShape[key];
568
- }
569
- return clone(schema, {
570
- ...schema._zod.def,
571
- shape: newShape,
572
- checks: [],
574
+ const currDef = schema._zod.def;
575
+
576
+ const def = cloneDef(schema);
577
+ Object.defineProperties(def, {
578
+ shape: {
579
+ get() {
580
+ const newShape: Writeable<schemas.$ZodShape> = { ...schema._zod.def.shape };
581
+ for (const key in mask) {
582
+ if (!(key in currDef.shape)) {
583
+ throw new Error(`Unrecognized key: "${key}"`);
584
+ }
585
+ if (!(mask as any)[key]) continue;
586
+
587
+ delete newShape[key];
588
+ }
589
+ assignProp(this, "shape", newShape); // self-caching
590
+ return newShape;
591
+ },
592
+ },
593
+ checks: {
594
+ value: [],
595
+ },
573
596
  });
597
+
598
+ return clone(schema, def);
574
599
  }
575
600
 
576
601
  export function extend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): any {
577
602
  if (!isPlainObject(shape)) {
578
603
  throw new Error("Invalid input to extend: expected a plain object");
579
604
  }
580
- const def = {
581
- ...schema._zod.def,
582
- get shape() {
583
- const _shape = { ...schema._zod.def.shape, ...shape };
584
- assignProp(this, "shape", _shape); // self-caching
585
- return _shape;
605
+
606
+ const def = cloneDef(schema);
607
+ Object.defineProperties(def, {
608
+ shape: {
609
+ get() {
610
+ const _shape = { ...schema._zod.def.shape, ...shape };
611
+ assignProp(this, "shape", _shape); // self-caching
612
+ return _shape;
613
+ },
614
+ },
615
+ checks: {
616
+ value: [],
586
617
  },
587
- checks: [], // delete existing checks
588
- } as any;
618
+ });
589
619
  return clone(schema, def) as any;
590
620
  }
591
621
 
592
622
  export function merge(a: schemas.$ZodObject, b: schemas.$ZodObject): any {
593
- return clone(a, {
594
- ...a._zod.def,
595
- get shape() {
596
- const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
597
- assignProp(this, "shape", _shape); // self-caching
598
- return _shape;
623
+ const def = cloneDef(a);
624
+ Object.defineProperties(def, {
625
+ shape: {
626
+ get() {
627
+ const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
628
+ assignProp(this, "shape", _shape); // self-caching
629
+ return _shape;
630
+ },
631
+ },
632
+ catchall: {
633
+ get() {
634
+ return b._zod.def.catchall;
635
+ },
636
+ },
637
+ checks: {
638
+ value: [], // delete existing checks
599
639
  },
600
- catchall: b._zod.def.catchall,
601
- checks: [], // delete existing checks
602
- }) as any;
640
+ });
641
+
642
+ return clone(a, def) as any;
603
643
  }
604
644
 
605
645
  export function partial(
@@ -607,40 +647,49 @@ export function partial(
607
647
  schema: schemas.$ZodObject,
608
648
  mask: object | undefined
609
649
  ): any {
610
- const oldShape = schema._zod.def.shape;
611
- const shape: Writeable<schemas.$ZodShape> = { ...oldShape };
612
-
613
- if (mask) {
614
- for (const key in mask) {
615
- if (!(key in oldShape)) {
616
- throw new Error(`Unrecognized key: "${key}"`);
617
- }
618
- if (!(mask as any)[key]) continue;
619
- // if (oldShape[key]!._zod.optin === "optional") continue;
620
- shape[key] = Class
621
- ? new Class({
622
- type: "optional",
623
- innerType: oldShape[key]!,
624
- })
625
- : oldShape[key]!;
626
- }
627
- } else {
628
- for (const key in oldShape) {
629
- // if (oldShape[key]!._zod.optin === "optional") continue;
630
- shape[key] = Class
631
- ? new Class({
632
- type: "optional",
633
- innerType: oldShape[key]!,
634
- })
635
- : oldShape[key]!;
636
- }
637
- }
650
+ const def = cloneDef(schema);
651
+ Object.defineProperties(def, {
652
+ shape: {
653
+ get() {
654
+ const oldShape = schema._zod.def.shape;
655
+ const shape: Writeable<schemas.$ZodShape> = { ...oldShape };
656
+
657
+ if (mask) {
658
+ for (const key in mask) {
659
+ if (!(key in oldShape)) {
660
+ throw new Error(`Unrecognized key: "${key}"`);
661
+ }
662
+ if (!(mask as any)[key]) continue;
663
+ // if (oldShape[key]!._zod.optin === "optional") continue;
664
+ shape[key] = Class
665
+ ? new Class({
666
+ type: "optional",
667
+ innerType: oldShape[key]!,
668
+ })
669
+ : oldShape[key]!;
670
+ }
671
+ } else {
672
+ for (const key in oldShape) {
673
+ // if (oldShape[key]!._zod.optin === "optional") continue;
674
+ shape[key] = Class
675
+ ? new Class({
676
+ type: "optional",
677
+ innerType: oldShape[key]!,
678
+ })
679
+ : oldShape[key]!;
680
+ }
681
+ }
682
+
683
+ assignProp(this, "shape", shape); // self-caching
684
+ return shape;
685
+ },
686
+ },
687
+ checks: {
688
+ value: [],
689
+ },
690
+ });
638
691
 
639
- return clone(schema, {
640
- ...schema._zod.def,
641
- shape,
642
- checks: [],
643
- }) as any;
692
+ return clone(schema, def) as any;
644
693
  }
645
694
 
646
695
  export function required(
@@ -648,44 +697,54 @@ export function required(
648
697
  schema: schemas.$ZodObject,
649
698
  mask: object | undefined
650
699
  ): any {
651
- const oldShape = schema._zod.def.shape;
652
- const shape: Writeable<schemas.$ZodShape> = { ...oldShape };
653
-
654
- if (mask) {
655
- for (const key in mask) {
656
- if (!(key in shape)) {
657
- throw new Error(`Unrecognized key: "${key}"`);
658
- }
659
- if (!(mask as any)[key]) continue;
660
- // overwrite with non-optional
661
- shape[key] = new Class({
662
- type: "nonoptional",
663
- innerType: oldShape[key]!,
664
- });
665
- }
666
- } else {
667
- for (const key in oldShape) {
668
- // overwrite with non-optional
669
- shape[key] = new Class({
670
- type: "nonoptional",
671
- innerType: oldShape[key]!,
672
- });
673
- }
674
- }
700
+ const def = cloneDef(schema);
701
+ Object.defineProperties(def, {
702
+ shape: {
703
+ get() {
704
+ const oldShape = schema._zod.def.shape;
705
+ const shape: Writeable<schemas.$ZodShape> = { ...oldShape };
706
+
707
+ if (mask) {
708
+ for (const key in mask) {
709
+ if (!(key in shape)) {
710
+ throw new Error(`Unrecognized key: "${key}"`);
711
+ }
712
+ if (!(mask as any)[key]) continue;
713
+ // overwrite with non-optional
714
+ shape[key] = new Class({
715
+ type: "nonoptional",
716
+ innerType: oldShape[key]!,
717
+ });
718
+ }
719
+ } else {
720
+ for (const key in oldShape) {
721
+ // overwrite with non-optional
722
+ shape[key] = new Class({
723
+ type: "nonoptional",
724
+ innerType: oldShape[key]!,
725
+ });
726
+ }
727
+ }
728
+
729
+ assignProp(this, "shape", shape); // self-caching
730
+ return shape;
731
+ },
732
+ },
733
+ checks: {
734
+ value: [],
735
+ },
736
+ });
675
737
 
676
- return clone(schema, {
677
- ...schema._zod.def,
678
- shape,
679
- // optional: [],
680
- checks: [],
681
- }) as any;
738
+ return clone(schema, def) as any;
682
739
  }
683
740
 
684
741
  export type Constructor<T, Def extends any[] = any[]> = new (...args: Def) => T;
685
742
 
686
743
  export function aborted(x: schemas.ParsePayload, startIndex = 0): boolean {
687
744
  for (let i = startIndex; i < x.issues.length; i++) {
688
- if (x.issues[i]?.continue !== true) return true;
745
+ if (x.issues[i]?.continue !== true) {
746
+ return true;
747
+ }
689
748
  }
690
749
  return false;
691
750
  }
@@ -733,6 +792,7 @@ export function finalizeIssue(
733
792
  export function getSizableOrigin(input: any): "set" | "map" | "file" | "unknown" {
734
793
  if (input instanceof Set) return "set";
735
794
  if (input instanceof Map) return "map";
795
+ // @ts-ignore
736
796
  if (input instanceof File) return "file";
737
797
  return "unknown";
738
798
  }
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 5 as number,
4
+ patch: 6 as number,
5
5
  } as const;
@@ -0,0 +1,136 @@
1
+ import type { $ZodStringFormats } from "../core/checks.js";
2
+ import type * as errors from "../core/errors.js";
3
+ import * as util from "../core/util.js";
4
+
5
+ export const parsedType = (data: any): string => {
6
+ const t = typeof data;
7
+
8
+ switch (t) {
9
+ case "number": {
10
+ return Number.isNaN(data) ? "NaN" : "число";
11
+ }
12
+ case "object": {
13
+ if (Array.isArray(data)) {
14
+ return "масив";
15
+ }
16
+ if (data === null) {
17
+ return "null";
18
+ }
19
+
20
+ if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
21
+ return data.constructor.name;
22
+ }
23
+ }
24
+ }
25
+ return t;
26
+ };
27
+
28
+ const error: () => errors.$ZodErrorMap = () => {
29
+ const Sizable: Record<string, { unit: string; verb: string }> = {
30
+ string: { unit: "символа", verb: "да съдържа" },
31
+ file: { unit: "байта", verb: "да съдържа" },
32
+ array: { unit: "елемента", verb: "да съдържа" },
33
+ set: { unit: "елемента", verb: "да съдържа" },
34
+ };
35
+
36
+ function getSizing(origin: string): { unit: string; verb: string } | null {
37
+ return Sizable[origin] ?? null;
38
+ }
39
+
40
+ const Nouns: {
41
+ [k in $ZodStringFormats | (string & {})]?: string;
42
+ } = {
43
+ regex: "вход",
44
+ email: "имейл адрес",
45
+ url: "URL",
46
+ emoji: "емоджи",
47
+ uuid: "UUID",
48
+ uuidv4: "UUIDv4",
49
+ uuidv6: "UUIDv6",
50
+ nanoid: "nanoid",
51
+ guid: "GUID",
52
+ cuid: "cuid",
53
+ cuid2: "cuid2",
54
+ ulid: "ULID",
55
+ xid: "XID",
56
+ ksuid: "KSUID",
57
+ datetime: "ISO време",
58
+ date: "ISO дата",
59
+ time: "ISO време",
60
+ duration: "ISO продължителност",
61
+ ipv4: "IPv4 адрес",
62
+ ipv6: "IPv6 адрес",
63
+ cidrv4: "IPv4 диапазон",
64
+ cidrv6: "IPv6 диапазон",
65
+ base64: "base64-кодиран низ",
66
+ base64url: "base64url-кодиран низ",
67
+ json_string: "JSON низ",
68
+ e164: "E.164 номер",
69
+ jwt: "JWT",
70
+ template_literal: "вход",
71
+ };
72
+
73
+ return (issue) => {
74
+ switch (issue.code) {
75
+ case "invalid_type":
76
+ return `Невалиден вход: очакван ${issue.expected}, получен ${parsedType(issue.input)}`;
77
+
78
+ case "invalid_value":
79
+ if (issue.values.length === 1) return `Невалиден вход: очакван ${util.stringifyPrimitive(issue.values[0])}`;
80
+ return `Невалидна опция: очаквано едно от ${util.joinValues(issue.values, "|")}`;
81
+ case "too_big": {
82
+ const adj = issue.inclusive ? "<=" : "<";
83
+ const sizing = getSizing(issue.origin);
84
+ if (sizing)
85
+ return `Твърде голямо: очаква се ${issue.origin ?? "стойност"} да съдържа ${adj}${issue.maximum.toString()} ${sizing.unit ?? "елемента"}`;
86
+ return `Твърде голямо: очаква се ${issue.origin ?? "стойност"} да бъде ${adj}${issue.maximum.toString()}`;
87
+ }
88
+ case "too_small": {
89
+ const adj = issue.inclusive ? ">=" : ">";
90
+ const sizing = getSizing(issue.origin);
91
+ if (sizing) {
92
+ return `Твърде малко: очаква се ${issue.origin} да съдържа ${adj}${issue.minimum.toString()} ${sizing.unit}`;
93
+ }
94
+
95
+ return `Твърде малко: очаква се ${issue.origin} да бъде ${adj}${issue.minimum.toString()}`;
96
+ }
97
+ case "invalid_format": {
98
+ const _issue = issue as errors.$ZodStringFormatIssues;
99
+ if (_issue.format === "starts_with") {
100
+ return `Невалиден низ: трябва да започва с "${_issue.prefix}"`;
101
+ }
102
+ if (_issue.format === "ends_with") return `Невалиден низ: трябва да завършва с "${_issue.suffix}"`;
103
+ if (_issue.format === "includes") return `Невалиден низ: трябва да включва "${_issue.includes}"`;
104
+ if (_issue.format === "regex") return `Невалиден низ: трябва да съвпада с ${_issue.pattern}`;
105
+
106
+ let invalid_adj = "Невалиден";
107
+
108
+ if (_issue.format === "emoji") invalid_adj = "Невалидно";
109
+ if (_issue.format === "datetime") invalid_adj = "Невалидно";
110
+ if (_issue.format === "date") invalid_adj = "Невалидна";
111
+ if (_issue.format === "time") invalid_adj = "Невалидно";
112
+ if (_issue.format === "duration") invalid_adj = "Невалидна";
113
+
114
+ return `${invalid_adj} ${Nouns[_issue.format] ?? issue.format}`;
115
+ }
116
+ case "not_multiple_of":
117
+ return `Невалидно число: трябва да бъде кратно на ${issue.divisor}`;
118
+ case "unrecognized_keys":
119
+ return `Неразпознат${issue.keys.length > 1 ? "и" : ""} ключ${issue.keys.length > 1 ? "ове" : ""}: ${util.joinValues(issue.keys, ", ")}`;
120
+ case "invalid_key":
121
+ return `Невалиден ключ в ${issue.origin}`;
122
+ case "invalid_union":
123
+ return "Невалиден вход";
124
+ case "invalid_element":
125
+ return `Невалидна стойност в ${issue.origin}`;
126
+ default:
127
+ return `Невалиден вход`;
128
+ }
129
+ };
130
+ };
131
+
132
+ export default function (): { localeError: errors.$ZodErrorMap } {
133
+ return {
134
+ localeError: error(),
135
+ };
136
+ }
@@ -0,0 +1,141 @@
1
+ import type { $ZodStringFormats } from "../core/checks.js";
2
+ import type * as errors from "../core/errors.js";
3
+ import * as util from "../core/util.js";
4
+
5
+ const error: () => errors.$ZodErrorMap = () => {
6
+ const Sizable: Record<string, { unit: string; verb: string }> = {
7
+ string: { unit: "tegn", verb: "havde" },
8
+ file: { unit: "bytes", verb: "havde" },
9
+ array: { unit: "elementer", verb: "indeholdt" },
10
+ set: { unit: "elementer", verb: "indeholdt" },
11
+ };
12
+
13
+ const TypeNames: Record<string, string> = {
14
+ string: "streng",
15
+ number: "tal",
16
+ boolean: "boolean",
17
+ array: "liste",
18
+ object: "objekt",
19
+ set: "sæt",
20
+ file: "fil",
21
+ };
22
+
23
+ function getSizing(origin: string): { unit: string; verb: string } | null {
24
+ return Sizable[origin] ?? null;
25
+ }
26
+
27
+ function getTypeName(type: string): string {
28
+ return TypeNames[type] ?? type;
29
+ }
30
+
31
+ const parsedType = (data: any): string => {
32
+ const t = typeof data;
33
+
34
+ switch (t) {
35
+ case "number": {
36
+ return Number.isNaN(data) ? "NaN" : "tal";
37
+ }
38
+ case "object": {
39
+ if (Array.isArray(data)) {
40
+ return "liste";
41
+ }
42
+ if (data === null) {
43
+ return "null";
44
+ }
45
+
46
+ if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
47
+ return data.constructor.name;
48
+ }
49
+ return "objekt";
50
+ }
51
+ }
52
+ return t;
53
+ };
54
+
55
+ const Nouns: {
56
+ [k in $ZodStringFormats | (string & {})]?: string;
57
+ } = {
58
+ regex: "input",
59
+ email: "e-mailadresse",
60
+ url: "URL",
61
+ emoji: "emoji",
62
+ uuid: "UUID",
63
+ uuidv4: "UUIDv4",
64
+ uuidv6: "UUIDv6",
65
+ nanoid: "nanoid",
66
+ guid: "GUID",
67
+ cuid: "cuid",
68
+ cuid2: "cuid2",
69
+ ulid: "ULID",
70
+ xid: "XID",
71
+ ksuid: "KSUID",
72
+ datetime: "ISO dato- og klokkeslæt",
73
+ date: "ISO-dato",
74
+ time: "ISO-klokkeslæt",
75
+ duration: "ISO-varighed",
76
+ ipv4: "IPv4-område",
77
+ ipv6: "IPv6-område",
78
+ cidrv4: "IPv4-spektrum",
79
+ cidrv6: "IPv6-spektrum",
80
+ base64: "base64-kodet streng",
81
+ base64url: "base64url-kodet streng",
82
+ json_string: "JSON-streng",
83
+ e164: "E.164-nummer",
84
+ jwt: "JWT",
85
+ template_literal: "input",
86
+ };
87
+
88
+ return (issue) => {
89
+ switch (issue.code) {
90
+ case "invalid_type":
91
+ return `Ugyldigt input: forventede ${getTypeName(issue.expected)}, fik ${getTypeName(parsedType(issue.input))}`;
92
+ case "invalid_value":
93
+ if (issue.values.length === 1) return `Ugyldig værdi: forventede ${util.stringifyPrimitive(issue.values[0])}`;
94
+ return `Ugyldigt valg: forventede en af følgende ${util.joinValues(issue.values, "|")}`;
95
+ case "too_big": {
96
+ const adj = issue.inclusive ? "<=" : "<";
97
+ const sizing = getSizing(issue.origin);
98
+ const origin = getTypeName(issue.origin);
99
+ if (sizing)
100
+ return `For stor: forventede ${origin ?? "value"} ${sizing.verb} ${adj} ${issue.maximum.toString()} ${sizing.unit ?? "elementer"}`;
101
+ return `For stor: forventede ${origin ?? "value"} havde ${adj} ${issue.maximum.toString()}`;
102
+ }
103
+ case "too_small": {
104
+ const adj = issue.inclusive ? ">=" : ">";
105
+ const sizing = getSizing(issue.origin);
106
+ const origin = getTypeName(issue.origin);
107
+ if (sizing) {
108
+ return `For lille: forventede ${origin} ${sizing.verb} ${adj} ${issue.minimum.toString()} ${sizing.unit}`;
109
+ }
110
+
111
+ return `For lille: forventede ${origin} havde ${adj} ${issue.minimum.toString()}`;
112
+ }
113
+ case "invalid_format": {
114
+ const _issue = issue as errors.$ZodStringFormatIssues;
115
+ if (_issue.format === "starts_with") return `Ugyldig streng: skal starte med "${_issue.prefix}"`;
116
+ if (_issue.format === "ends_with") return `Ugyldig streng: skal ende med "${_issue.suffix}"`;
117
+ if (_issue.format === "includes") return `Ugyldig streng: skal indeholde "${_issue.includes}"`;
118
+ if (_issue.format === "regex") return `Ugyldig streng: skal matche mønsteret ${_issue.pattern}`;
119
+ return `Ugyldig ${Nouns[_issue.format] ?? issue.format}`;
120
+ }
121
+ case "not_multiple_of":
122
+ return `Ugyldigt tal: skal være deleligt med ${issue.divisor}`;
123
+ case "unrecognized_keys":
124
+ return `${issue.keys.length > 1 ? "Ukendte nøgler" : "Ukendt nøgle"}: ${util.joinValues(issue.keys, ", ")}`;
125
+ case "invalid_key":
126
+ return `Ugyldig nøgle i ${issue.origin}`;
127
+ case "invalid_union":
128
+ return "Ugyldigt input: matcher ingen af de tilladte typer";
129
+ case "invalid_element":
130
+ return `Ugyldig værdi i ${issue.origin}`;
131
+ default:
132
+ return `Ugyldigt input`;
133
+ }
134
+ };
135
+ };
136
+
137
+ export default function (): { localeError: errors.$ZodErrorMap } {
138
+ return {
139
+ localeError: error(),
140
+ };
141
+ }
@@ -3,6 +3,7 @@ export { default as az } from "./az.js";
3
3
  export { default as be } from "./be.js";
4
4
  export { default as ca } from "./ca.js";
5
5
  export { default as cs } from "./cs.js";
6
+ export { default as da } from "./da.js";
6
7
  export { default as de } from "./de.js";
7
8
  export { default as en } from "./en.js";
8
9
  export { default as eo } from "./eo.js";
@@ -14,6 +15,7 @@ export { default as frCA } from "./fr-CA.js";
14
15
  export { default as he } from "./he.js";
15
16
  export { default as hu } from "./hu.js";
16
17
  export { default as id } from "./id.js";
18
+ export { default as is } from "./is.js";
17
19
  export { default as it } from "./it.js";
18
20
  export { default as ja } from "./ja.js";
19
21
  export { default as kh } from "./kh.js";