File size: 4,770 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
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: "讗讜转讬讜转", verb: "诇讻诇讜诇" },
    file: { unit: "讘讬讬讟讬诐", verb: "诇讻诇讜诇" },
    array: { unit: "驻专讬讟讬诐", verb: "诇讻诇讜诇" },
    set: { unit: "驻专讬讟讬诐", verb: "诇讻诇讜诇" },
  };

  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" : "number";
      }
      case "object": {
        if (Array.isArray(data)) {
          return "array";
        }
        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: "拽诇讟",
    email: "讻转讜讘转 讗讬诪讬讬诇",
    url: "讻转讜讘转 专砖转",
    emoji: "讗讬诪讜讙'讬",
    uuid: "UUID",
    uuidv4: "UUIDv4",
    uuidv6: "UUIDv6",
    nanoid: "nanoid",
    guid: "GUID",
    cuid: "cuid",
    cuid2: "cuid2",
    ulid: "ULID",
    xid: "XID",
    ksuid: "KSUID",
    datetime: "转讗专讬讱 讜讝诪谉 ISO",
    date: "转讗专讬讱 ISO",
    time: "讝诪谉 ISO",
    duration: "诪砖讱 讝诪谉 ISO",
    ipv4: "讻转讜讘转 IPv4",
    ipv6: "讻转讜讘转 IPv6",
    cidrv4: "讟讜讜讞 IPv4",
    cidrv6: "讟讜讜讞 IPv6",
    base64: "诪讞专讜讝转 讘讘住讬住 64",
    base64url: "诪讞专讜讝转 讘讘住讬住 64 诇讻转讜讘讜转 专砖转",
    json_string: "诪讞专讜讝转 JSON",
    e164: "诪住驻专 E.164",
    jwt: "JWT",
    template_literal: "拽诇讟",
  };

  return (issue) => {
    switch (issue.code) {
      case "invalid_type":
        return `拽诇讟 诇讗 转拽讬谉: 爪专讬讱 ${issue.expected}, 讛转拽讘诇 ${parsedType(issue.input)}`;
      // return `Invalid input: expected ${issue.expected}, received ${util.getParsedType(issue.input)}`;
      case "invalid_value":
        if (issue.values.length === 1) return `拽诇讟 诇讗 转拽讬谉: 爪专讬讱 ${util.stringifyPrimitive(issue.values[0])}`;
        return `拽诇讟 诇讗 转拽讬谉: 爪专讬讱 讗讞转 诪讛讗驻砖专讜讬讜转  ${util.joinValues(issue.values, "|")}`;
      case "too_big": {
        const adj = issue.inclusive ? "<=" : "<";
        const sizing = getSizing(issue.origin);
        if (sizing)
          return `讙讚讜诇 诪讚讬: ${issue.origin ?? "value"} 爪专讬讱 诇讛讬讜转 ${adj}${issue.maximum.toString()} ${sizing.unit ?? "elements"}`;
        return `讙讚讜诇 诪讚讬: ${issue.origin ?? "value"} 爪专讬讱 诇讛讬讜转 ${adj}${issue.maximum.toString()}`;
      }
      case "too_small": {
        const adj = issue.inclusive ? ">=" : ">";
        const sizing = getSizing(issue.origin);
        if (sizing) {
          return `拽讟谉 诪讚讬: ${issue.origin} 爪专讬讱 诇讛讬讜转 ${adj}${issue.minimum.toString()} ${sizing.unit}`;
        }

        return `拽讟谉 诪讚讬: ${issue.origin} 爪专讬讱 诇讛讬讜转 ${adj}${issue.minimum.toString()}`;
      }
      case "invalid_format": {
        const _issue = issue as errors.$ZodStringFormatIssues;
        if (_issue.format === "starts_with") return `诪讞专讜讝转 诇讗 转拽讬谞讛: 讞讬讬讘转 诇讛转讞讬诇 讘"${_issue.prefix}"`;
        if (_issue.format === "ends_with") return `诪讞专讜讝转 诇讗 转拽讬谞讛: 讞讬讬讘转 诇讛住转讬讬诐 讘 "${_issue.suffix}"`;
        if (_issue.format === "includes") return `诪讞专讜讝转 诇讗 转拽讬谞讛: 讞讬讬讘转 诇讻诇讜诇 "${_issue.includes}"`;
        if (_issue.format === "regex") return `诪讞专讜讝转 诇讗 转拽讬谞讛: 讞讬讬讘转 诇讛转讗讬诐 诇转讘谞讬转 ${_issue.pattern}`;
        return `${Nouns[_issue.format] ?? issue.format} 诇讗 转拽讬谉`;
      }
      case "not_multiple_of":
        return `诪住驻专 诇讗 转拽讬谉: 讞讬讬讘 诇讛讬讜转 诪讻驻诇讛 砖诇 ${issue.divisor}`;
      case "unrecognized_keys":
        return `诪驻转讞${issue.keys.length > 1 ? "讜转" : ""} 诇讗 诪讝讜讛${issue.keys.length > 1 ? "讬诐" : "讛"}: ${util.joinValues(issue.keys, ", ")}`;
      case "invalid_key":
        return `诪驻转讞 诇讗 转拽讬谉 讘${issue.origin}`;
      case "invalid_union":
        return "拽诇讟 诇讗 转拽讬谉";
      case "invalid_element":
        return `注专讱 诇讗 转拽讬谉 讘${issue.origin}`;
      default:
        return `拽诇讟 诇讗 转拽讬谉`;
    }
  };
};

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