@imcms/core 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -1
- package/src/formatZodError.ts +35 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imcms/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -31,6 +31,9 @@
|
|
|
31
31
|
"tsx": "^4.23.15",
|
|
32
32
|
"typescript": "^5.9.3"
|
|
33
33
|
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"zod": "^4.6.7"
|
|
36
|
+
},
|
|
34
37
|
"scripts": {
|
|
35
38
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
36
39
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ZodError, ZodIssue } from "zod";
|
|
2
|
+
|
|
3
|
+
export interface FormatZodErrorOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Prefixo exibido no início de cada linha de erro.
|
|
6
|
+
* @default " • "
|
|
7
|
+
*/
|
|
8
|
+
prefix?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Formata um erro do Zod (ZodError ou array de ZodIssue) em uma string legível para logs e terminal.
|
|
13
|
+
*
|
|
14
|
+
* Exemplo de saída:
|
|
15
|
+
* • [PORT]: Expected number, received string
|
|
16
|
+
* • [DATABASE_URL]: Required
|
|
17
|
+
*/
|
|
18
|
+
export function formatZodError(
|
|
19
|
+
error: ZodError | ZodIssue[],
|
|
20
|
+
options: FormatZodErrorOptions = {}
|
|
21
|
+
): string {
|
|
22
|
+
const { prefix = " • " } = options;
|
|
23
|
+
const issues = Array.isArray(error) ? error : error.issues;
|
|
24
|
+
|
|
25
|
+
if (!issues || issues.length === 0) {
|
|
26
|
+
return `${prefix}Erro de validação desconhecido.`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return issues
|
|
30
|
+
.map((issue) => {
|
|
31
|
+
const path = issue.path.length > 0 ? issue.path.join(".") : "ENV";
|
|
32
|
+
return `${prefix}[${path}]: ${issue.message}`;
|
|
33
|
+
})
|
|
34
|
+
.join("\n");
|
|
35
|
+
}
|