Skip to content

Commit

Permalink
feat: impl ajv + typebox Validator
Browse files Browse the repository at this point in the history
closes #200
  • Loading branch information
fengmk2 committed Apr 1, 2024
1 parent 7637fc3 commit 233c724
Show file tree
Hide file tree
Showing 64 changed files with 1,222 additions and 41 deletions.
5 changes: 5 additions & 0 deletions core/ajv-decorator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `@eggjs/ajv-decorator`

## Usage

Please read [@eggjs/tegg-ajv-plugin](../../plugin/ajv-plugin)
3 changes: 3 additions & 0 deletions core/ajv-decorator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from '@sinclair/typebox';
export * from './src/type/Ajv';
export * from './src/enum/Transform';
54 changes: 54 additions & 0 deletions core/ajv-decorator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@eggjs/ajv-decorator",
"version": "3.35.1",
"description": "tegg ajv decorator",
"keywords": [
"egg",
"typescript",
"decorator",
"tegg",
"ajv"
],
"main": "dist/index.js",
"files": [
"dist/**/*.js",
"dist/**/*.d.ts"
],
"typings": "dist/index.d.ts",
"scripts": {
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
"clean": "tsc -b --clean",
"tsc": "npm run clean && tsc -p ./tsconfig.json",
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
"prepublishOnly": "npm run tsc:pub"
},
"author": "killagu <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/eggjs/tegg",
"bugs": {
"url": "https://github.com/eggjs/tegg/issues"
},
"repository": {
"type": "git",
"url": "[email protected]:eggjs/tegg.git",
"directory": "core/ajv-decorator"
},
"engines": {
"node": ">=14.0.0"
},
"dependencies": {
"@sinclair/typebox": "^0.32.20",
"ajv": "^8.12.0"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
45 changes: 45 additions & 0 deletions core/ajv-decorator/src/enum/Transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This keyword allows a string to be modified during validation.
* This keyword applies only to strings. If the data is not a string, the transform keyword is ignored.
* @see https://github.com/ajv-validator/ajv-keywords?tab=readme-ov-file#transform
*/
export enum Transform {
/** remove whitespace from start and end */
trim = 'trim',
/** remove whitespace from start */
trimStart = 'trimStart',
/**
* @alias trimStart
*/
trimLeft = 'trimLeft',
/** remove whitespace from end */
trimEnd = 'trimEnd',
/**
* @alias trimEnd
*/
trimRight = 'trimRight',
/** convert to lower case */
toLowerCase = 'toLowerCase',
/** convert to upper case */
toUpperCase = 'toUpperCase',
/**
* change string case to be equal to one of `enum` values in the schema
*
* **NOTE**: requires that all allowed values are unique when case insensitive
* ```ts
* const schema = {
* type: "array",
* items: {
* type: "string",
* transform: ["trim", Transform.toEnumCase],
* enum: ["pH"],
* },
* };
*
* const data = ["ph", " Ph", "PH", "pH "];
* ajv.validate(schema, data);
* console.log(data) // ['pH','pH','pH','pH'];
* ```
*/
toEnumCase = 'toEnumCase',
}
5 changes: 5 additions & 0 deletions core/ajv-decorator/src/type/Ajv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Schema } from 'ajv/dist/2019';

export interface Ajv {
validate(schema: Schema, data: unknown): void;
}
12 changes: 12 additions & 0 deletions core/ajv-decorator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"baseUrl": "./"
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
12 changes: 12 additions & 0 deletions core/ajv-decorator/tsconfig.pub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"baseUrl": "./"
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
1 change: 1 addition & 0 deletions core/tegg/ajv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@eggjs/ajv-decorator';
1 change: 1 addition & 0 deletions core/tegg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"node": ">=14.0.0"
},
"dependencies": {
"@eggjs/ajv-decorator": "^3.35.1",
"@eggjs/aop-decorator": "^3.35.1",
"@eggjs/controller-decorator": "^3.35.1",
"@eggjs/core-decorator": "^3.35.1",
Expand Down
Loading

0 comments on commit 233c724

Please sign in to comment.