@jderstd/hono-standard-validator 0.1.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/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/_virtual/rolldown_runtime.js +23 -0
- package/dist/hook.d.ts +15 -0
- package/dist/hook.js +30 -0
- package/dist/hook.js.map +1 -0
- package/dist/hook.mjs +26 -0
- package/dist/hook.mjs.map +1 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +71 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +69 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present, Alpheus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# JDER Hono Standard Validator
|
|
2
|
+
|
|
3
|
+
A standard validator for Hono.
|
|
4
|
+
|
|
5
|
+
This package includes a standard validator based on the JSON response structure specified in [JSON Data Errors Response (JDER)](https://github.com/jderstd/spec). With the validator, various kinds of requests can be validated easily and send JSON responses instead of plain text responses.
|
|
6
|
+
|
|
7
|
+
## License
|
|
8
|
+
|
|
9
|
+
This project is licensed under the terms of the MIT license.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __copyProps = (to, from, except, desc) => {
|
|
8
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
9
|
+
key = keys[i];
|
|
10
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
11
|
+
get: ((k) => from[k]).bind(null, key),
|
|
12
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
18
|
+
value: mod,
|
|
19
|
+
enumerable: true
|
|
20
|
+
}) : target, mod));
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
exports.__toESM = __toESM;
|
package/dist/hook.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { Context } from "hono";
|
|
3
|
+
import { Env, TypedResponse, ValidationTargets as ValidationTargets$1 } from "hono/types";
|
|
4
|
+
declare const sValidatorHook: <T, E extends Env, P extends string, Target extends keyof ValidationTargets$1 = keyof ValidationTargets$1, O = {}>(result: ({
|
|
5
|
+
success: true;
|
|
6
|
+
data: T;
|
|
7
|
+
} | {
|
|
8
|
+
success: false;
|
|
9
|
+
error: readonly StandardSchemaV1.Issue[];
|
|
10
|
+
data: T;
|
|
11
|
+
}) & {
|
|
12
|
+
target: Target;
|
|
13
|
+
}, c: Context<E, P>) => Response | void | TypedResponse<O> | Promise<Response | void | TypedResponse<O>>;
|
|
14
|
+
export { sValidatorHook };
|
|
15
|
+
//# sourceMappingURL=hook.d.ts.map
|
package/dist/hook.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
|
+
let __jderstd_hono_response = require("@jderstd/hono/response");
|
|
3
|
+
__jderstd_hono_response = require_rolldown_runtime.__toESM(__jderstd_hono_response);
|
|
4
|
+
let __jderstd_hono_response_error = require("@jderstd/hono/response/error");
|
|
5
|
+
__jderstd_hono_response_error = require_rolldown_runtime.__toESM(__jderstd_hono_response_error);
|
|
6
|
+
let hono_http_exception = require("hono/http-exception");
|
|
7
|
+
hono_http_exception = require_rolldown_runtime.__toESM(hono_http_exception);
|
|
8
|
+
|
|
9
|
+
const sValidatorHook = (result, c) => {
|
|
10
|
+
if (result.success) return void 0;
|
|
11
|
+
const errors = [];
|
|
12
|
+
const errs = result.error;
|
|
13
|
+
for (let i = 0; i < errs.length; i++) {
|
|
14
|
+
const err = errs[i];
|
|
15
|
+
if (!err) continue;
|
|
16
|
+
const path = err.path?.map((p) => {
|
|
17
|
+
if (typeof p === "object") return p.key.toString();
|
|
18
|
+
return p.toString();
|
|
19
|
+
}) ?? [];
|
|
20
|
+
errors.push({
|
|
21
|
+
code: __jderstd_hono_response_error.ResponseErrorCode.Parse,
|
|
22
|
+
path,
|
|
23
|
+
message: err.message
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
throw new hono_http_exception.HTTPException(400, { res: (0, __jderstd_hono_response.createJsonResponse)(c, { errors }) });
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
exports.sValidatorHook = sValidatorHook;
|
|
30
|
+
//# sourceMappingURL=hook.js.map
|
package/dist/hook.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hook.js","names":["errors: JsonResponseError[]","errs: readonly StandardSchemaV1.Issue[]","i: number","err: StandardSchemaV1.Issue | undefined","path: string[]","ResponseErrorCode","HTTPException"],"sources":["../src/hook.ts"],"sourcesContent":["import type { JsonResponseError } from \"@jderstd/hono/response\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport type { Env, TypedResponse, ValidationTargets } from \"hono/types\";\n\nimport { createJsonResponse } from \"@jderstd/hono/response\";\nimport { ResponseErrorCode } from \"@jderstd/hono/response/error\";\nimport { HTTPException } from \"hono/http-exception\";\n\nconst sValidatorHook = <\n T,\n E extends Env,\n P extends string,\n Target extends keyof ValidationTargets = keyof ValidationTargets,\n // biome-ignore lint/complexity/noBannedTypes: any non-nullable value\n O = {},\n>(\n result: (\n | {\n success: true;\n data: T;\n }\n | {\n success: false;\n error: readonly StandardSchemaV1.Issue[];\n data: T;\n }\n ) & {\n target: Target;\n },\n c: Context<E, P>,\n):\n | Response\n | void\n | TypedResponse<O>\n // biome-ignore lint/suspicious/noConfusingVoidType: return type\n | Promise<Response | void | TypedResponse<O>> => {\n if (result.success) return void 0;\n\n const errors: JsonResponseError[] = [];\n\n const errs: readonly StandardSchemaV1.Issue[] = result.error;\n\n for (let i: number = 0; i < errs.length; i++) {\n const err: StandardSchemaV1.Issue | undefined = errs[i];\n\n if (!err) continue;\n\n const path: string[] =\n err.path?.map((p) => {\n if (typeof p === \"object\") return p.key.toString();\n return p.toString();\n }) ?? [];\n\n errors.push({\n code: ResponseErrorCode.Parse,\n path,\n message: err.message,\n });\n }\n\n throw new HTTPException(400, {\n res: createJsonResponse(c, {\n errors,\n }),\n });\n};\n\nexport { sValidatorHook };\n"],"mappings":";;;;;;;;AASA,MAAM,kBAQF,QAaA,MAMiD;AACjD,KAAI,OAAO,QAAS,QAAO,KAAK;CAEhC,MAAMA,SAA8B,EAAE;CAEtC,MAAMC,OAA0C,OAAO;AAEvD,MAAK,IAAIC,IAAY,GAAG,IAAI,KAAK,QAAQ,KAAK;EAC1C,MAAMC,MAA0C,KAAK;AAErD,MAAI,CAAC,IAAK;EAEV,MAAMC,OACF,IAAI,MAAM,KAAK,MAAM;AACjB,OAAI,OAAO,MAAM,SAAU,QAAO,EAAE,IAAI,UAAU;AAClD,UAAO,EAAE,UAAU;IACrB,IAAI,EAAE;AAEZ,SAAO,KAAK;GACR,MAAMC,gDAAkB;GACxB;GACA,SAAS,IAAI;GAChB,CAAC;;AAGN,OAAM,IAAIC,kCAAc,KAAK,EACzB,qDAAwB,GAAG,EACvB,QACH,CAAC,EACL,CAAC"}
|
package/dist/hook.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createJsonResponse } from "@jderstd/hono/response";
|
|
2
|
+
import { ResponseErrorCode } from "@jderstd/hono/response/error";
|
|
3
|
+
import { HTTPException } from "hono/http-exception";
|
|
4
|
+
|
|
5
|
+
const sValidatorHook = (result, c) => {
|
|
6
|
+
if (result.success) return void 0;
|
|
7
|
+
const errors = [];
|
|
8
|
+
const errs = result.error;
|
|
9
|
+
for (let i = 0; i < errs.length; i++) {
|
|
10
|
+
const err = errs[i];
|
|
11
|
+
if (!err) continue;
|
|
12
|
+
const path = err.path?.map((p) => {
|
|
13
|
+
if (typeof p === "object") return p.key.toString();
|
|
14
|
+
return p.toString();
|
|
15
|
+
}) ?? [];
|
|
16
|
+
errors.push({
|
|
17
|
+
code: ResponseErrorCode.Parse,
|
|
18
|
+
path,
|
|
19
|
+
message: err.message
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
throw new HTTPException(400, { res: createJsonResponse(c, { errors }) });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { sValidatorHook };
|
|
26
|
+
//# sourceMappingURL=hook.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hook.mjs","names":["errors: JsonResponseError[]","errs: readonly StandardSchemaV1.Issue[]","i: number","err: StandardSchemaV1.Issue | undefined","path: string[]"],"sources":["../src/hook.ts"],"sourcesContent":["import type { JsonResponseError } from \"@jderstd/hono/response\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport type { Env, TypedResponse, ValidationTargets } from \"hono/types\";\n\nimport { createJsonResponse } from \"@jderstd/hono/response\";\nimport { ResponseErrorCode } from \"@jderstd/hono/response/error\";\nimport { HTTPException } from \"hono/http-exception\";\n\nconst sValidatorHook = <\n T,\n E extends Env,\n P extends string,\n Target extends keyof ValidationTargets = keyof ValidationTargets,\n // biome-ignore lint/complexity/noBannedTypes: any non-nullable value\n O = {},\n>(\n result: (\n | {\n success: true;\n data: T;\n }\n | {\n success: false;\n error: readonly StandardSchemaV1.Issue[];\n data: T;\n }\n ) & {\n target: Target;\n },\n c: Context<E, P>,\n):\n | Response\n | void\n | TypedResponse<O>\n // biome-ignore lint/suspicious/noConfusingVoidType: return type\n | Promise<Response | void | TypedResponse<O>> => {\n if (result.success) return void 0;\n\n const errors: JsonResponseError[] = [];\n\n const errs: readonly StandardSchemaV1.Issue[] = result.error;\n\n for (let i: number = 0; i < errs.length; i++) {\n const err: StandardSchemaV1.Issue | undefined = errs[i];\n\n if (!err) continue;\n\n const path: string[] =\n err.path?.map((p) => {\n if (typeof p === \"object\") return p.key.toString();\n return p.toString();\n }) ?? [];\n\n errors.push({\n code: ResponseErrorCode.Parse,\n path,\n message: err.message,\n });\n }\n\n throw new HTTPException(400, {\n res: createJsonResponse(c, {\n errors,\n }),\n });\n};\n\nexport { sValidatorHook };\n"],"mappings":";;;;AASA,MAAM,kBAQF,QAaA,MAMiD;AACjD,KAAI,OAAO,QAAS,QAAO,KAAK;CAEhC,MAAMA,SAA8B,EAAE;CAEtC,MAAMC,OAA0C,OAAO;AAEvD,MAAK,IAAIC,IAAY,GAAG,IAAI,KAAK,QAAQ,KAAK;EAC1C,MAAMC,MAA0C,KAAK;AAErD,MAAI,CAAC,IAAK;EAEV,MAAMC,OACF,IAAI,MAAM,KAAK,MAAM;AACjB,OAAI,OAAO,MAAM,SAAU,QAAO,EAAE,IAAI,UAAU;AAClD,UAAO,EAAE,UAAU;IACrB,IAAI,EAAE;AAEZ,SAAO,KAAK;GACR,MAAM,kBAAkB;GACxB;GACA,SAAS,IAAI;GAChB,CAAC;;AAGN,OAAM,IAAI,cAAc,KAAK,EACzB,KAAK,mBAAmB,GAAG,EACvB,QACH,CAAC,EACL,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import * as hono0 from "hono";
|
|
3
|
+
import { ValidationTargets } from "hono";
|
|
4
|
+
/**
|
|
5
|
+
* Validate the request with validator based on Standard Schema.
|
|
6
|
+
*
|
|
7
|
+
* Following error may returned if the request is invalid:
|
|
8
|
+
*
|
|
9
|
+
* ```jsonc
|
|
10
|
+
* // Status: 400
|
|
11
|
+
* {
|
|
12
|
+
* "success": false,
|
|
13
|
+
* "errors": [
|
|
14
|
+
* {
|
|
15
|
+
* "code": "parse",
|
|
16
|
+
* "path": ["xxx"],
|
|
17
|
+
* "message": "xxx"
|
|
18
|
+
* }
|
|
19
|
+
* ]
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ### Example
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import type { Context, Env } from "hono";
|
|
27
|
+
*
|
|
28
|
+
* import { Hono } from "hono";
|
|
29
|
+
* import { z } from "zod";
|
|
30
|
+
* import { sValidator } from "@jderstd/hono-standard-validator";
|
|
31
|
+
*
|
|
32
|
+
* const app: Hono = new Hono();
|
|
33
|
+
*
|
|
34
|
+
* const json = z.object({
|
|
35
|
+
* name: z.string(),
|
|
36
|
+
* age: z.number()
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* type Json = z.infer<typeof json>;
|
|
40
|
+
*
|
|
41
|
+
* type RouteContext = Context<
|
|
42
|
+
* Env,
|
|
43
|
+
* "/",
|
|
44
|
+
* {
|
|
45
|
+
* in: {
|
|
46
|
+
* json: Json;
|
|
47
|
+
* },
|
|
48
|
+
* out: {
|
|
49
|
+
* json: Json;
|
|
50
|
+
* },
|
|
51
|
+
* },
|
|
52
|
+
* >;
|
|
53
|
+
*
|
|
54
|
+
* app.post(
|
|
55
|
+
* "/",
|
|
56
|
+
* sValidator("json", json),
|
|
57
|
+
* (c: RouteContext): Response => {
|
|
58
|
+
* const data: Json = c.req.valid("json");
|
|
59
|
+
* return c.json(data);
|
|
60
|
+
* }
|
|
61
|
+
* );
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare const sValidator: <Target extends keyof ValidationTargets, Schema extends StandardSchemaV1>(target: Target, schema: Schema) => hono0.MiddlewareHandler<hono0.Env, string, {
|
|
65
|
+
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 };
|
|
66
|
+
out: { [K_2 in Target]: StandardSchemaV1.InferOutput<Schema> };
|
|
67
|
+
}>;
|
|
68
|
+
export { sValidator };
|
|
69
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
|
+
const require_hook = require('./hook.js');
|
|
3
|
+
let __hono_standard_validator = require("@hono/standard-validator");
|
|
4
|
+
__hono_standard_validator = require_rolldown_runtime.__toESM(__hono_standard_validator);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Validate the request with validator based on Standard Schema.
|
|
8
|
+
*
|
|
9
|
+
* Following error may returned if the request is invalid:
|
|
10
|
+
*
|
|
11
|
+
* ```jsonc
|
|
12
|
+
* // Status: 400
|
|
13
|
+
* {
|
|
14
|
+
* "success": false,
|
|
15
|
+
* "errors": [
|
|
16
|
+
* {
|
|
17
|
+
* "code": "parse",
|
|
18
|
+
* "path": ["xxx"],
|
|
19
|
+
* "message": "xxx"
|
|
20
|
+
* }
|
|
21
|
+
* ]
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* ### Example
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import type { Context, Env } from "hono";
|
|
29
|
+
*
|
|
30
|
+
* import { Hono } from "hono";
|
|
31
|
+
* import { z } from "zod";
|
|
32
|
+
* import { sValidator } from "@jderstd/hono-standard-validator";
|
|
33
|
+
*
|
|
34
|
+
* const app: Hono = new Hono();
|
|
35
|
+
*
|
|
36
|
+
* const json = z.object({
|
|
37
|
+
* name: z.string(),
|
|
38
|
+
* age: z.number()
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* type Json = z.infer<typeof json>;
|
|
42
|
+
*
|
|
43
|
+
* type RouteContext = Context<
|
|
44
|
+
* Env,
|
|
45
|
+
* "/",
|
|
46
|
+
* {
|
|
47
|
+
* in: {
|
|
48
|
+
* json: Json;
|
|
49
|
+
* },
|
|
50
|
+
* out: {
|
|
51
|
+
* json: Json;
|
|
52
|
+
* },
|
|
53
|
+
* },
|
|
54
|
+
* >;
|
|
55
|
+
*
|
|
56
|
+
* app.post(
|
|
57
|
+
* "/",
|
|
58
|
+
* sValidator("json", json),
|
|
59
|
+
* (c: RouteContext): Response => {
|
|
60
|
+
* const data: Json = c.req.valid("json");
|
|
61
|
+
* return c.json(data);
|
|
62
|
+
* }
|
|
63
|
+
* );
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
const sValidator = (target, schema) => {
|
|
67
|
+
return (0, __hono_standard_validator.sValidator)(target, schema, require_hook.sValidatorHook);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
exports.sValidator = sValidator;
|
|
71
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["sValidatorHook"],"sources":["../src/index.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { ValidationTargets } from \"hono\";\n\nimport { sValidator as sv } from \"@hono/standard-validator\";\n\nimport { sValidatorHook } from \"#/hook\";\n\n/**\n * Validate the request with validator based on Standard Schema.\n *\n * Following error may returned if the request is invalid:\n *\n * ```jsonc\n * // Status: 400\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"parse\",\n * \"path\": [\"xxx\"],\n * \"message\": \"xxx\"\n * }\n * ]\n * }\n * ```\n *\n * ### Example\n *\n * ```ts\n * import type { Context, Env } from \"hono\";\n *\n * import { Hono } from \"hono\";\n * import { z } from \"zod\";\n * import { sValidator } from \"@jderstd/hono-standard-validator\";\n *\n * const app: Hono = new Hono();\n *\n * const json = z.object({\n * name: z.string(),\n * age: z.number()\n * });\n *\n * type Json = z.infer<typeof json>;\n *\n * type RouteContext = Context<\n * Env,\n * \"/\",\n * {\n * in: {\n * json: Json;\n * },\n * out: {\n * json: Json;\n * },\n * },\n * >;\n *\n * app.post(\n * \"/\",\n * sValidator(\"json\", json),\n * (c: RouteContext): Response => {\n * const data: Json = c.req.valid(\"json\");\n * return c.json(data);\n * }\n * );\n * ```\n */\nconst sValidator = <\n Target extends keyof ValidationTargets,\n Schema extends StandardSchemaV1,\n>(\n target: Target,\n schema: Schema,\n) => {\n return sv(target, schema, sValidatorHook);\n};\n\nexport { sValidator };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,MAAM,cAIF,QACA,WACC;AACD,kDAAU,QAAQ,QAAQA,4BAAe"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { sValidatorHook } from "./hook.mjs";
|
|
2
|
+
import { sValidator as sValidator$1 } from "@hono/standard-validator";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Validate the request with validator based on Standard Schema.
|
|
6
|
+
*
|
|
7
|
+
* Following error may returned if the request is invalid:
|
|
8
|
+
*
|
|
9
|
+
* ```jsonc
|
|
10
|
+
* // Status: 400
|
|
11
|
+
* {
|
|
12
|
+
* "success": false,
|
|
13
|
+
* "errors": [
|
|
14
|
+
* {
|
|
15
|
+
* "code": "parse",
|
|
16
|
+
* "path": ["xxx"],
|
|
17
|
+
* "message": "xxx"
|
|
18
|
+
* }
|
|
19
|
+
* ]
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ### Example
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import type { Context, Env } from "hono";
|
|
27
|
+
*
|
|
28
|
+
* import { Hono } from "hono";
|
|
29
|
+
* import { z } from "zod";
|
|
30
|
+
* import { sValidator } from "@jderstd/hono-standard-validator";
|
|
31
|
+
*
|
|
32
|
+
* const app: Hono = new Hono();
|
|
33
|
+
*
|
|
34
|
+
* const json = z.object({
|
|
35
|
+
* name: z.string(),
|
|
36
|
+
* age: z.number()
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* type Json = z.infer<typeof json>;
|
|
40
|
+
*
|
|
41
|
+
* type RouteContext = Context<
|
|
42
|
+
* Env,
|
|
43
|
+
* "/",
|
|
44
|
+
* {
|
|
45
|
+
* in: {
|
|
46
|
+
* json: Json;
|
|
47
|
+
* },
|
|
48
|
+
* out: {
|
|
49
|
+
* json: Json;
|
|
50
|
+
* },
|
|
51
|
+
* },
|
|
52
|
+
* >;
|
|
53
|
+
*
|
|
54
|
+
* app.post(
|
|
55
|
+
* "/",
|
|
56
|
+
* sValidator("json", json),
|
|
57
|
+
* (c: RouteContext): Response => {
|
|
58
|
+
* const data: Json = c.req.valid("json");
|
|
59
|
+
* return c.json(data);
|
|
60
|
+
* }
|
|
61
|
+
* );
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
const sValidator = (target, schema) => {
|
|
65
|
+
return sValidator$1(target, schema, sValidatorHook);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export { sValidator };
|
|
69
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["sv"],"sources":["../src/index.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { ValidationTargets } from \"hono\";\n\nimport { sValidator as sv } from \"@hono/standard-validator\";\n\nimport { sValidatorHook } from \"#/hook\";\n\n/**\n * Validate the request with validator based on Standard Schema.\n *\n * Following error may returned if the request is invalid:\n *\n * ```jsonc\n * // Status: 400\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"parse\",\n * \"path\": [\"xxx\"],\n * \"message\": \"xxx\"\n * }\n * ]\n * }\n * ```\n *\n * ### Example\n *\n * ```ts\n * import type { Context, Env } from \"hono\";\n *\n * import { Hono } from \"hono\";\n * import { z } from \"zod\";\n * import { sValidator } from \"@jderstd/hono-standard-validator\";\n *\n * const app: Hono = new Hono();\n *\n * const json = z.object({\n * name: z.string(),\n * age: z.number()\n * });\n *\n * type Json = z.infer<typeof json>;\n *\n * type RouteContext = Context<\n * Env,\n * \"/\",\n * {\n * in: {\n * json: Json;\n * },\n * out: {\n * json: Json;\n * },\n * },\n * >;\n *\n * app.post(\n * \"/\",\n * sValidator(\"json\", json),\n * (c: RouteContext): Response => {\n * const data: Json = c.req.valid(\"json\");\n * return c.json(data);\n * }\n * );\n * ```\n */\nconst sValidator = <\n Target extends keyof ValidationTargets,\n Schema extends StandardSchemaV1,\n>(\n target: Target,\n schema: Schema,\n) => {\n return sv(target, schema, sValidatorHook);\n};\n\nexport { sValidator };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,MAAM,cAIF,QACA,WACC;AACD,QAAOA,aAAG,QAAQ,QAAQ,eAAe"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jderstd/hono-standard-validator",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A standard validator for Hono",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"jder",
|
|
7
|
+
"hono",
|
|
8
|
+
"validator",
|
|
9
|
+
"ts",
|
|
10
|
+
"typescript",
|
|
11
|
+
"js",
|
|
12
|
+
"javascript"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/jderstd/hono",
|
|
15
|
+
"bugs": "https://github.com/jderstd/hono/issues",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/jderstd/hono.git",
|
|
19
|
+
"directory": "packages/hono-standard-validator"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Alpheus",
|
|
24
|
+
"email": "contact@alphe.us"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.mjs",
|
|
30
|
+
"require": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./hook": {
|
|
33
|
+
"types": "./dist/hook.d.ts",
|
|
34
|
+
"import": "./dist/hook.mjs",
|
|
35
|
+
"require": "./dist/hook.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"main": "./dist/index.js",
|
|
40
|
+
"module": "./dist/index.mjs",
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"files": [
|
|
43
|
+
"dist"
|
|
44
|
+
],
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@hono/standard-validator": "~0.1.5",
|
|
47
|
+
"@standard-schema/spec": "^1.0.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"hono": "4.5.0",
|
|
51
|
+
"@jderstd/hono": "0.6.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@jderstd/hono": "~0.6.0",
|
|
55
|
+
"hono": "^4.5.0"
|
|
56
|
+
}
|
|
57
|
+
}
|