@gravito/mass 1.0.0-alpha.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/README.md +145 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/validator.d.ts +22 -0
- package/dist/validator.d.ts.map +1 -0
- package/package.json +54 -0
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# @gravito/mass
|
|
2
|
+
|
|
3
|
+
TypeBox-based validation for Gravito - High-performance schema validation with full TypeScript support.
|
|
4
|
+
|
|
5
|
+
## 特色
|
|
6
|
+
|
|
7
|
+
- **高效能驗證**: 基於 TypeBox,比 Zod 更快的執行速度
|
|
8
|
+
- **完整 TypeScript 支援**: 自動推導型別,無需手動定義
|
|
9
|
+
- **Hono 整合**: 完美整合 Hono 的驗證中間件
|
|
10
|
+
- **多種資料來源**: 支援 JSON、Query、Param、Form 驗證
|
|
11
|
+
|
|
12
|
+
## 安裝
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun add @gravito/mass
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 基本使用
|
|
19
|
+
|
|
20
|
+
### JSON 驗證
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { Hono } from 'hono'
|
|
24
|
+
import { Schema, validate } from '@gravito/mass'
|
|
25
|
+
|
|
26
|
+
const app = new Hono()
|
|
27
|
+
|
|
28
|
+
app.post('/login',
|
|
29
|
+
validate('json', Schema.Object({
|
|
30
|
+
username: Schema.String(),
|
|
31
|
+
password: Schema.String()
|
|
32
|
+
})),
|
|
33
|
+
(c) => {
|
|
34
|
+
const { username } = c.req.valid('json')
|
|
35
|
+
return c.json({ success: true, message: `Welcome ${username}` })
|
|
36
|
+
}
|
|
37
|
+
)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Query 參數驗證
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
app.get('/search',
|
|
44
|
+
validate('query', Schema.Object({
|
|
45
|
+
q: Schema.String(),
|
|
46
|
+
page: Schema.Optional(Schema.Number())
|
|
47
|
+
})),
|
|
48
|
+
(c) => {
|
|
49
|
+
const { q, page } = c.req.valid('query')
|
|
50
|
+
return c.json({ query: q, page: page ?? 1 })
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Route 參數驗證
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
app.get('/users/:id',
|
|
59
|
+
validate('param', Schema.Object({
|
|
60
|
+
id: Schema.String({ pattern: '^[0-9]+$' })
|
|
61
|
+
})),
|
|
62
|
+
(c) => {
|
|
63
|
+
const { id } = c.req.valid('param')
|
|
64
|
+
return c.json({ userId: id })
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Schema 建構器
|
|
70
|
+
|
|
71
|
+
`Schema` 物件提供所有 TypeBox 的建構器:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { Schema } from '@gravito/mass'
|
|
75
|
+
|
|
76
|
+
// 基本型別
|
|
77
|
+
Schema.String()
|
|
78
|
+
Schema.Number()
|
|
79
|
+
Schema.Boolean()
|
|
80
|
+
Schema.Array(Schema.String())
|
|
81
|
+
|
|
82
|
+
// 物件
|
|
83
|
+
Schema.Object({
|
|
84
|
+
name: Schema.String(),
|
|
85
|
+
age: Schema.Number()
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// 選填欄位
|
|
89
|
+
Schema.Optional(Schema.String())
|
|
90
|
+
|
|
91
|
+
// 預設值
|
|
92
|
+
Schema.String({ default: 'hello' })
|
|
93
|
+
|
|
94
|
+
// 驗證規則
|
|
95
|
+
Schema.String({ minLength: 2, maxLength: 100 })
|
|
96
|
+
Schema.Number({ minimum: 0, maximum: 100 })
|
|
97
|
+
Schema.String({ format: 'email' })
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## 與 Hono Client 整合
|
|
101
|
+
|
|
102
|
+
使用 `app.route()` 方法串接路由模組,可以獲得完整的型別推導:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// app.ts
|
|
106
|
+
import { Hono } from 'hono'
|
|
107
|
+
import { userRoute } from './routes/user'
|
|
108
|
+
|
|
109
|
+
const app = new Hono()
|
|
110
|
+
const routes = app.route('/api/users', userRoute)
|
|
111
|
+
|
|
112
|
+
export default app
|
|
113
|
+
export type AppRoutes = typeof routes
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
前端使用時可以獲得完整的型別提示:
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// client.ts
|
|
120
|
+
import { hc } from 'hono/client'
|
|
121
|
+
import type { AppRoutes } from './types'
|
|
122
|
+
|
|
123
|
+
export const createClient = (baseUrl: string) => {
|
|
124
|
+
return hc<AppRoutes>(baseUrl)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 使用時有完整的型別提示
|
|
128
|
+
const client = createClient('http://localhost:3000')
|
|
129
|
+
const result = await client.api.users.login.$post({
|
|
130
|
+
json: { username: 'user', password: 'pass' }
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## 效能優勢
|
|
135
|
+
|
|
136
|
+
TypeBox 相較於 Zod 的優勢:
|
|
137
|
+
|
|
138
|
+
- **編譯時驗證**: TypeBox 在編譯時生成驗證器,執行時效能更高
|
|
139
|
+
- **更小的 bundle**: 產生的程式碼更小
|
|
140
|
+
- **更好的型別推導**: 與 TypeScript 深度整合
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT
|
|
145
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gravito/mass
|
|
3
|
+
*
|
|
4
|
+
* TypeBox-based validation for Gravito
|
|
5
|
+
* High-performance schema validation with full TypeScript support
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { Hono } from 'hono'
|
|
10
|
+
* import { Schema, validate } from '@gravito/mass'
|
|
11
|
+
*
|
|
12
|
+
* const app = new Hono()
|
|
13
|
+
*
|
|
14
|
+
* app.post('/login',
|
|
15
|
+
* validate('json', Schema.Object({
|
|
16
|
+
* username: Schema.String(),
|
|
17
|
+
* password: Schema.String()
|
|
18
|
+
* })),
|
|
19
|
+
* (c) => {
|
|
20
|
+
* const { username } = c.req.valid('json')
|
|
21
|
+
* return c.json({ success: true, message: `Welcome ${username}` })
|
|
22
|
+
* }
|
|
23
|
+
* )
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export { tbValidator as validator } from '@hono/typebox-validator';
|
|
27
|
+
export type { Static, TSchema } from '@sinclair/typebox';
|
|
28
|
+
export * as Schema from '@sinclair/typebox';
|
|
29
|
+
export { type ValidationSource, validate } from './validator';
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,EAAE,WAAW,IAAI,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAGlE,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAExD,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAA;AAG3C,OAAO,EAAE,KAAK,gBAAgB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
import { tbValidator as tbValidator2 } from "@hono/typebox-validator";
|
|
4
|
+
import * as Schema from "@sinclair/typebox";
|
|
5
|
+
|
|
6
|
+
// src/validator.ts
|
|
7
|
+
import { tbValidator } from "@hono/typebox-validator";
|
|
8
|
+
function validate(source, schema) {
|
|
9
|
+
return tbValidator(source, schema);
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
tbValidator2 as validator,
|
|
13
|
+
validate,
|
|
14
|
+
Schema
|
|
15
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Static, TSchema } from '@sinclair/typebox';
|
|
2
|
+
import type { Env, MiddlewareHandler } from 'hono';
|
|
3
|
+
/**
|
|
4
|
+
* Validation source type.
|
|
5
|
+
*/
|
|
6
|
+
export type ValidationSource = 'json' | 'query' | 'param' | 'form';
|
|
7
|
+
/**
|
|
8
|
+
* Create a validation middleware.
|
|
9
|
+
*
|
|
10
|
+
* @param source - Validation source (json, query, param, form)
|
|
11
|
+
* @param schema - TypeBox Schema
|
|
12
|
+
* @returns Hono middleware
|
|
13
|
+
*/
|
|
14
|
+
export declare function validate<T extends TSchema, S extends ValidationSource, E extends Env = any, P extends string = any>(source: S, schema: T): MiddlewareHandler<E, P, {
|
|
15
|
+
in: {
|
|
16
|
+
[K in S]: Static<T>;
|
|
17
|
+
};
|
|
18
|
+
out: {
|
|
19
|
+
[K in S]: Static<T>;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
//# sourceMappingURL=validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../src/validator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,GAAG,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAA;AAElD;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;AAElE;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACtB,CAAC,SAAS,OAAO,EACjB,CAAC,SAAS,gBAAgB,EAC1B,CAAC,SAAS,GAAG,GAAG,GAAG,EACnB,CAAC,SAAS,MAAM,GAAG,GAAG,EAEtB,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,CAAC,GACR,iBAAiB,CAClB,CAAC,EACD,CAAC,EACD;IACE,EAAE,EAAE;SAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;KAAE,CAAA;IAC3B,GAAG,EAAE;SAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;KAAE,CAAA;CAC7B,CACF,CAEA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gravito/mass",
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
|
+
"description": "TypeBox-based validation for Gravito - High-performance schema validation with full TypeScript support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "bun run build.ts",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"test": "bun test",
|
|
21
|
+
"prepublishOnly": "bun run build"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@hono/typebox-validator": "^1.1.0",
|
|
28
|
+
"@sinclair/typebox": "^0.34.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"hono": "^4.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"bun-types": "^1.1.0",
|
|
35
|
+
"typescript": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"author": "Carl Lee <carllee0520@gmail.com>",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/gravito-framework/gravito.git",
|
|
42
|
+
"directory": "packages/mass"
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"gravito",
|
|
46
|
+
"validation",
|
|
47
|
+
"typebox",
|
|
48
|
+
"schema",
|
|
49
|
+
"hono",
|
|
50
|
+
"middleware",
|
|
51
|
+
"typescript"
|
|
52
|
+
],
|
|
53
|
+
"homepage": "https://github.com/gravito-framework/gravito#readme"
|
|
54
|
+
}
|