|
| 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 | +} |
0 commit comments