egg-typebox-validate
Advanced tools
Comparing version 2.0.0 to 2.1.0
{ | ||
"name": "egg-typebox-validate", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "another validate for typescript egg projects", | ||
@@ -59,8 +59,8 @@ "eggPlugin": { | ||
"tsc:noEmit": "tsc -p tsconfig.json --noEmit", | ||
"clean": "rm -rf app/**/*.js && rm -rf app.js", | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"clean": "rm -rf app/**/*.js && rm -rf *.js", | ||
"test": "npm run lint && npm run test-local", | ||
"test-local": "egg-bin test", | ||
"benchmark": "node ./benchmark/ajv-vs-parameter.mjs", | ||
"cov": "egg-bin cov", | ||
"lint": "eslint app --ext .ts", | ||
"lint": "eslint app --ext .ts --fix", | ||
"ci": "npm run lint && npm run cov", | ||
@@ -72,3 +72,5 @@ "prepublishOnly": "npm run clean && npm run build" | ||
"app/**/*.js", | ||
"app.js" | ||
"app.js", | ||
"decorator.js", | ||
"decorator.d.ts" | ||
], | ||
@@ -75,0 +77,0 @@ "ci": { |
@@ -167,3 +167,3 @@ # egg-typebox-validate | ||
```js | ||
```ts | ||
export const TYPEBOX_NAME_DESC_OBJECT = Type.Object({ | ||
@@ -311,7 +311,36 @@ name: Type.String(), | ||
} else { | ||
const errors = this.app.ajv.errors | ||
// handle errors | ||
... | ||
} | ||
``` | ||
3. ⭐⭐⭐ 装饰器 decorator `@Validate([ [rule1, ctx => ctx.xx1], [rule2, ctx => ctx.xx2] ])` 调用(写法更干净,推荐使用!️) | ||
```diff | ||
+ import { Validate } from 'egg-typebox-validate/decorator'; | ||
class HomeController extends Controller { | ||
+ @Validate([ | ||
+ [paramsSchema, ctx => ctx.params], | ||
+ [bodySchema, ctx => ctx.request.body], | ||
+ ]) | ||
async index() { | ||
const { ctx } = this; | ||
// 直接校验 | ||
- ctx.tValidate(paramsSchema, ctx.params); | ||
- ctx.tValidate(bodySchema, ctx.request.body); | ||
// 不用写 js 类型定义 | ||
const params: ParamsType = ctx.params; | ||
... | ||
} | ||
} | ||
export default HomeController; | ||
``` | ||
目前装饰器只支持有 `this.ctx` 的 class 上使用,比如 controller,service 等。更多使用案例可以看这个项目里写的测试用例。 | ||
## 怎么写 typebox 定义 | ||
@@ -318,0 +347,0 @@ |
22101
8
101
449