src/lib/format-error.ts 809 B raw
1
import * as Evolu from "@evolu/common";
2
3
/**
4
 * Formats Evolu Type errors into user-friendly messages.
5
 *
6
 * Evolu Type typed errors ensure every error type used in schema must have a
7
 * formatter. TypeScript enforces this at compile-time, preventing unhandled
8
 * validation errors from reaching users.
9
 *
10
 * The `createFormatTypeError` function handles both built-in and custom errors,
11
 * and lets us override default formatting for specific errors.
12
 */
13
export const formatTypeError = Evolu.createFormatTypeError<
14
	Evolu.MinLengthError | Evolu.MaxLengthError
15
>((error): string => {
16
	switch (error.type) {
17
		case "MinLength":
18
			return `Text must be at least ${error.min} character${error.min === 1 ? "" : "s"} long`;
19
		case "MaxLength":
20
			return `Text is too long (maximum ${error.max} characters)`;
21
	}
22
});