File size: 4,743 Bytes
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { $ZodStringFormats } from "../core/checks.js";
import type * as errors from "../core/errors.js";
import * as util from "../core/util.js";

const error: () => errors.$ZodErrorMap = () => {
  const Sizable: Record<string, { unit: string; verb: string }> = {
    string: { unit: "znak贸w", verb: "mie膰" },
    file: { unit: "bajt贸w", verb: "mie膰" },
    array: { unit: "element贸w", verb: "mie膰" },
    set: { unit: "element贸w", verb: "mie膰" },
  };

  function getSizing(origin: string): { unit: string; verb: string } | null {
    return Sizable[origin] ?? null;
  }

  const parsedType = (data: any): string => {
    const t = typeof data;

    switch (t) {
      case "number": {
        return Number.isNaN(data) ? "NaN" : "liczba";
      }
      case "object": {
        if (Array.isArray(data)) {
          return "tablica";
        }
        if (data === null) {
          return "null";
        }

        if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
          return data.constructor.name;
        }
      }
    }
    return t;
  };

  const Nouns: {
    [k in $ZodStringFormats | (string & {})]?: string;
  } = {
    regex: "wyra偶enie",
    email: "adres email",
    url: "URL",
    emoji: "emoji",
    uuid: "UUID",
    uuidv4: "UUIDv4",
    uuidv6: "UUIDv6",
    nanoid: "nanoid",
    guid: "GUID",
    cuid: "cuid",
    cuid2: "cuid2",
    ulid: "ULID",
    xid: "XID",
    ksuid: "KSUID",
    datetime: "data i godzina w formacie ISO",
    date: "data w formacie ISO",
    time: "godzina w formacie ISO",
    duration: "czas trwania ISO",
    ipv4: "adres IPv4",
    ipv6: "adres IPv6",
    cidrv4: "zakres IPv4",
    cidrv6: "zakres IPv6",
    base64: "ci膮g znak贸w zakodowany w formacie base64",
    base64url: "ci膮g znak贸w zakodowany w formacie base64url",
    json_string: "ci膮g znak贸w w formacie JSON",
    e164: "liczba E.164",
    jwt: "JWT",
    template_literal: "wej艣cie",
  };

  return (issue) => {
    switch (issue.code) {
      case "invalid_type":
        return `Nieprawid艂owe dane wej艣ciowe: oczekiwano ${issue.expected}, otrzymano ${parsedType(issue.input)}`;
      case "invalid_value":
        if (issue.values.length === 1)
          return `Nieprawid艂owe dane wej艣ciowe: oczekiwano ${util.stringifyPrimitive(issue.values[0])}`;
        return `Nieprawid艂owa opcja: oczekiwano jednej z warto艣ci ${util.joinValues(issue.values, "|")}`;
      case "too_big": {
        const adj = issue.inclusive ? "<=" : "<";
        const sizing = getSizing(issue.origin);
        if (sizing) {
          return `Za du偶a warto艣膰: oczekiwano, 偶e ${issue.origin ?? "warto艣膰"} b臋dzie mie膰 ${adj}${issue.maximum.toString()} ${sizing.unit ?? "element贸w"}`;
        }
        return `Zbyt du偶(y/a/e): oczekiwano, 偶e ${issue.origin ?? "warto艣膰"} b臋dzie wynosi膰 ${adj}${issue.maximum.toString()}`;
      }
      case "too_small": {
        const adj = issue.inclusive ? ">=" : ">";
        const sizing = getSizing(issue.origin);
        if (sizing) {
          return `Za ma艂a warto艣膰: oczekiwano, 偶e ${issue.origin ?? "warto艣膰"} b臋dzie mie膰 ${adj}${issue.minimum.toString()} ${sizing.unit ?? "element贸w"}`;
        }
        return `Zbyt ma艂(y/a/e): oczekiwano, 偶e ${issue.origin ?? "warto艣膰"} b臋dzie wynosi膰 ${adj}${issue.minimum.toString()}`;
      }
      case "invalid_format": {
        const _issue = issue as errors.$ZodStringFormatIssues;
        if (_issue.format === "starts_with")
          return `Nieprawid艂owy ci膮g znak贸w: musi zaczyna膰 si臋 od "${_issue.prefix}"`;
        if (_issue.format === "ends_with") return `Nieprawid艂owy ci膮g znak贸w: musi ko艅czy膰 si臋 na "${_issue.suffix}"`;
        if (_issue.format === "includes") return `Nieprawid艂owy ci膮g znak贸w: musi zawiera膰 "${_issue.includes}"`;
        if (_issue.format === "regex") return `Nieprawid艂owy ci膮g znak贸w: musi odpowiada膰 wzorcowi ${_issue.pattern}`;
        return `Nieprawid艂ow(y/a/e) ${Nouns[_issue.format] ?? issue.format}`;
      }
      case "not_multiple_of":
        return `Nieprawid艂owa liczba: musi by膰 wielokrotno艣ci膮 ${issue.divisor}`;
      case "unrecognized_keys":
        return `Nierozpoznane klucze${issue.keys.length > 1 ? "s" : ""}: ${util.joinValues(issue.keys, ", ")}`;
      case "invalid_key":
        return `Nieprawid艂owy klucz w ${issue.origin}`;
      case "invalid_union":
        return "Nieprawid艂owe dane wej艣ciowe";
      case "invalid_element":
        return `Nieprawid艂owa warto艣膰 w ${issue.origin}`;
      default:
        return `Nieprawid艂owe dane wej艣ciowe`;
    }
  };
};

export default function (): { localeError: errors.$ZodErrorMap } {
  return {
    localeError: error(),
  };
}