|
| 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 Sizable: Record<string, { unit: string; verb: string }> = { |
| 6 | + string: { unit: "字元", verb: "擁有" }, |
| 7 | + file: { unit: "位元組", verb: "擁有" }, |
| 8 | + array: { unit: "項目", verb: "擁有" }, |
| 9 | + set: { unit: "項目", verb: "擁有" }, |
| 10 | +}; |
| 11 | + |
| 12 | +function getSizing(origin: string): { unit: string; verb: string } | null { |
| 13 | + return Sizable[origin] ?? null; |
| 14 | +} |
| 15 | + |
| 16 | +export const parsedType = (data: any): string => { |
| 17 | + const t = typeof data; |
| 18 | + |
| 19 | + switch (t) { |
| 20 | + case "number": { |
| 21 | + return Number.isNaN(data) ? "NaN" : "number"; |
| 22 | + } |
| 23 | + case "object": { |
| 24 | + if (Array.isArray(data)) { |
| 25 | + return "array"; |
| 26 | + } |
| 27 | + if (data === null) { |
| 28 | + return "null"; |
| 29 | + } |
| 30 | + |
| 31 | + if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) { |
| 32 | + return data.constructor.name; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + return t; |
| 37 | +}; |
| 38 | + |
| 39 | +const Nouns: { |
| 40 | + [k in $ZodStringFormats | (string & {})]?: string; |
| 41 | +} = { |
| 42 | + regex: "輸入", |
| 43 | + email: "郵件地址", |
| 44 | + url: "URL", |
| 45 | + emoji: "emoji", |
| 46 | + uuid: "UUID", |
| 47 | + uuidv4: "UUIDv4", |
| 48 | + uuidv6: "UUIDv6", |
| 49 | + nanoid: "nanoid", |
| 50 | + guid: "GUID", |
| 51 | + cuid: "cuid", |
| 52 | + cuid2: "cuid2", |
| 53 | + ulid: "ULID", |
| 54 | + xid: "XID", |
| 55 | + ksuid: "KSUID", |
| 56 | + datetime: "ISO 日期時間", |
| 57 | + date: "ISO 日期", |
| 58 | + time: "ISO 時間", |
| 59 | + duration: "ISO 期間", |
| 60 | + ipv4: "IPv4 位址", |
| 61 | + ipv6: "IPv6 位址", |
| 62 | + cidrv4: "IPv4 範圍", |
| 63 | + cidrv6: "IPv6 範圍", |
| 64 | + base64: "base64 編碼字串", |
| 65 | + base64url: "base64url 編碼字串", |
| 66 | + json_string: "JSON 字串", |
| 67 | + e164: "E.164 數值", |
| 68 | + jwt: "JWT", |
| 69 | + template_literal: "輸入", |
| 70 | +}; |
| 71 | + |
| 72 | +const error: errors.$ZodErrorMap = (issue) => { |
| 73 | + switch (issue.code) { |
| 74 | + case "invalid_type": |
| 75 | + return `無效的輸入值:預期為 ${issue.expected},但收到 ${parsedType(issue.input)}`; |
| 76 | + case "invalid_value": |
| 77 | + if (issue.values.length === 1) return `無效的輸入值:預期為 ${util.stringifyPrimitive(issue.values[0])}`; |
| 78 | + return `無效的選項:預期為以下其中之一 ${util.joinValues(issue.values, "|")}`; |
| 79 | + case "too_big": { |
| 80 | + const adj = issue.inclusive ? "<=" : "<"; |
| 81 | + const sizing = getSizing(issue.origin); |
| 82 | + if (sizing) |
| 83 | + return `數值過大:預期 ${issue.origin ?? "值"} 應為 ${adj}${issue.maximum.toString()} ${sizing.unit ?? "個元素"}`; |
| 84 | + return `數值過大:預期 ${issue.origin ?? "值"} 應為 ${adj}${issue.maximum.toString()}`; |
| 85 | + } |
| 86 | + case "too_small": { |
| 87 | + const adj = issue.inclusive ? ">=" : ">"; |
| 88 | + const sizing = getSizing(issue.origin); |
| 89 | + if (sizing) { |
| 90 | + return `數值過小:預期 ${issue.origin} 應為 ${adj}${issue.minimum.toString()} ${sizing.unit}`; |
| 91 | + } |
| 92 | + return `數值過小:預期 ${issue.origin} 應為 ${adj}${issue.minimum.toString()}`; |
| 93 | + } |
| 94 | + case "invalid_format": { |
| 95 | + const _issue = issue as errors.$ZodStringFormatIssues; |
| 96 | + if (_issue.format === "starts_with") { |
| 97 | + return `無效的字串:必須以 "${_issue.prefix}" 開頭`; |
| 98 | + } |
| 99 | + if (_issue.format === "ends_with") return `無效的字串:必須以 "${_issue.suffix}" 結尾`; |
| 100 | + if (_issue.format === "includes") return `無效的字串:必須包含 "${_issue.includes}"`; |
| 101 | + if (_issue.format === "regex") return `無效的字串:必須符合格式 ${_issue.pattern}`; |
| 102 | + return `無效的 ${Nouns[_issue.format] ?? issue.format}`; |
| 103 | + } |
| 104 | + case "not_multiple_of": |
| 105 | + return `無效的數字:必須為 ${issue.divisor} 的倍數`; |
| 106 | + case "unrecognized_keys": |
| 107 | + return `無法識別的鍵值${issue.keys.length > 1 ? "們" : ""}:${util.joinValues(issue.keys, "、")}`; |
| 108 | + case "invalid_key": |
| 109 | + return `${issue.origin} 中有無效的鍵值`; |
| 110 | + case "invalid_union": |
| 111 | + return "無效的輸入值"; |
| 112 | + case "invalid_element": |
| 113 | + return `${issue.origin} 中有無效的值`; |
| 114 | + default: |
| 115 | + return `無效的輸入值`; |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +export { error }; |
| 120 | + |
| 121 | +export default function (): { localeError: errors.$ZodErrorMap } { |
| 122 | + return { |
| 123 | + localeError: error, |
| 124 | + }; |
| 125 | +} |
0 commit comments