|
| 1 | +import type { $ZodStringFormats } from "../checks.js"; |
| 2 | +import type * as errors from "../errors.js"; |
| 3 | +import * as util from "../util.js"; |
| 4 | + |
| 5 | +const Sizable: Record<string, { unit: string; verb: string }> = { |
| 6 | + string: { unit: "simvol", verb: "olmalıdır" }, |
| 7 | + file: { unit: "bayt", verb: "olmalıdır" }, |
| 8 | + array: { unit: "element", verb: "olmalıdır" }, |
| 9 | + set: { unit: "element", verb: "olmalıdır" }, |
| 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: "input", |
| 43 | + email: "email address", |
| 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 datetime", |
| 57 | + date: "ISO date", |
| 58 | + time: "ISO time", |
| 59 | + duration: "ISO duration", |
| 60 | + ipv4: "IPv4 address", |
| 61 | + ipv6: "IPv6 address", |
| 62 | + cidrv4: "IPv4 range", |
| 63 | + cidrv6: "IPv6 range", |
| 64 | + base64: "base64-encoded string", |
| 65 | + base64url: "base64url-encoded string", |
| 66 | + json_string: "JSON string", |
| 67 | + e164: "E.164 number", |
| 68 | + jwt: "JWT", |
| 69 | + template_literal: "input", |
| 70 | +}; |
| 71 | + |
| 72 | +const error: errors.$ZodErrorMap = (issue) => { |
| 73 | + switch (issue.code) { |
| 74 | + case "invalid_type": |
| 75 | + return `Yanlış dəyər: gözlənilən ${issue.expected}, daxil olan ${parsedType(issue.input)}`; |
| 76 | + case "invalid_value": |
| 77 | + if (issue.values.length === 1) |
| 78 | + return `Yanlış dəyər: gözlənilən ${util.stringifyPrimitive(issue.values[0])}`; |
| 79 | + return `Yanlış seçim: aşağıdakılardan biri olmalıdır: ${util.joinValues(issue.values, "|")}`; |
| 80 | + case "too_big": { |
| 81 | + const adj = issue.inclusive ? "<=" : "<"; |
| 82 | + const sizing = getSizing(issue.origin); |
| 83 | + if (sizing) |
| 84 | + return `Çox böyük: gözlənilən ${issue.origin ?? "dəyər"} ${adj}${issue.maximum.toString()} ${sizing.unit ?? "element"}`; |
| 85 | + return `Çox böyük: gözlənilən ${issue.origin ?? "dəyər"} ${adj}${issue.maximum.toString()}`; |
| 86 | + } |
| 87 | + case "too_small": { |
| 88 | + const adj = issue.inclusive ? ">=" : ">"; |
| 89 | + const sizing = getSizing(issue.origin); |
| 90 | + if (sizing) |
| 91 | + return `Çox kiçik: gözlənilən ${issue.origin} ${adj}${issue.minimum.toString()} ${sizing.unit}`; |
| 92 | + return `Çox kiçik: gözlənilən ${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 `Yanlış mətn: "${issue}" ilə başlamalıdır`; |
| 98 | + if (_issue.format === "ends_with") |
| 99 | + return `Yanlış mətn: "${_issue.suffix}" ilə bitməlidir`; |
| 100 | + if (_issue.format === "includes") |
| 101 | + return `Yanlış mətn: "${_issue.includes}" daxil olmalıdır`; |
| 102 | + if (_issue.format === "regex") |
| 103 | + return `Yanlış mətn: ${_issue.pattern} şablonuna uyğun olmalıdır`; |
| 104 | + return `Yanlış ${Nouns[_issue.format] ?? issue.format}`; |
| 105 | + } |
| 106 | + case "not_multiple_of": |
| 107 | + return `Yanlış ədəd: ${issue.divisor} ilə bölünə bilən olmalıdır`; |
| 108 | + case "unrecognized_keys": |
| 109 | + return `Tanınmayan açar${issue.keys.length > 1 ? "lar" : ""}: ${util.joinValues(issue.keys, ", ")}`; |
| 110 | + case "invalid_key": |
| 111 | + return `${issue.origin} daxilində yanlış açar`; |
| 112 | + case "invalid_union": |
| 113 | + return "Yanlış dəyər"; |
| 114 | + case "invalid_element": |
| 115 | + return `${issue.origin} daxilində yanlış dəyər`; |
| 116 | + default: |
| 117 | + return `Yanlış dəyər`; |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +export { error }; |
| 122 | + |
| 123 | +export default function (): { localeError: errors.$ZodErrorMap } { |
| 124 | + return { |
| 125 | + localeError: error, |
| 126 | + }; |
| 127 | +} |
0 commit comments