@enshou/valibot 0.0.1 → 0.0.2

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/dist/index.d.ts CHANGED
@@ -1,6 +1,10 @@
1
- import { ValidatorAdapter } from "@enshou/core";
1
+ import * as v from "valibot";
2
+ import { RouteSchema, ValidatorAdapter } from "@enshou/core";
2
3
 
3
4
  //#region src/adpater.d.ts
4
5
  declare function valibotAdapter(): ValidatorAdapter;
5
6
  //#endregion
6
- export { valibotAdapter };
7
+ //#region src/types.d.ts
8
+ type InferSchema<Schema extends RouteSchema> = { [K in keyof Schema]: Schema[K] extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>> ? v.InferOutput<Schema[K]> : never };
9
+ //#endregion
10
+ export { type InferSchema, valibotAdapter };
package/dist/index.js CHANGED
@@ -3,7 +3,17 @@ import * as v from "valibot";
3
3
  function valibotAdapter() {
4
4
  return {
5
5
  name: "valibot",
6
- parse: (schema, data) => v.parse(schema, data)
6
+ parse: (schema, data) => {
7
+ const result = v.safeParse(schema, data);
8
+ return {
9
+ success: result.success,
10
+ value: result.success ? result.output : void 0,
11
+ issues: result.success ? [] : result.issues.map((issue) => ({
12
+ path: issue.path?.map((p) => String(p.key)),
13
+ message: issue.message
14
+ }))
15
+ };
16
+ }
7
17
  };
8
18
  }
9
19
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enshou/valibot",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "private": false,
5
5
  "license": "Apache-2.0",
6
6
  "author": "Ivan Popov <iiivanpopov999@gmail.com>",
@@ -26,12 +26,12 @@
26
26
  "unplugin-swc": "^1.5.9",
27
27
  "valibot": "^1.3.1",
28
28
  "vitest": "^4.1.2",
29
- "@enshou/core": "0.2.1"
29
+ "@enshou/core": "0.2.3"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "hono": "^4.12.9",
33
33
  "valibot": "^1.3.1",
34
- "@enshou/core": "0.2.1"
34
+ "@enshou/core": "0.2.3"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "tsdown"
package/README.md DELETED
@@ -1,120 +0,0 @@
1
- # @enshou/valibot
2
-
3
- `@enshou/valibot` is the official Valibot adapter for `@enshou/core`
4
- validation.
5
-
6
- It connects route schemas declared in `@enshou/core` decorators to
7
- [`valibot`](https://valibot.dev/) parsing.
8
-
9
- ## Quick Example
10
-
11
- ```ts
12
- import { Application, Controller, Post, type Ctx } from '@enshou/core'
13
- import { Inject } from '@enshou/di'
14
- import { valibotAdapter } from '@enshou/valibot'
15
- import * as v from 'valibot'
16
-
17
- const CreateUserSchema = v.object({
18
- json: v.object({
19
- name: v.pipe(v.string(), v.minLength(1)),
20
- age: v.pipe(v.number(), v.integer(), v.minValue(18)),
21
- }),
22
- })
23
-
24
- type CreateUserInput = v.InferOutput<typeof CreateUserSchema>['json']
25
- type CreateUserContext = Ctx<v.InferOutput<typeof CreateUserSchema>>
26
-
27
- class UserService {
28
- createUser(input: CreateUserInput) {
29
- return {
30
- id: 1,
31
- ...input,
32
- }
33
- }
34
- }
35
-
36
- @Controller('/users')
37
- @Inject([UserService])
38
- class UserController {
39
- constructor(private readonly userService: UserService) {}
40
-
41
- @Post('/', CreateUserSchema)
42
- createUser(c: CreateUserContext) {
43
- const input = c.req.valid('json')
44
- return c.json(this.userService.createUser(input), 201)
45
- }
46
- }
47
-
48
- const app = new Application({
49
- controllers: [UserController],
50
- services: [UserService],
51
- validator: valibotAdapter(),
52
- })
53
-
54
- export default app.instantiate()
55
- ```
56
-
57
- ## API
58
-
59
- ### `valibotAdapter()`
60
-
61
- Creates a `ValidatorAdapter` implementation for `@enshou/core`.
62
-
63
- ```ts
64
- import { valibotAdapter } from '@enshou/valibot'
65
-
66
- const validator = valibotAdapter()
67
- ```
68
-
69
- ## Schema Shape
70
-
71
- `@enshou/core` passes a single object into the adapter's `parse(...)` method.
72
- Shape your Valibot schema around the request parts you want to validate:
73
-
74
- ```ts
75
- const schema = v.object({
76
- json: v.object({
77
- name: v.string(),
78
- }),
79
- query: v.optional(
80
- v.object({
81
- page: v.string(),
82
- }),
83
- ),
84
- param: v.optional(
85
- v.object({
86
- id: v.string(),
87
- }),
88
- ),
89
- })
90
- ```
91
-
92
- The available keys are:
93
-
94
- - `json` for JSON request bodies
95
- - `query` for query string values
96
- - `param` for route parameters
97
-
98
- After parsing succeeds, the validated values are available through
99
- `c.req.valid('json')`, `c.req.valid('query')`, and `c.req.valid('param')`.
100
-
101
- ## Runtime Behavior
102
-
103
- - `valibotAdapter()` returns an object with `name: 'valibot'`
104
- - the adapter uses `v.parse(schema, data)` internally
105
- - validation errors are thrown by Valibot and are not transformed by this
106
- package
107
-
108
- ## Peer Dependencies
109
-
110
- This package expects these libraries in the consuming app:
111
-
112
- - `@enshou/core`
113
- - `hono`
114
- - `valibot`
115
-
116
- ## Exports
117
-
118
- ```ts
119
- import { valibotAdapter } from '@enshou/valibot'
120
- ```