117 lines
4.4 KiB
JavaScript
117 lines
4.4 KiB
JavaScript
import * as util from "../core/util.js";
|
|
export const parsedType = (data) => {
|
|
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 error = () => {
|
|
const Sizable = {
|
|
string: { unit: "characters", verb: "to have" },
|
|
file: { unit: "bytes", verb: "to have" },
|
|
array: { unit: "items", verb: "to have" },
|
|
set: { unit: "items", verb: "to have" },
|
|
};
|
|
function getSizing(origin) {
|
|
return Sizable[origin] ?? null;
|
|
}
|
|
const Nouns = {
|
|
regex: "input",
|
|
email: "email address",
|
|
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: "ISO datetime",
|
|
date: "ISO date",
|
|
time: "ISO time",
|
|
duration: "ISO duration",
|
|
ipv4: "IPv4 address",
|
|
ipv6: "IPv6 address",
|
|
cidrv4: "IPv4 range",
|
|
cidrv6: "IPv6 range",
|
|
base64: "base64-encoded string",
|
|
base64url: "base64url-encoded string",
|
|
json_string: "JSON string",
|
|
e164: "E.164 number",
|
|
jwt: "JWT",
|
|
template_literal: "input",
|
|
};
|
|
return (issue) => {
|
|
switch (issue.code) {
|
|
case "invalid_type":
|
|
return `Invalid input: expected ${issue.expected}, received ${parsedType(issue.input)}`;
|
|
case "invalid_value":
|
|
if (issue.values.length === 1)
|
|
return `Invalid input: expected ${util.stringifyPrimitive(issue.values[0])}`;
|
|
return `Invalid option: expected one of ${util.joinValues(issue.values, "|")}`;
|
|
case "too_big": {
|
|
const adj = issue.inclusive ? "<=" : "<";
|
|
const sizing = getSizing(issue.origin);
|
|
if (sizing)
|
|
return `Too big: expected ${issue.origin ?? "value"} to have ${adj}${issue.maximum.toString()} ${sizing.unit ?? "elements"}`;
|
|
return `Too big: expected ${issue.origin ?? "value"} to be ${adj}${issue.maximum.toString()}`;
|
|
}
|
|
case "too_small": {
|
|
const adj = issue.inclusive ? ">=" : ">";
|
|
const sizing = getSizing(issue.origin);
|
|
if (sizing) {
|
|
return `Too small: expected ${issue.origin} to have ${adj}${issue.minimum.toString()} ${sizing.unit}`;
|
|
}
|
|
return `Too small: expected ${issue.origin} to be ${adj}${issue.minimum.toString()}`;
|
|
}
|
|
case "invalid_format": {
|
|
const _issue = issue;
|
|
if (_issue.format === "starts_with") {
|
|
return `Invalid string: must start with "${_issue.prefix}"`;
|
|
}
|
|
if (_issue.format === "ends_with")
|
|
return `Invalid string: must end with "${_issue.suffix}"`;
|
|
if (_issue.format === "includes")
|
|
return `Invalid string: must include "${_issue.includes}"`;
|
|
if (_issue.format === "regex")
|
|
return `Invalid string: must match pattern ${_issue.pattern}`;
|
|
return `Invalid ${Nouns[_issue.format] ?? issue.format}`;
|
|
}
|
|
case "not_multiple_of":
|
|
return `Invalid number: must be a multiple of ${issue.divisor}`;
|
|
case "unrecognized_keys":
|
|
return `Unrecognized key${issue.keys.length > 1 ? "s" : ""}: ${util.joinValues(issue.keys, ", ")}`;
|
|
case "invalid_key":
|
|
return `Invalid key in ${issue.origin}`;
|
|
case "invalid_union":
|
|
return "Invalid input";
|
|
case "invalid_element":
|
|
return `Invalid value in ${issue.origin}`;
|
|
default:
|
|
return `Invalid input`;
|
|
}
|
|
};
|
|
};
|
|
export default function () {
|
|
return {
|
|
localeError: error(),
|
|
};
|
|
}
|