@nice-code/common-errors 0.8.0 → 0.10.0
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/build/hono/index.d.ts +14 -0
- package/build/hono/index.js +32 -96
- package/build/hono/index.js.map +1 -0
- package/build/index.d.ts +24 -0
- package/build/index.js +2 -60
- package/build/validation-788jnLm7.js +37 -0
- package/build/validation-788jnLm7.js.map +1 -0
- package/build/validation-DeF-MFLS.js +54 -0
- package/build/validation-DeF-MFLS.js.map +1 -0
- package/package.json +6 -5
- package/build/types/hono/index.d.ts +0 -2
- package/build/types/hono/niceCatchSValidation.d.ts +0 -2
- package/build/types/hono/niceSValidator.d.ts +0 -6
- package/build/types/index.d.ts +0 -1
- package/build/types/validation/err_validation.d.ts +0 -11
- package/build/types/validation/err_validation.types.d.ts +0 -4
- package/build/types/validation/index.d.ts +0 -2
- package/build/types/validation/standard_schema/extractMessageFromStandardSchema.d.ts +0 -2
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MiddlewareHandler, ValidationTargets } from "hono";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/hono/niceCatchSValidation.d.ts
|
|
5
|
+
declare const niceCatchSValidation: () => MiddlewareHandler;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/hono/niceSValidator.d.ts
|
|
8
|
+
declare function niceSValidator<Schema extends StandardSchemaV1, Target extends keyof ValidationTargets>(target: Target, schema: Schema): import("hono").Handler<import("hono").Env, string, {
|
|
9
|
+
in: (undefined extends StandardSchemaV1.InferInput<Schema> ? true : false) extends true ? { [K in Target]?: (StandardSchemaV1.InferInput<Schema> extends infer T ? T extends StandardSchemaV1.InferInput<Schema> ? T extends ValidationTargets[K] ? T : { [K2 in keyof T]?: ValidationTargets[K][K2] | undefined } : never : never) | undefined } : { [K_1 in Target]: StandardSchemaV1.InferInput<Schema> extends infer T ? T extends StandardSchemaV1.InferInput<Schema> ? T extends ValidationTargets[K_1] ? T : { [K2 in keyof T]: ValidationTargets[K_1][K2] } : never : never };
|
|
10
|
+
out: { [K_2 in Target]: StandardSchemaV1.InferOutput<Schema> };
|
|
11
|
+
}, never>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { niceCatchSValidation, niceSValidator };
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
package/build/hono/index.js
CHANGED
|
@@ -1,99 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
import { err, err_nice } from "@nice-code/error";
|
|
3
|
-
import { StatusCodes } from "http-status-codes";
|
|
4
|
-
|
|
5
|
-
// src/validation/standard_schema/extractMessageFromStandardSchema.ts
|
|
6
|
-
function extractPathFromIssue(issue) {
|
|
7
|
-
let pathString = "";
|
|
8
|
-
for (const segment of issue) {
|
|
9
|
-
if (typeof segment === "object") {
|
|
10
|
-
if (segment.key != null) {
|
|
11
|
-
if (typeof segment.key === "number") {
|
|
12
|
-
pathString += `[${String(segment.key)}]`;
|
|
13
|
-
} else if (typeof segment.key === "symbol") {
|
|
14
|
-
pathString += `[SYMBOL:${String(segment.key)}]`;
|
|
15
|
-
} else {
|
|
16
|
-
pathString += `.${String(segment.key)}`;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
} else {
|
|
20
|
-
pathString += `.${String(segment)}`;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return pathString.slice(1);
|
|
24
|
-
}
|
|
25
|
-
var extractMessageFromStandardSchema = (failureResult) => {
|
|
26
|
-
let message = `Data validation failed:
|
|
27
|
-
`;
|
|
28
|
-
let issueCount = 0;
|
|
29
|
-
for (const issue of failureResult.issues) {
|
|
30
|
-
issueCount++;
|
|
31
|
-
if (issue.path == null || issue.path.length === 0) {
|
|
32
|
-
message += ` (issue ${issueCount}) ${issue.message}
|
|
33
|
-
`;
|
|
34
|
-
} else {
|
|
35
|
-
message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}
|
|
36
|
-
`;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return message;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// src/validation/err_validation.ts
|
|
43
|
-
var EValidator;
|
|
44
|
-
((EValidator2) => {
|
|
45
|
-
EValidator2["standard_schema"] = "standard_schema";
|
|
46
|
-
})(EValidator ||= {});
|
|
47
|
-
var err_validation = err_nice.createChildDomain({
|
|
48
|
-
domain: "err_validation",
|
|
49
|
-
defaultHttpStatusCode: StatusCodes.BAD_REQUEST,
|
|
50
|
-
schema: {
|
|
51
|
-
["standard_schema" /* standard_schema */]: err({
|
|
52
|
-
message: ({ issues }) => extractMessageFromStandardSchema({ issues })
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
// src/hono/niceCatchSValidation.ts
|
|
57
|
-
var niceCatchSValidation = () => async (ctx, next) => {
|
|
58
|
-
await next();
|
|
59
|
-
if (!ctx.res.ok) {
|
|
60
|
-
const clonedResponse = ctx.res.clone();
|
|
61
|
-
const contentType = clonedResponse.headers.get("content-type");
|
|
62
|
-
if (contentType?.includes("application/json")) {
|
|
63
|
-
try {
|
|
64
|
-
const responseJson = await clonedResponse.json();
|
|
65
|
-
if (responseJson["success"] != null && responseJson["error"] != null) {
|
|
66
|
-
console.log("Intercepted JSON:", responseJson);
|
|
67
|
-
const result = responseJson;
|
|
68
|
-
const newError = err_validation.fromId("standard_schema" /* standard_schema */, {
|
|
69
|
-
issues: result.error
|
|
70
|
-
});
|
|
71
|
-
ctx.res = undefined;
|
|
72
|
-
ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {
|
|
73
|
-
status: newError.httpStatusCode,
|
|
74
|
-
headers: {
|
|
75
|
-
"Content-Type": "application/json"
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
} catch (error) {
|
|
80
|
-
console.error("Failed to parse response JSON:", error);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
// src/hono/niceSValidator.ts
|
|
1
|
+
import { n as err_validation } from "../validation-788jnLm7.js";
|
|
86
2
|
import { sValidator } from "@hono/standard-validator";
|
|
3
|
+
//#region src/hono/niceCatchSValidation.ts
|
|
4
|
+
const niceCatchSValidation = () => async (ctx, next) => {
|
|
5
|
+
await next();
|
|
6
|
+
if (!ctx.res.ok) {
|
|
7
|
+
const clonedResponse = ctx.res.clone();
|
|
8
|
+
if (clonedResponse.headers.get("content-type")?.includes("application/json")) try {
|
|
9
|
+
const responseJson = await clonedResponse.json();
|
|
10
|
+
if (responseJson["success"] != null && responseJson["error"] != null) {
|
|
11
|
+
console.log("Intercepted JSON:", responseJson);
|
|
12
|
+
const result = responseJson;
|
|
13
|
+
const newError = err_validation.fromId("standard_schema", { issues: result.error });
|
|
14
|
+
ctx.res = void 0;
|
|
15
|
+
ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {
|
|
16
|
+
status: newError.httpStatusCode,
|
|
17
|
+
headers: { "Content-Type": "application/json" }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("Failed to parse response JSON:", error);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/hono/niceSValidator.ts
|
|
87
27
|
function niceSValidator(target, schema) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
issues: result.error
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
});
|
|
28
|
+
return sValidator(target, schema, (result) => {
|
|
29
|
+
if (!result.success) throw err_validation.fromId("standard_schema", { issues: result.error });
|
|
30
|
+
});
|
|
95
31
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
32
|
+
//#endregion
|
|
33
|
+
export { niceCatchSValidation, niceSValidator };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/hono/niceCatchSValidation.ts","../../src/hono/niceSValidator.ts"],"sourcesContent":["import type { MiddlewareHandler } from \"hono\";\nimport type { ContentfulStatusCode } from \"hono/utils/http-status\";\nimport { EValidator, err_validation } from \"../validation\";\nimport type { IErrContext_HonoStandardSchema } from \"../validation/err_validation.types\";\n\nexport const niceCatchSValidation = (): MiddlewareHandler => async (ctx, next) => {\n // 1. Execute downstream routes and wait for them to finish\n await next();\n\n if (!ctx.res.ok) {\n // 2. Clone the response so we don't consume the original body stream\n const clonedResponse = ctx.res.clone();\n\n // 3. Safely check if the response is actually JSON\n const contentType = clonedResponse.headers.get(\"content-type\");\n\n if (contentType?.includes(\"application/json\")) {\n try {\n // 4. Parse the JSON from the cloned response\n const responseJson = (await clonedResponse.json()) as any;\n\n if (responseJson[\"success\"] != null && responseJson[\"error\"] != null) {\n console.log(\"Intercepted JSON:\", responseJson);\n const result = responseJson as {\n success: false;\n error: IErrContext_HonoStandardSchema[\"issues\"];\n data: unknown;\n };\n const newError = err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n ctx.res = undefined;\n ctx.res = new Response(JSON.stringify(newError.toJsonObject()), {\n status: newError.httpStatusCode as ContentfulStatusCode,\n headers: {\n \"Content-Type\": \"application/json\",\n },\n });\n }\n } catch (error) {\n console.error(\"Failed to parse response JSON:\", error);\n }\n }\n }\n};\n","import { sValidator } from \"@hono/standard-validator\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { ValidationTargets } from \"hono\";\nimport { EValidator, err_validation } from \"../validation/err_validation\";\n\nexport function niceSValidator<\n Schema extends StandardSchemaV1,\n Target extends keyof ValidationTargets,\n>(target: Target, schema: Schema) {\n return sValidator(target, schema, (result) => {\n if (!result.success) {\n throw err_validation.fromId(EValidator.standard_schema, {\n issues: result.error,\n });\n }\n });\n}\n"],"mappings":";;;AAKA,MAAa,6BAAgD,OAAO,KAAK,SAAS;CAEhF,MAAM,KAAK;CAEX,IAAI,CAAC,IAAI,IAAI,IAAI;EAEf,MAAM,iBAAiB,IAAI,IAAI,MAAM;EAKrC,IAFoB,eAAe,QAAQ,IAAI,cAEjC,CAAC,EAAE,SAAS,kBAAkB,GAC1C,IAAI;GAEF,MAAM,eAAgB,MAAM,eAAe,KAAK;GAEhD,IAAI,aAAa,cAAc,QAAQ,aAAa,YAAY,MAAM;IACpE,QAAQ,IAAI,qBAAqB,YAAY;IAC7C,MAAM,SAAS;IAKf,MAAM,WAAW,eAAe,OAAA,mBAAmC,EACjE,QAAQ,OAAO,MACjB,CAAC;IACD,IAAI,MAAM,KAAA;IACV,IAAI,MAAM,IAAI,SAAS,KAAK,UAAU,SAAS,aAAa,CAAC,GAAG;KAC9D,QAAQ,SAAS;KACjB,SAAS,EACP,gBAAgB,mBAClB;IACF,CAAC;GACH;EACF,SAAS,OAAO;GACd,QAAQ,MAAM,kCAAkC,KAAK;EACvD;CAEJ;AACF;;;ACvCA,SAAgB,eAGd,QAAgB,QAAgB;CAChC,OAAO,WAAW,QAAQ,SAAS,WAAW;EAC5C,IAAI,CAAC,OAAO,SACV,MAAM,eAAe,OAAA,mBAAmC,EACtD,QAAQ,OAAO,MACjB,CAAC;CAEL,CAAC;AACH"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
|
|
3
|
+
//#region src/validation/err_validation.types.d.ts
|
|
4
|
+
interface IErrContext_HonoStandardSchema {
|
|
5
|
+
issues: readonly StandardSchemaV1.Issue[];
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/validation/err_validation.d.ts
|
|
9
|
+
declare enum EValidator {
|
|
10
|
+
standard_schema = "standard_schema"
|
|
11
|
+
}
|
|
12
|
+
declare const err_validation: import("@nice-code/error").NiceErrorDefined<{
|
|
13
|
+
domain: string;
|
|
14
|
+
allDomains: [string, "err_nice"];
|
|
15
|
+
schema: {
|
|
16
|
+
standard_schema: import("@nice-code/error").INiceErrorIdMetadata<IErrContext_HonoStandardSchema, import("@nice-code/error").JSONSerializableValue>;
|
|
17
|
+
};
|
|
18
|
+
}>;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/validation/standard_schema/extractMessageFromStandardSchema.d.ts
|
|
21
|
+
declare const extractMessageFromStandardSchema: (failureResult: StandardSchemaV1.FailureResult) => string;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { EValidator, err_validation, extractMessageFromStandardSchema };
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
package/build/index.js
CHANGED
|
@@ -1,60 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { StatusCodes } from "http-status-codes";
|
|
4
|
-
|
|
5
|
-
// src/validation/standard_schema/extractMessageFromStandardSchema.ts
|
|
6
|
-
function extractPathFromIssue(issue) {
|
|
7
|
-
let pathString = "";
|
|
8
|
-
for (const segment of issue) {
|
|
9
|
-
if (typeof segment === "object") {
|
|
10
|
-
if (segment.key != null) {
|
|
11
|
-
if (typeof segment.key === "number") {
|
|
12
|
-
pathString += `[${String(segment.key)}]`;
|
|
13
|
-
} else if (typeof segment.key === "symbol") {
|
|
14
|
-
pathString += `[SYMBOL:${String(segment.key)}]`;
|
|
15
|
-
} else {
|
|
16
|
-
pathString += `.${String(segment.key)}`;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
} else {
|
|
20
|
-
pathString += `.${String(segment)}`;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return pathString.slice(1);
|
|
24
|
-
}
|
|
25
|
-
var extractMessageFromStandardSchema = (failureResult) => {
|
|
26
|
-
let message = `Data validation failed:
|
|
27
|
-
`;
|
|
28
|
-
let issueCount = 0;
|
|
29
|
-
for (const issue of failureResult.issues) {
|
|
30
|
-
issueCount++;
|
|
31
|
-
if (issue.path == null || issue.path.length === 0) {
|
|
32
|
-
message += ` (issue ${issueCount}) ${issue.message}
|
|
33
|
-
`;
|
|
34
|
-
} else {
|
|
35
|
-
message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}
|
|
36
|
-
`;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return message;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// src/validation/err_validation.ts
|
|
43
|
-
var EValidator;
|
|
44
|
-
((EValidator2) => {
|
|
45
|
-
EValidator2["standard_schema"] = "standard_schema";
|
|
46
|
-
})(EValidator ||= {});
|
|
47
|
-
var err_validation = err_nice.createChildDomain({
|
|
48
|
-
domain: "err_validation",
|
|
49
|
-
defaultHttpStatusCode: StatusCodes.BAD_REQUEST,
|
|
50
|
-
schema: {
|
|
51
|
-
["standard_schema" /* standard_schema */]: err({
|
|
52
|
-
message: ({ issues }) => extractMessageFromStandardSchema({ issues })
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
export {
|
|
57
|
-
extractMessageFromStandardSchema,
|
|
58
|
-
err_validation,
|
|
59
|
-
EValidator
|
|
60
|
-
};
|
|
1
|
+
import { n as err_validation, r as extractMessageFromStandardSchema, t as EValidator } from "./validation-788jnLm7.js";
|
|
2
|
+
export { EValidator, err_validation, extractMessageFromStandardSchema };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { err, err_nice } from "@nice-code/error";
|
|
2
|
+
import { StatusCodes } from "http-status-codes";
|
|
3
|
+
//#region src/validation/standard_schema/extractMessageFromStandardSchema.ts
|
|
4
|
+
function extractPathFromIssue(issue) {
|
|
5
|
+
let pathString = "";
|
|
6
|
+
for (const segment of issue) if (typeof segment === "object") {
|
|
7
|
+
if (segment.key != null) if (typeof segment.key === "number") pathString += `[${String(segment.key)}]`;
|
|
8
|
+
else if (typeof segment.key === "symbol") pathString += `[SYMBOL:${String(segment.key)}]`;
|
|
9
|
+
else pathString += `.${String(segment.key)}`;
|
|
10
|
+
} else pathString += `.${String(segment)}`;
|
|
11
|
+
return pathString.slice(1);
|
|
12
|
+
}
|
|
13
|
+
const extractMessageFromStandardSchema = (failureResult) => {
|
|
14
|
+
let message = "Data validation failed:\n";
|
|
15
|
+
let issueCount = 0;
|
|
16
|
+
for (const issue of failureResult.issues) {
|
|
17
|
+
issueCount++;
|
|
18
|
+
if (issue.path == null || issue.path.length === 0) message += ` (issue ${issueCount}) ${issue.message}\n`;
|
|
19
|
+
else message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\n`;
|
|
20
|
+
}
|
|
21
|
+
return message;
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/validation/err_validation.ts
|
|
25
|
+
let EValidator = /* @__PURE__ */ function(EValidator) {
|
|
26
|
+
EValidator["standard_schema"] = "standard_schema";
|
|
27
|
+
return EValidator;
|
|
28
|
+
}({});
|
|
29
|
+
const err_validation = err_nice.createChildDomain({
|
|
30
|
+
domain: "err_validation",
|
|
31
|
+
defaultHttpStatusCode: StatusCodes.BAD_REQUEST,
|
|
32
|
+
schema: { ["standard_schema"]: err({ message: ({ issues }) => extractMessageFromStandardSchema({ issues }) }) }
|
|
33
|
+
});
|
|
34
|
+
//#endregion
|
|
35
|
+
export { err_validation as n, extractMessageFromStandardSchema as r, EValidator as t };
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=validation-788jnLm7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation-788jnLm7.js","names":[],"sources":["../src/validation/standard_schema/extractMessageFromStandardSchema.ts","../src/validation/err_validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\nfunction extractPathFromIssue(\n issue: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>,\n): string {\n let pathString = \"\";\n\n for (const segment of issue) {\n if (typeof segment === \"object\") {\n if (segment.key != null) {\n if (typeof segment.key === \"number\") {\n pathString += `[${String(segment.key)}]`;\n } else if (typeof segment.key === \"symbol\") {\n pathString += `[SYMBOL:${String(segment.key)}]`;\n } else {\n pathString += `.${String(segment.key)}`;\n }\n }\n } else {\n pathString += `.${String(segment)}`;\n }\n }\n\n return pathString.slice(1); // Remove the leading dot\n}\n\nexport const extractMessageFromStandardSchema = (\n failureResult: StandardSchemaV1.FailureResult,\n): string => {\n let message = \"Data validation failed:\\n\";\n let issueCount = 0;\n\n for (const issue of failureResult.issues) {\n issueCount++;\n if (issue.path == null || issue.path.length === 0) {\n message += ` (issue ${issueCount}) ${issue.message}\\n`;\n } else {\n message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\\n`;\n }\n }\n\n return message;\n};\n","import { err, err_nice } from \"@nice-code/error\";\nimport { StatusCodes } from \"http-status-codes\";\nimport type { IErrContext_HonoStandardSchema } from \"./err_validation.types\";\nimport { extractMessageFromStandardSchema } from \"./standard_schema/extractMessageFromStandardSchema\";\n\nexport enum EValidator {\n standard_schema = \"standard_schema\",\n}\n\nexport const err_validation = err_nice.createChildDomain({\n domain: \"err_validation\",\n defaultHttpStatusCode: StatusCodes.BAD_REQUEST,\n schema: {\n [EValidator.standard_schema]: err<IErrContext_HonoStandardSchema>({\n message: ({ issues }) => extractMessageFromStandardSchema({ issues }),\n }),\n },\n});\n"],"mappings":";;;AAEA,SAAS,qBACP,OACQ;CACR,IAAI,aAAa;CAEjB,KAAK,MAAM,WAAW,OACpB,IAAI,OAAO,YAAY;MACjB,QAAQ,OAAO,MACjB,IAAI,OAAO,QAAQ,QAAQ,UACzB,cAAc,IAAI,OAAO,QAAQ,GAAG,EAAE;OACjC,IAAI,OAAO,QAAQ,QAAQ,UAChC,cAAc,WAAW,OAAO,QAAQ,GAAG,EAAE;OAE7C,cAAc,IAAI,OAAO,QAAQ,GAAG;CAAA,OAIxC,cAAc,IAAI,OAAO,OAAO;CAIpC,OAAO,WAAW,MAAM,CAAC;AAC3B;AAEA,MAAa,oCACX,kBACW;CACX,IAAI,UAAU;CACd,IAAI,aAAa;CAEjB,KAAK,MAAM,SAAS,cAAc,QAAQ;EACxC;EACA,IAAI,MAAM,QAAQ,QAAQ,MAAM,KAAK,WAAW,GAC9C,WAAW,WAAW,WAAW,IAAI,MAAM,QAAQ;OAEnD,WAAW,WAAW,WAAW,KAAK,qBAAqB,MAAM,IAAI,EAAE,KAAK,MAAM,QAAQ;CAE9F;CAEA,OAAO;AACT;;;ACrCA,IAAY,aAAL,yBAAA,YAAA;CACL,WAAA,qBAAA;;AACF,EAAA,CAAA,CAAA;AAEA,MAAa,iBAAiB,SAAS,kBAAkB;CACvD,QAAQ;CACR,uBAAuB,YAAY;CACnC,QAAQ,GAAA,oBACwB,IAAoC,EAChE,UAAU,EAAE,aAAa,iCAAiC,EAAE,OAAO,CAAC,EACtE,CAAC,EACH;AACF,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
let _nice_code_error = require("@nice-code/error");
|
|
2
|
+
let http_status_codes = require("http-status-codes");
|
|
3
|
+
//#region src/validation/standard_schema/extractMessageFromStandardSchema.ts
|
|
4
|
+
function extractPathFromIssue(issue) {
|
|
5
|
+
let pathString = "";
|
|
6
|
+
for (const segment of issue) if (typeof segment === "object") {
|
|
7
|
+
if (segment.key != null) if (typeof segment.key === "number") pathString += `[${String(segment.key)}]`;
|
|
8
|
+
else if (typeof segment.key === "symbol") pathString += `[SYMBOL:${String(segment.key)}]`;
|
|
9
|
+
else pathString += `.${String(segment.key)}`;
|
|
10
|
+
} else pathString += `.${String(segment)}`;
|
|
11
|
+
return pathString.slice(1);
|
|
12
|
+
}
|
|
13
|
+
const extractMessageFromStandardSchema = (failureResult) => {
|
|
14
|
+
let message = "Data validation failed:\n";
|
|
15
|
+
let issueCount = 0;
|
|
16
|
+
for (const issue of failureResult.issues) {
|
|
17
|
+
issueCount++;
|
|
18
|
+
if (issue.path == null || issue.path.length === 0) message += ` (issue ${issueCount}) ${issue.message}\n`;
|
|
19
|
+
else message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\n`;
|
|
20
|
+
}
|
|
21
|
+
return message;
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/validation/err_validation.ts
|
|
25
|
+
let EValidator = /* @__PURE__ */ function(EValidator) {
|
|
26
|
+
EValidator["standard_schema"] = "standard_schema";
|
|
27
|
+
return EValidator;
|
|
28
|
+
}({});
|
|
29
|
+
const err_validation = _nice_code_error.err_nice.createChildDomain({
|
|
30
|
+
domain: "err_validation",
|
|
31
|
+
defaultHttpStatusCode: http_status_codes.StatusCodes.BAD_REQUEST,
|
|
32
|
+
schema: { ["standard_schema"]: (0, _nice_code_error.err)({ message: ({ issues }) => extractMessageFromStandardSchema({ issues }) }) }
|
|
33
|
+
});
|
|
34
|
+
//#endregion
|
|
35
|
+
Object.defineProperty(exports, "EValidator", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function() {
|
|
38
|
+
return EValidator;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(exports, "err_validation", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
get: function() {
|
|
44
|
+
return err_validation;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(exports, "extractMessageFromStandardSchema", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function() {
|
|
50
|
+
return extractMessageFromStandardSchema;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=validation-DeF-MFLS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation-DeF-MFLS.js","names":["err_nice","StatusCodes"],"sources":["../src/validation/standard_schema/extractMessageFromStandardSchema.ts","../src/validation/err_validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\nfunction extractPathFromIssue(\n issue: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>,\n): string {\n let pathString = \"\";\n\n for (const segment of issue) {\n if (typeof segment === \"object\") {\n if (segment.key != null) {\n if (typeof segment.key === \"number\") {\n pathString += `[${String(segment.key)}]`;\n } else if (typeof segment.key === \"symbol\") {\n pathString += `[SYMBOL:${String(segment.key)}]`;\n } else {\n pathString += `.${String(segment.key)}`;\n }\n }\n } else {\n pathString += `.${String(segment)}`;\n }\n }\n\n return pathString.slice(1); // Remove the leading dot\n}\n\nexport const extractMessageFromStandardSchema = (\n failureResult: StandardSchemaV1.FailureResult,\n): string => {\n let message = \"Data validation failed:\\n\";\n let issueCount = 0;\n\n for (const issue of failureResult.issues) {\n issueCount++;\n if (issue.path == null || issue.path.length === 0) {\n message += ` (issue ${issueCount}) ${issue.message}\\n`;\n } else {\n message += ` (issue ${issueCount}) [${extractPathFromIssue(issue.path)}]: ${issue.message}\\n`;\n }\n }\n\n return message;\n};\n","import { err, err_nice } from \"@nice-code/error\";\nimport { StatusCodes } from \"http-status-codes\";\nimport type { IErrContext_HonoStandardSchema } from \"./err_validation.types\";\nimport { extractMessageFromStandardSchema } from \"./standard_schema/extractMessageFromStandardSchema\";\n\nexport enum EValidator {\n standard_schema = \"standard_schema\",\n}\n\nexport const err_validation = err_nice.createChildDomain({\n domain: \"err_validation\",\n defaultHttpStatusCode: StatusCodes.BAD_REQUEST,\n schema: {\n [EValidator.standard_schema]: err<IErrContext_HonoStandardSchema>({\n message: ({ issues }) => extractMessageFromStandardSchema({ issues }),\n }),\n },\n});\n"],"mappings":";;;AAEA,SAAS,qBACP,OACQ;CACR,IAAI,aAAa;CAEjB,KAAK,MAAM,WAAW,OACpB,IAAI,OAAO,YAAY;MACjB,QAAQ,OAAO,MACjB,IAAI,OAAO,QAAQ,QAAQ,UACzB,cAAc,IAAI,OAAO,QAAQ,GAAG,EAAE;OACjC,IAAI,OAAO,QAAQ,QAAQ,UAChC,cAAc,WAAW,OAAO,QAAQ,GAAG,EAAE;OAE7C,cAAc,IAAI,OAAO,QAAQ,GAAG;CAAA,OAIxC,cAAc,IAAI,OAAO,OAAO;CAIpC,OAAO,WAAW,MAAM,CAAC;AAC3B;AAEA,MAAa,oCACX,kBACW;CACX,IAAI,UAAU;CACd,IAAI,aAAa;CAEjB,KAAK,MAAM,SAAS,cAAc,QAAQ;EACxC;EACA,IAAI,MAAM,QAAQ,QAAQ,MAAM,KAAK,WAAW,GAC9C,WAAW,WAAW,WAAW,IAAI,MAAM,QAAQ;OAEnD,WAAW,WAAW,WAAW,KAAK,qBAAqB,MAAM,IAAI,EAAE,KAAK,MAAM,QAAQ;CAE9F;CAEA,OAAO;AACT;;;ACrCA,IAAY,aAAL,yBAAA,YAAA;CACL,WAAA,qBAAA;;AACF,EAAA,CAAA,CAAA;AAEA,MAAa,iBAAiBA,iBAAAA,SAAS,kBAAkB;CACvD,QAAQ;CACR,uBAAuBC,kBAAAA,YAAY;CACnC,QAAQ,GAAA,qBAAA,GAAA,iBAAA,IAAA,CAC4D,EAChE,UAAU,EAAE,aAAa,iCAAiC,EAAE,OAAO,CAAC,EACtE,CAAC,EACH;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nice-code/common-errors",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
8
|
"source": "./src/index.ts",
|
|
9
|
-
"types": "./build/
|
|
9
|
+
"types": "./build/index.d.ts",
|
|
10
10
|
"import": "./build/index.js"
|
|
11
11
|
},
|
|
12
12
|
"./hono": {
|
|
13
13
|
"source": "./src/hono/index.ts",
|
|
14
|
-
"types": "./build/
|
|
14
|
+
"types": "./build/hono/index.d.ts",
|
|
15
15
|
"import": "./build/hono/index.js"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
@@ -27,11 +27,12 @@
|
|
|
27
27
|
"type-check": "bunx tsc --noEmit",
|
|
28
28
|
"type-check-watch": "bunx tsc --noEmit --watch",
|
|
29
29
|
"clean-build": "bunx rimraf build",
|
|
30
|
-
"build": "bun run clean-build && bun run build.ts && bun run build-types",
|
|
30
|
+
"build-bun": "bun run clean-build && bun run build.ts && bun run build-types",
|
|
31
|
+
"build": "bun run clean-build && tsdown",
|
|
31
32
|
"build-types": "tsc --project tsconfig.build.json"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@nice-code/error": "0.
|
|
35
|
+
"@nice-code/error": "0.10.0",
|
|
35
36
|
"@standard-schema/spec": "^1.1.0",
|
|
36
37
|
"@hono/standard-validator": "^0.2.2",
|
|
37
38
|
"http-status-codes": "^2.3.0"
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
-
import type { ValidationTargets } from "hono";
|
|
3
|
-
export declare function niceSValidator<Schema extends StandardSchemaV1, Target extends keyof ValidationTargets>(target: Target, schema: Schema): import("hono").Handler<import("hono").Env, string, {
|
|
4
|
-
in: (undefined extends StandardSchemaV1.InferInput<Schema> ? true : false) extends true ? { [K in Target]?: (StandardSchemaV1.InferInput<Schema> extends infer T ? T extends StandardSchemaV1.InferInput<Schema> ? T extends ValidationTargets[K] ? T : { [K2 in keyof T]?: ValidationTargets[K][K2] | undefined; } : never : never) | undefined; } : { [K_1 in Target]: StandardSchemaV1.InferInput<Schema> extends infer T ? T extends StandardSchemaV1.InferInput<Schema> ? T extends ValidationTargets[K_1] ? T : { [K2 in keyof T]: ValidationTargets[K_1][K2]; } : never : never; };
|
|
5
|
-
out: { [K_2 in Target]: StandardSchemaV1.InferOutput<Schema>; };
|
|
6
|
-
}, never>;
|
package/build/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./validation";
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { IErrContext_HonoStandardSchema } from "./err_validation.types";
|
|
2
|
-
export declare enum EValidator {
|
|
3
|
-
standard_schema = "standard_schema"
|
|
4
|
-
}
|
|
5
|
-
export declare const err_validation: import("@nice-code/error").NiceErrorDomain<{
|
|
6
|
-
domain: string;
|
|
7
|
-
allDomains: [string, "err_nice"];
|
|
8
|
-
schema: {
|
|
9
|
-
standard_schema: import("@nice-code/error").INiceErrorIdMetadata<IErrContext_HonoStandardSchema, import("@nice-code/error").JSONSerializableValue>;
|
|
10
|
-
};
|
|
11
|
-
}>;
|