vafast 0.3.7 → 0.3.10

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.
@@ -2,67 +2,75 @@ import { TSchema, Static } from '@sinclair/typebox';
2
2
  export { Static, TSchema, Type } from '@sinclair/typebox';
3
3
 
4
4
  /**
5
- * 高性能 Schema 验证器
5
+ * Schema 验证器 - 简洁版
6
6
  *
7
- * 使用 TypeBox TypeCompiler 进行 JIT 编译
8
- * 编译后的验证器会被缓存,避免重复编译开销
7
+ * 特点:
8
+ * - WeakMap 缓存避免内存泄漏
9
+ * - TypeCompiler JIT 编译,性能最佳
10
+ * - 支持 FormatRegistry(需确保同一实例)
9
11
  *
10
- * @version 2.0.0 - 添加预编译缓存
12
+ * @version 7.0.0
11
13
  */
12
14
 
15
+ /** Schema 配置接口 */
16
+ interface SchemaConfig {
17
+ body?: TSchema;
18
+ query?: TSchema;
19
+ params?: TSchema;
20
+ headers?: TSchema;
21
+ cookies?: TSchema;
22
+ }
13
23
  /** 验证错误接口 */
14
24
  interface ValidationError {
15
25
  path: string;
16
26
  message: string;
17
27
  code: string;
18
28
  value?: unknown;
19
- schema?: unknown;
20
29
  }
21
- /** 验证失败结果接口 */
22
- interface ValidationFailure {
23
- success: false;
24
- errors: ValidationError[];
25
- }
26
- /** 验证成功结果接口 */
27
- interface ValidationSuccess<T> {
30
+ /** 验证结果 */
31
+ type ValidationResult<T = unknown> = {
28
32
  success: true;
29
33
  data: T;
30
- }
31
- /** 验证结果联合类型 */
32
- type ValidationResult<T = unknown> = ValidationSuccess<T> | ValidationFailure;
33
- /**
34
- * 预编译 Schema(在启动时调用,避免首次请求的编译开销)
35
- * @param schemas 要预编译的 Schema 数组
36
- */
37
- declare function precompileSchemas(schemas: TSchema[]): void;
34
+ } | {
35
+ success: false;
36
+ errors: ValidationError[];
37
+ };
38
38
  /**
39
- * 使用TypeBox Schema验证数据(带缓存优化)
40
- * @param schema TypeBox Schema
41
- * @param data 要验证的数据
42
- * @returns 验证结果,包含类型安全的数据或详细错误信息
39
+ * 验证单个 Schema(返回结果对象)
43
40
  */
44
41
  declare function validateSchema<T extends TSchema>(schema: T, data: unknown): ValidationResult<Static<T>>;
45
42
  /**
46
- * 创建类型特化的验证器(最高性能)
47
- * 适用于频繁验证同一 Schema 的场景
48
- * @param schema TypeBox Schema
49
- * @returns 类型安全的验证函数
43
+ * 验证 Schema(抛出异常版本,用于框架内部)
50
44
  */
51
- declare function createValidator<T extends TSchema>(schema: T): (data: unknown) => ValidationResult<Static<T>>;
45
+ declare function validateSchemaOrThrow<T extends TSchema>(schema: T, data: unknown, context: string): Static<T>;
52
46
  /**
53
- * 快速验证(只返回布尔值,不收集错误)
54
- * 适用于只需要知道验证结果的场景
55
- * @param schema TypeBox Schema
56
- * @param data 要验证的数据
57
- * @returns 验证是否通过
47
+ * 快速验证(只返回布尔值)
58
48
  */
59
49
  declare function validateFast<T extends TSchema>(schema: T, data: unknown): data is Static<T>;
60
50
  /**
61
- * 获取缓存统计信息(用于调试)
51
+ * 批量验证所有 Schema(用于请求验证)
52
+ */
53
+ declare function validateAllSchemas(config: SchemaConfig, data: {
54
+ body: unknown;
55
+ query: unknown;
56
+ params: unknown;
57
+ headers: unknown;
58
+ cookies: unknown;
59
+ }): typeof data;
60
+ /**
61
+ * 预编译 Schema(启动时调用,避免首次请求开销)
62
+ */
63
+ declare function precompileSchemas(config: SchemaConfig): void;
64
+ /**
65
+ * 创建类型特化的验证器(高频使用场景)
66
+ */
67
+ declare function createValidator<T extends TSchema>(schema: T): (data: unknown) => ValidationResult<Static<T>>;
68
+ /**
69
+ * 获取缓存统计(调试用)
62
70
  */
63
71
  declare function getValidatorCacheStats(): {
64
72
  cacheType: string;
65
73
  note: string;
66
74
  };
67
75
 
68
- export { type ValidationError, type ValidationFailure, type ValidationResult, type ValidationSuccess, createValidator, getValidatorCacheStats, precompileSchemas, validateFast, validateSchema };
76
+ export { type SchemaConfig, type ValidationError, type ValidationResult, createValidator, getValidatorCacheStats, precompileSchemas, validateAllSchemas, validateFast, validateSchema, validateSchemaOrThrow };
@@ -10,76 +10,78 @@ function getCompiledValidator(schema) {
10
10
  }
11
11
  return compiler;
12
12
  }
13
- function precompileSchemas(schemas) {
14
- for (const schema of schemas) {
15
- getCompiledValidator(schema);
16
- }
17
- }
18
13
  function validateSchema(schema, data) {
19
14
  try {
20
15
  const compiler = getCompiledValidator(schema);
21
16
  if (compiler.Check(data)) {
22
- return {
23
- success: true,
24
- data
25
- };
17
+ return { success: true, data };
26
18
  }
27
19
  const errors = [];
28
- const errorIterator = compiler.Errors(data);
29
- for (const error of errorIterator) {
20
+ for (const error of compiler.Errors(data)) {
30
21
  errors.push({
31
22
  path: error.path,
32
23
  message: error.message,
33
24
  code: "VALIDATION_FAILED",
34
- value: error.value,
35
- schema: error.schema
25
+ value: error.value
36
26
  });
37
27
  }
38
- return {
39
- success: false,
40
- errors
41
- };
28
+ return { success: false, errors };
42
29
  } catch (error) {
43
30
  return {
44
31
  success: false,
45
32
  errors: [
46
33
  {
47
34
  path: "",
48
- message: error instanceof Error ? error.message : "Unknown validation error",
49
- code: "VALIDATION_EXCEPTION",
50
- value: data
35
+ message: error instanceof Error ? error.message : "\u9A8C\u8BC1\u5F02\u5E38",
36
+ code: "VALIDATION_EXCEPTION"
51
37
  }
52
38
  ]
53
39
  };
54
40
  }
55
41
  }
56
- function createValidator(schema) {
42
+ function validateSchemaOrThrow(schema, data, context) {
57
43
  const compiler = getCompiledValidator(schema);
58
- return (data) => {
59
- if (compiler.Check(data)) {
60
- return { success: true, data };
61
- }
62
- const errors = [];
63
- for (const error of compiler.Errors(data)) {
64
- errors.push({
65
- path: error.path,
66
- message: error.message,
67
- code: "VALIDATION_FAILED",
68
- value: error.value,
69
- schema: error.schema
70
- });
71
- }
72
- return { success: false, errors };
73
- };
44
+ if (!compiler.Check(data)) {
45
+ throw new Error(`${context}\u9A8C\u8BC1\u5931\u8D25`);
46
+ }
47
+ return data;
74
48
  }
75
49
  function validateFast(schema, data) {
76
50
  const compiler = getCompiledValidator(schema);
77
51
  return compiler.Check(data);
78
52
  }
53
+ function validateAllSchemas(config, data) {
54
+ if (config.body) {
55
+ validateSchemaOrThrow(config.body, data.body, "\u8BF7\u6C42\u4F53");
56
+ }
57
+ if (config.query) {
58
+ validateSchemaOrThrow(config.query, data.query, "Query\u53C2\u6570");
59
+ }
60
+ if (config.params) {
61
+ validateSchemaOrThrow(config.params, data.params, "\u8DEF\u5F84\u53C2\u6570");
62
+ }
63
+ if (config.headers) {
64
+ validateSchemaOrThrow(config.headers, data.headers, "\u8BF7\u6C42\u5934");
65
+ }
66
+ if (config.cookies) {
67
+ validateSchemaOrThrow(config.cookies, data.cookies, "Cookie");
68
+ }
69
+ return data;
70
+ }
71
+ function precompileSchemas(config) {
72
+ if (config.body) getCompiledValidator(config.body);
73
+ if (config.query) getCompiledValidator(config.query);
74
+ if (config.params) getCompiledValidator(config.params);
75
+ if (config.headers) getCompiledValidator(config.headers);
76
+ if (config.cookies) getCompiledValidator(config.cookies);
77
+ }
78
+ function createValidator(schema) {
79
+ return (data) => validateSchema(schema, data);
80
+ }
79
81
  function getValidatorCacheStats() {
80
82
  return {
81
83
  cacheType: "WeakMap",
82
- note: "WeakMap \u4E0D\u652F\u6301 size \u5C5E\u6027\uFF0C\u7F13\u5B58\u4F1A\u968F Schema \u5BF9\u8C61\u81EA\u52A8\u6E05\u7406"
84
+ note: "WeakMap \u7F13\u5B58\u4F1A\u968F Schema \u5BF9\u8C61\u81EA\u52A8\u6E05\u7406\uFF0C\u65E0\u5185\u5B58\u6CC4\u6F0F\u98CE\u9669"
83
85
  };
84
86
  }
85
87
  export {
@@ -87,7 +89,9 @@ export {
87
89
  createValidator,
88
90
  getValidatorCacheStats,
89
91
  precompileSchemas,
92
+ validateAllSchemas,
90
93
  validateFast,
91
- validateSchema
94
+ validateSchema,
95
+ validateSchemaOrThrow
92
96
  };
93
97
  //# sourceMappingURL=validators.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/validators/validators.ts"],"sourcesContent":["// src/utils/validators.ts\n/**\n * 高性能 Schema 验证器\n *\n * 使用 TypeBox TypeCompiler 进行 JIT 编译\n * 编译后的验证器会被缓存,避免重复编译开销\n *\n * @version 2.0.0 - 添加预编译缓存\n */\n\nimport { Type } from \"@sinclair/typebox\";\nimport type { Static, TSchema } from \"@sinclair/typebox\";\nimport { TypeCompiler, type TypeCheck } from \"@sinclair/typebox/compiler\";\n\n/** 验证错误接口 */\nexport interface ValidationError {\n path: string;\n message: string;\n code: string;\n value?: unknown;\n schema?: unknown;\n}\n\n/** 验证失败结果接口 */\nexport interface ValidationFailure {\n success: false;\n errors: ValidationError[];\n}\n\n/** 验证成功结果接口 */\nexport interface ValidationSuccess<T> {\n success: true;\n data: T;\n}\n\n/** 验证结果联合类型 */\nexport type ValidationResult<T = unknown> =\n | ValidationSuccess<T>\n | ValidationFailure;\n\n/**\n * 编译器缓存\n * 使用 WeakMap 避免内存泄漏(Schema 对象被垃圾回收时,缓存也会自动清理)\n */\nconst compilerCache = new WeakMap<TSchema, TypeCheck<TSchema>>();\n\n/**\n * 获取或创建编译后的验证器\n * @param schema TypeBox Schema\n * @returns 编译后的验证器\n */\nfunction getCompiledValidator<T extends TSchema>(schema: T): TypeCheck<T> {\n let compiler = compilerCache.get(schema);\n if (!compiler) {\n compiler = TypeCompiler.Compile(schema);\n compilerCache.set(schema, compiler);\n }\n return compiler as TypeCheck<T>;\n}\n\n/**\n * 预编译 Schema(在启动时调用,避免首次请求的编译开销)\n * @param schemas 要预编译的 Schema 数组\n */\nexport function precompileSchemas(schemas: TSchema[]): void {\n for (const schema of schemas) {\n getCompiledValidator(schema);\n }\n}\n\n/**\n * 使用TypeBox Schema验证数据(带缓存优化)\n * @param schema TypeBox Schema\n * @param data 要验证的数据\n * @returns 验证结果,包含类型安全的数据或详细错误信息\n */\nexport function validateSchema<T extends TSchema>(\n schema: T,\n data: unknown,\n): ValidationResult<Static<T>> {\n try {\n // 从缓存获取或编译验证器\n const compiler = getCompiledValidator(schema);\n\n if (compiler.Check(data)) {\n return {\n success: true,\n data: data as Static<T>,\n };\n }\n\n // 验证失败时,使用Errors函数生成详细的错误信息\n const errors: ValidationError[] = [];\n const errorIterator = compiler.Errors(data);\n\n // 收集所有错误(可以根据需要限制数量)\n for (const error of errorIterator) {\n errors.push({\n path: error.path,\n message: error.message,\n code: \"VALIDATION_FAILED\",\n value: error.value,\n schema: error.schema,\n });\n }\n\n return {\n success: false,\n errors,\n };\n } catch (error) {\n // 处理验证过程中的异常\n return {\n success: false,\n errors: [\n {\n path: \"\",\n message:\n error instanceof Error ? error.message : \"Unknown validation error\",\n code: \"VALIDATION_EXCEPTION\",\n value: data,\n },\n ],\n };\n }\n}\n\n/**\n * 创建类型特化的验证器(最高性能)\n * 适用于频繁验证同一 Schema 的场景\n * @param schema TypeBox Schema\n * @returns 类型安全的验证函数\n */\nexport function createValidator<T extends TSchema>(\n schema: T,\n): (data: unknown) => ValidationResult<Static<T>> {\n const compiler = getCompiledValidator(schema);\n\n return (data: unknown): ValidationResult<Static<T>> => {\n if (compiler.Check(data)) {\n return { success: true, data: data as Static<T> };\n }\n\n const errors: ValidationError[] = [];\n for (const error of compiler.Errors(data)) {\n errors.push({\n path: error.path,\n message: error.message,\n code: \"VALIDATION_FAILED\",\n value: error.value,\n schema: error.schema,\n });\n }\n return { success: false, errors };\n };\n}\n\n/**\n * 快速验证(只返回布尔值,不收集错误)\n * 适用于只需要知道验证结果的场景\n * @param schema TypeBox Schema\n * @param data 要验证的数据\n * @returns 验证是否通过\n */\nexport function validateFast<T extends TSchema>(\n schema: T,\n data: unknown,\n): data is Static<T> {\n const compiler = getCompiledValidator(schema);\n return compiler.Check(data);\n}\n\n/**\n * 获取缓存统计信息(用于调试)\n */\nexport function getValidatorCacheStats(): { cacheType: string; note: string } {\n return {\n cacheType: \"WeakMap\",\n note: \"WeakMap 不支持 size 属性,缓存会随 Schema 对象自动清理\",\n };\n}\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,EAAE,SAAS,MAAM,KAAwB;AAAA,IAClD;AAEA,UAAM,SAA4B,CAAC;AACnC,eAAW,SAAS,SAAS,OAAO,IAAI,GAAG;AACzC,aAAO,KAAK;AAAA,QACV,MAAM,MAAM;AAAA,QACZ,SAAS,MAAM;AAAA,QACf,MAAM;AAAA,QACN,OAAO,MAAM;AAAA,QACb,QAAQ,MAAM;AAAA,MAChB,CAAC;AAAA,IACH;AACA,WAAO,EAAE,SAAS,OAAO,OAAO;AAAA,EAClC;AACF;AASO,SAAS,aACd,QACA,MACmB;AACnB,QAAM,WAAW,qBAAqB,MAAM;AAC5C,SAAO,SAAS,MAAM,IAAI;AAC5B;AAKO,SAAS,yBAA8D;AAC5E,SAAO;AAAA,IACL,WAAW;AAAA,IACX,MAAM;AAAA,EACR;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/utils/validators/validators.ts"],"sourcesContent":["/**\n * Schema 验证器 - 简洁版\n *\n * 特点:\n * - WeakMap 缓存避免内存泄漏\n * - TypeCompiler JIT 编译,性能最佳\n * - 支持 FormatRegistry(需确保同一实例)\n *\n * @version 7.0.0\n */\n\nimport { Type } from \"@sinclair/typebox\";\nimport type { Static, TSchema } from \"@sinclair/typebox\";\nimport { TypeCompiler, type TypeCheck } from \"@sinclair/typebox/compiler\";\nimport { Value } from \"@sinclair/typebox/value\";\n\n// ============== 类型定义 ==============\n\n/** Schema 配置接口 */\nexport interface SchemaConfig {\n body?: TSchema;\n query?: TSchema;\n params?: TSchema;\n headers?: TSchema;\n cookies?: TSchema;\n}\n\n/** 验证错误接口 */\nexport interface ValidationError {\n path: string;\n message: string;\n code: string;\n value?: unknown;\n}\n\n/** 验证结果 */\nexport type ValidationResult<T = unknown> =\n | { success: true; data: T }\n | { success: false; errors: ValidationError[] };\n\n// ============== 缓存 ==============\n\n/** 编译器缓存 - WeakMap 避免内存泄漏 */\nconst compilerCache = new WeakMap<TSchema, TypeCheck<TSchema>>();\n\n// ============== 核心函数 ==============\n\n/**\n * 获取或创建编译后的验证器\n */\nfunction getCompiledValidator<T extends TSchema>(schema: T): TypeCheck<T> {\n let compiler = compilerCache.get(schema);\n if (!compiler) {\n compiler = TypeCompiler.Compile(schema);\n compilerCache.set(schema, compiler);\n }\n return compiler as TypeCheck<T>;\n}\n\n/**\n * 验证单个 Schema(返回结果对象)\n */\nexport function validateSchema<T extends TSchema>(\n schema: T,\n data: unknown,\n): ValidationResult<Static<T>> {\n try {\n const compiler = getCompiledValidator(schema);\n\n if (compiler.Check(data)) {\n return { success: true, data: data as Static<T> };\n }\n\n // 收集错误\n const errors: ValidationError[] = [];\n for (const error of compiler.Errors(data)) {\n errors.push({\n path: error.path,\n message: error.message,\n code: \"VALIDATION_FAILED\",\n value: error.value,\n });\n }\n return { success: false, errors };\n } catch (error) {\n return {\n success: false,\n errors: [\n {\n path: \"\",\n message: error instanceof Error ? error.message : \"验证异常\",\n code: \"VALIDATION_EXCEPTION\",\n },\n ],\n };\n }\n}\n\n/**\n * 验证 Schema(抛出异常版本,用于框架内部)\n */\nexport function validateSchemaOrThrow<T extends TSchema>(\n schema: T,\n data: unknown,\n context: string,\n): Static<T> {\n const compiler = getCompiledValidator(schema);\n\n if (!compiler.Check(data)) {\n throw new Error(`${context}验证失败`);\n }\n\n return data as Static<T>;\n}\n\n/**\n * 快速验证(只返回布尔值)\n */\nexport function validateFast<T extends TSchema>(\n schema: T,\n data: unknown,\n): data is Static<T> {\n const compiler = getCompiledValidator(schema);\n return compiler.Check(data);\n}\n\n/**\n * 批量验证所有 Schema(用于请求验证)\n */\nexport function validateAllSchemas(\n config: SchemaConfig,\n data: {\n body: unknown;\n query: unknown;\n params: unknown;\n headers: unknown;\n cookies: unknown;\n },\n): typeof data {\n if (config.body) {\n validateSchemaOrThrow(config.body, data.body, \"请求体\");\n }\n if (config.query) {\n validateSchemaOrThrow(config.query, data.query, \"Query参数\");\n }\n if (config.params) {\n validateSchemaOrThrow(config.params, data.params, \"路径参数\");\n }\n if (config.headers) {\n validateSchemaOrThrow(config.headers, data.headers, \"请求头\");\n }\n if (config.cookies) {\n validateSchemaOrThrow(config.cookies, data.cookies, \"Cookie\");\n }\n return data;\n}\n\n/**\n * 预编译 Schema(启动时调用,避免首次请求开销)\n */\nexport function precompileSchemas(config: SchemaConfig): void {\n if (config.body) getCompiledValidator(config.body);\n if (config.query) getCompiledValidator(config.query);\n if (config.params) getCompiledValidator(config.params);\n if (config.headers) getCompiledValidator(config.headers);\n if (config.cookies) getCompiledValidator(config.cookies);\n}\n\n/**\n * 创建类型特化的验证器(高频使用场景)\n */\nexport function createValidator<T extends TSchema>(\n schema: T,\n): (data: unknown) => ValidationResult<Static<T>> {\n return (data: unknown) => validateSchema(schema, data);\n}\n\n/**\n * 获取缓存统计(调试用)\n */\nexport function getValidatorCacheStats(): { cacheType: string; note: string } {\n return {\n cacheType: \"WeakMap\",\n note: \"WeakMap 缓存会随 Schema 对象自动清理,无内存泄漏风险\",\n };\n}\n\n// 导出 TypeBox 类型\nexport { Type, Static, TSchema };\n"],"mappings":";AAWA,SAAS,YAAY;AAErB,SAAS,oBAAoC;AA8B7C,IAAM,gBAAgB,oBAAI,QAAqC;AAO/D,SAAS,qBAAwC,QAAyB;AACxE,MAAI,WAAW,cAAc,IAAI,MAAM;AACvC,MAAI,CAAC,UAAU;AACb,eAAW,aAAa,QAAQ,MAAM;AACtC,kBAAc,IAAI,QAAQ,QAAQ;AAAA,EACpC;AACA,SAAO;AACT;AAKO,SAAS,eACd,QACA,MAC6B;AAC7B,MAAI;AACF,UAAM,WAAW,qBAAqB,MAAM;AAE5C,QAAI,SAAS,MAAM,IAAI,GAAG;AACxB,aAAO,EAAE,SAAS,MAAM,KAAwB;AAAA,IAClD;AAGA,UAAM,SAA4B,CAAC;AACnC,eAAW,SAAS,SAAS,OAAO,IAAI,GAAG;AACzC,aAAO,KAAK;AAAA,QACV,MAAM,MAAM;AAAA,QACZ,SAAS,MAAM;AAAA,QACf,MAAM;AAAA,QACN,OAAO,MAAM;AAAA,MACf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B,OAAO,OAAO;AACzD;AAKO,SAAS,gBACd,QACgD;AAChD,SAAO,CAAC,SAAkB,eAAe,QAAQ,IAAI;AACvD;AAKO,SAAS,yBAA8D;AAC5E,SAAO;AAAA,IACL,WAAW;AAAA,IACX,MAAM;AAAA,EACR;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vafast",
3
- "version": "0.3.7",
3
+ "version": "0.3.10",
4
4
  "description": "极简结构化Web框架,支持 Bun 和 Node.js。Go风格,函数优先。",
5
5
  "type": "module",
6
6
  "repository": {
@@ -13,49 +13,31 @@
13
13
  },
14
14
  "exports": {
15
15
  ".": {
16
+ "types": "./dist/index.d.ts",
16
17
  "import": "./dist/index.js",
17
18
  "require": "./dist/index.js",
18
- "types": "./dist/index.d.ts",
19
19
  "default": "./dist/index.js"
20
20
  },
21
21
  "./mod": {
22
+ "types": "./dist/index.d.ts",
22
23
  "import": "./dist/index.js",
23
24
  "require": "./dist/index.js",
24
- "types": "./dist/index.d.ts",
25
25
  "default": "./dist/index.js"
26
26
  },
27
27
  "./node-server": {
28
+ "types": "./dist/node-server/index.d.ts",
28
29
  "import": "./dist/node-server/index.js",
29
30
  "require": "./dist/node-server/index.js",
30
- "types": "./dist/node-server/index.d.ts",
31
31
  "default": "./dist/node-server/index.js"
32
32
  }
33
33
  },
34
34
  "types": "./dist/index.d.ts",
35
35
  "main": "./dist/index.js",
36
36
  "scripts": {
37
- "clean": "rimraf dist",
38
37
  "build": "tsup",
39
- "dev": "bun --watch example/index.ts",
40
- "start": "bun run dist/index.js",
38
+ "dev": "npx tsx watch example/index.ts",
41
39
  "test": "vitest run",
42
- "test:run": "vitest run",
43
- "test:watch": "vitest",
44
- "test:unit": "vitest run tests/unit/",
45
- "test:integration": "vitest run tests/integration/",
46
- "test:coverage": "vitest run --coverage",
47
- "test:ui": "vitest --ui",
48
- "test:types": "tsc --noEmit",
49
- "benchmark": "bun run benchmarks/micro/router.bench.ts",
50
- "lint": "eslint src tests benchmarks",
51
- "lint:fix": "eslint src tests benchmarks --fix",
52
- "format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\" \"benchmarks/**/*.ts\"",
53
- "format:check": "prettier --check \"src/**/*.ts\" \"tests/**/*.ts\" \"benchmarks/**/*.ts\"",
54
- "type-check": "tsc --noEmit",
55
- "prepublishOnly": "npm run build && npm run test",
56
- "release:patch": "npm version patch && npm run build && npm run test && npm publish --access=public && git push && git push --tags",
57
- "release:minor": "npm version minor && npm run build && npm run test && npm publish --access=public && git push && git push --tags",
58
- "release:major": "npm version major && npm run build && npm run test && npm publish --access=public && git push && git push --tags"
40
+ "release": "npm run build && npm run test && npx bumpp && npm publish --access=public"
59
41
  },
60
42
  "keywords": [
61
43
  "bun",
@@ -1,70 +0,0 @@
1
- import { TSchema } from '@sinclair/typebox';
2
- import { ValidationResult } from './validators.js';
3
-
4
- /**
5
- * Schema配置验证器
6
- *
7
- * 使用validateSchema函数对SchemaConfig结构的数据进行验证
8
- * 提供统一的验证接口和错误处理
9
- *
10
- * @author Framework Team
11
- * @version 1.0.0
12
- * @license MIT
13
- */
14
-
15
- interface SchemaConfig {
16
- body?: TSchema;
17
- query?: TSchema;
18
- params?: TSchema;
19
- headers?: TSchema;
20
- cookies?: TSchema;
21
- }
22
- interface RequestData {
23
- body?: unknown;
24
- query?: unknown;
25
- params?: unknown;
26
- headers?: unknown;
27
- cookies?: unknown;
28
- }
29
- interface SchemaValidationResult {
30
- success: boolean;
31
- data?: {
32
- body?: unknown;
33
- query?: unknown;
34
- params?: unknown;
35
- headers?: unknown;
36
- cookies?: unknown;
37
- };
38
- errors?: Array<{
39
- field: keyof SchemaConfig;
40
- error: ValidationResult<unknown>;
41
- }>;
42
- }
43
- /**
44
- * 使用SchemaConfig验证完整的请求数据
45
- * @param config Schema配置
46
- * @param data 请求数据
47
- * @returns 验证结果
48
- */
49
- declare function validateSchemaConfig(config: SchemaConfig, data: RequestData): SchemaValidationResult;
50
- /**
51
- * 异步验证SchemaConfig(支持异步验证器)
52
- * @param config Schema配置
53
- * @param data 请求数据
54
- * @returns Promise<验证结果>
55
- */
56
- declare function validateSchemaConfigAsync(config: SchemaConfig, data: RequestData): Promise<SchemaValidationResult>;
57
- /**
58
- * 创建验证器工厂函数
59
- * @param config Schema配置
60
- * @returns 验证器函数
61
- */
62
- declare function createSchemaValidator(config: SchemaConfig): (data: RequestData) => SchemaValidationResult;
63
- /**
64
- * 创建异步验证器工厂函数
65
- * @param config Schema配置
66
- * @returns 异步验证器函数
67
- */
68
- declare function createAsyncSchemaValidator(config: SchemaConfig): (data: RequestData) => Promise<SchemaValidationResult>;
69
-
70
- export { type RequestData, type SchemaConfig, type SchemaValidationResult, createAsyncSchemaValidator, createSchemaValidator, validateSchemaConfig, validateSchemaConfigAsync };
@@ -1,241 +0,0 @@
1
- // src/utils/validators/validators.ts
2
- import { Type } from "@sinclair/typebox";
3
- import { TypeCompiler } from "@sinclair/typebox/compiler";
4
- var compilerCache = /* @__PURE__ */ new WeakMap();
5
- function getCompiledValidator(schema) {
6
- let compiler = compilerCache.get(schema);
7
- if (!compiler) {
8
- compiler = TypeCompiler.Compile(schema);
9
- compilerCache.set(schema, compiler);
10
- }
11
- return compiler;
12
- }
13
- function validateSchema(schema, data) {
14
- try {
15
- const compiler = getCompiledValidator(schema);
16
- if (compiler.Check(data)) {
17
- return {
18
- success: true,
19
- data
20
- };
21
- }
22
- const errors = [];
23
- const errorIterator = compiler.Errors(data);
24
- for (const error of errorIterator) {
25
- errors.push({
26
- path: error.path,
27
- message: error.message,
28
- code: "VALIDATION_FAILED",
29
- value: error.value,
30
- schema: error.schema
31
- });
32
- }
33
- return {
34
- success: false,
35
- errors
36
- };
37
- } catch (error) {
38
- return {
39
- success: false,
40
- errors: [
41
- {
42
- path: "",
43
- message: error instanceof Error ? error.message : "Unknown validation error",
44
- code: "VALIDATION_EXCEPTION",
45
- value: data
46
- }
47
- ]
48
- };
49
- }
50
- }
51
-
52
- // src/utils/validators/schema-validator.ts
53
- function validateSingleSchema(schema, data, fieldName) {
54
- return validateSchema(schema, data);
55
- }
56
- function validateSchemaConfig(config, data) {
57
- const errors = [];
58
- const validatedData = {};
59
- if (config.body && data.body !== void 0) {
60
- const result = validateSingleSchema(config.body, data.body, "body");
61
- if (result.success) {
62
- validatedData.body = result.data;
63
- } else {
64
- errors.push({ field: "body", error: result });
65
- }
66
- } else if (data.body !== void 0) {
67
- validatedData.body = data.body;
68
- }
69
- if (config.query && data.query !== void 0) {
70
- const result = validateSingleSchema(config.query, data.query, "query");
71
- if (result.success) {
72
- validatedData.query = result.data;
73
- } else {
74
- errors.push({ field: "query", error: result });
75
- }
76
- } else if (data.query !== void 0) {
77
- validatedData.query = data.query;
78
- }
79
- if (config.params && data.params !== void 0) {
80
- const result = validateSingleSchema(config.params, data.params, "params");
81
- if (result.success) {
82
- validatedData.params = result.data;
83
- } else {
84
- errors.push({ field: "params", error: result });
85
- }
86
- } else if (data.params !== void 0) {
87
- validatedData.params = data.params;
88
- }
89
- if (config.headers && data.headers !== void 0) {
90
- const result = validateSingleSchema(
91
- config.headers,
92
- data.headers,
93
- "headers"
94
- );
95
- if (result.success) {
96
- validatedData.headers = result.data;
97
- } else {
98
- errors.push({ field: "headers", error: result });
99
- }
100
- } else if (data.headers !== void 0) {
101
- validatedData.headers = data.headers;
102
- }
103
- if (config.cookies && data.cookies !== void 0) {
104
- const result = validateSingleSchema(
105
- config.cookies,
106
- data.cookies,
107
- "cookies"
108
- );
109
- if (result.success) {
110
- validatedData.cookies = result.data;
111
- } else {
112
- errors.push({ field: "cookies", error: result });
113
- }
114
- } else if (data.cookies !== void 0) {
115
- validatedData.cookies = data.cookies;
116
- }
117
- if (data.body !== void 0 && !config.body) {
118
- validatedData.body = data.body;
119
- }
120
- if (data.query !== void 0 && !config.query) {
121
- validatedData.query = data.query;
122
- }
123
- if (data.params !== void 0 && !config.params) {
124
- validatedData.params = data.params;
125
- }
126
- if (data.headers !== void 0 && !config.headers) {
127
- validatedData.headers = data.headers;
128
- }
129
- if (data.cookies !== void 0 && !config.cookies) {
130
- validatedData.cookies = data.cookies;
131
- }
132
- if (errors.length > 0) {
133
- return {
134
- success: false,
135
- errors
136
- };
137
- }
138
- return {
139
- success: true,
140
- data: validatedData
141
- };
142
- }
143
- async function validateSchemaConfigAsync(config, data) {
144
- const errors = [];
145
- const validatedData = {};
146
- const validationPromises = [];
147
- if (config.body && data.body !== void 0) {
148
- validationPromises.push(
149
- Promise.resolve({
150
- field: "body",
151
- result: validateSingleSchema(config.body, data.body, "body")
152
- })
153
- );
154
- }
155
- if (config.query && data.query !== void 0) {
156
- validationPromises.push(
157
- Promise.resolve({
158
- field: "query",
159
- result: validateSingleSchema(config.query, data.query, "query")
160
- })
161
- );
162
- }
163
- if (config.params && data.params !== void 0) {
164
- validationPromises.push(
165
- Promise.resolve({
166
- field: "params",
167
- result: validateSingleSchema(config.params, data.params, "params")
168
- })
169
- );
170
- }
171
- if (config.headers && data.headers !== void 0) {
172
- validationPromises.push(
173
- Promise.resolve({
174
- field: "headers",
175
- result: validateSingleSchema(config.headers, data.headers, "headers")
176
- })
177
- );
178
- }
179
- if (config.cookies && data.cookies !== void 0) {
180
- validationPromises.push(
181
- Promise.resolve({
182
- field: "cookies",
183
- result: validateSingleSchema(config.cookies, data.cookies, "cookies")
184
- })
185
- );
186
- }
187
- const results = await Promise.all(validationPromises);
188
- for (const { field, result } of results) {
189
- if (result.success) {
190
- validatedData[field] = result.data;
191
- } else {
192
- errors.push({ field, error: result });
193
- }
194
- }
195
- if (data.body !== void 0 && !config.body) validatedData.body = data.body;
196
- if (data.query !== void 0 && !config.query)
197
- validatedData.query = data.query;
198
- if (data.params !== void 0 && !config.params)
199
- validatedData.params = data.params;
200
- if (data.headers !== void 0 && !config.headers)
201
- validatedData.headers = data.headers;
202
- if (data.cookies !== void 0 && !config.cookies)
203
- validatedData.cookies = data.cookies;
204
- if (errors.length > 0) {
205
- return {
206
- success: false,
207
- errors
208
- };
209
- }
210
- return {
211
- success: true,
212
- data: validatedData
213
- };
214
- }
215
- function createSchemaValidator(config) {
216
- return (data) => {
217
- return validateSchemaConfig(config, data);
218
- };
219
- }
220
- function createAsyncSchemaValidator(config) {
221
- return (data) => {
222
- return validateSchemaConfigAsync(config, data);
223
- };
224
- }
225
- export {
226
- createAsyncSchemaValidator,
227
- createSchemaValidator,
228
- validateSchemaConfig,
229
- validateSchemaConfigAsync
230
- };
231
- /**
232
- * Schema配置验证器
233
- *
234
- * 使用validateSchema函数对SchemaConfig结构的数据进行验证
235
- * 提供统一的验证接口和错误处理
236
- *
237
- * @author Framework Team
238
- * @version 1.0.0
239
- * @license MIT
240
- */
241
- //# sourceMappingURL=schema-validator.js.map