Skip to content

Commit 233c724

Browse files
committed
feat: impl ajv + typebox Validator
closes #200
1 parent 7637fc3 commit 233c724

64 files changed

Lines changed: 1222 additions & 41 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/ajv-decorator/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `@eggjs/ajv-decorator`
2+
3+
## Usage
4+
5+
Please read [@eggjs/tegg-ajv-plugin](../../plugin/ajv-plugin)

core/ajv-decorator/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from '@sinclair/typebox';
2+
export * from './src/type/Ajv';
3+
export * from './src/enum/Transform';

core/ajv-decorator/package.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "@eggjs/ajv-decorator",
3+
"version": "3.35.1",
4+
"description": "tegg ajv decorator",
5+
"keywords": [
6+
"egg",
7+
"typescript",
8+
"decorator",
9+
"tegg",
10+
"ajv"
11+
],
12+
"main": "dist/index.js",
13+
"files": [
14+
"dist/**/*.js",
15+
"dist/**/*.d.ts"
16+
],
17+
"typings": "dist/index.d.ts",
18+
"scripts": {
19+
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
20+
"clean": "tsc -b --clean",
21+
"tsc": "npm run clean && tsc -p ./tsconfig.json",
22+
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
23+
"prepublishOnly": "npm run tsc:pub"
24+
},
25+
"author": "killagu <[email protected]>",
26+
"license": "MIT",
27+
"homepage": "https://github.com/eggjs/tegg",
28+
"bugs": {
29+
"url": "https://github.com/eggjs/tegg/issues"
30+
},
31+
"repository": {
32+
"type": "git",
33+
"url": "[email protected]:eggjs/tegg.git",
34+
"directory": "core/ajv-decorator"
35+
},
36+
"engines": {
37+
"node": ">=14.0.0"
38+
},
39+
"dependencies": {
40+
"@sinclair/typebox": "^0.32.20",
41+
"ajv": "^8.12.0"
42+
},
43+
"publishConfig": {
44+
"access": "public"
45+
},
46+
"devDependencies": {
47+
"@types/mocha": "^10.0.1",
48+
"@types/node": "^20.2.4",
49+
"cross-env": "^7.0.3",
50+
"mocha": "^10.2.0",
51+
"ts-node": "^10.9.1",
52+
"typescript": "^5.0.4"
53+
}
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This keyword allows a string to be modified during validation.
3+
* This keyword applies only to strings. If the data is not a string, the transform keyword is ignored.
4+
* @see https://github.com/ajv-validator/ajv-keywords?tab=readme-ov-file#transform
5+
*/
6+
export enum Transform {
7+
/** remove whitespace from start and end */
8+
trim = 'trim',
9+
/** remove whitespace from start */
10+
trimStart = 'trimStart',
11+
/**
12+
* @alias trimStart
13+
*/
14+
trimLeft = 'trimLeft',
15+
/** remove whitespace from end */
16+
trimEnd = 'trimEnd',
17+
/**
18+
* @alias trimEnd
19+
*/
20+
trimRight = 'trimRight',
21+
/** convert to lower case */
22+
toLowerCase = 'toLowerCase',
23+
/** convert to upper case */
24+
toUpperCase = 'toUpperCase',
25+
/**
26+
* change string case to be equal to one of `enum` values in the schema
27+
*
28+
* **NOTE**: requires that all allowed values are unique when case insensitive
29+
* ```ts
30+
* const schema = {
31+
* type: "array",
32+
* items: {
33+
* type: "string",
34+
* transform: ["trim", Transform.toEnumCase],
35+
* enum: ["pH"],
36+
* },
37+
* };
38+
*
39+
* const data = ["ph", " Ph", "PH", "pH "];
40+
* ajv.validate(schema, data);
41+
* console.log(data) // ['pH','pH','pH','pH'];
42+
* ```
43+
*/
44+
toEnumCase = 'toEnumCase',
45+
}

core/ajv-decorator/src/type/Ajv.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Schema } from 'ajv/dist/2019';
2+
3+
export interface Ajv {
4+
validate(schema: Schema, data: unknown): void;
5+
}

core/ajv-decorator/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"baseUrl": "./"
6+
},
7+
"exclude": [
8+
"dist",
9+
"node_modules",
10+
"test"
11+
]
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"baseUrl": "./"
6+
},
7+
"exclude": [
8+
"dist",
9+
"node_modules",
10+
"test"
11+
]
12+
}

core/tegg/ajv.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@eggjs/ajv-decorator';

core/tegg/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"node": ">=14.0.0"
3636
},
3737
"dependencies": {
38+
"@eggjs/ajv-decorator": "^3.35.1",
3839
"@eggjs/aop-decorator": "^3.35.1",
3940
"@eggjs/controller-decorator": "^3.35.1",
4041
"@eggjs/core-decorator": "^3.35.1",

0 commit comments

Comments
 (0)