类型方案
✨ https://typeschema.com (中文(简体) ) ✨
用于设计验证的通用适配器
设置 二. 支助 API 密码 二. 支助 覆盖范围 二. 支助 GitHub 图像 二. 支助 无 二. 支助 德诺
许多图书馆依赖于某种类型的验证. 他们的维护者可以选择:
- 执行自己的 验证逻辑:这会导致更多的代码需要维护,我们那里已经有许多好的解决方案(例如.
zod, (中文(简体) ),arktype, (中文(简体) ),typia(中文(简体) ) - 两人的密码 带有特定验证库: 限制使用另一个软件的开发者采用
- 支持多个 验证库:这是不断更新(如. tRPC 主题(中文(简体) )
因为总有取舍,所以没有最好的验证库. 每个开发者选择对他们最有意义的库. TypeSchema通过方便提供选项3来解决这个问题: 支持多个校验库外出.
特征
- 从计划验证库解析
- 小客户足迹,可树摇
- 方便使用,最小API
使用量
import type {Infer, InferIn, Schema} from '@decs/typeschema';
import {assert, validate, wrap} from '@decs/typeschema';
// Use your favorite validation library, e.g. `zod`, `arktype`, `typia`
const schema: Schema = z.string();
const schema: Schema = type('string');
const schema: Schema = typia.createAssert<string>();
// Extracts the schema type
type Output = Infer<typeof schema>; // `string`
type Input = InferIn<typeof schema>; // `string`
// Returns the wrapped schema with access to all its operations
const wrapped = wrap(schema);
await wrapped.validate('123'); // {success: true, data: '123'}
await wrapped.assert('123'); // '123'
// Returns the validated data or a list of `ValidationIssue`s
await validate(schema, '123'); // {success: true, data: '123'}
await validate(schema, 123); // {success: false, issues: [`ValidationIssue`]}
// Returns the validated data or throws an `AggregateError`
await assert(schema, '123'); // '123'
await assert(schema, 123); // throws `AggregateError`
tRPC 主题
您可以使用任何支持的计划 tRPC 主题 通过 wrap 函数 :
import {wrap} from '@decs/typeschema';
import {initTRPC} from '@trpc/server';
import {object, string} from 'valibot';
// Use your favorite validation library, e.g. `valibot`
const schema = object({name: string()});
const t = initTRPC.create();
const appRouter = t.router({
hello: t.procedure
.input(wrap(schema)) // like this
.query(({input}) => `Hello, ${input.name}!`),
});
覆盖范围
TypeSchema 支持所有主要的计划验证库 :
| 项目 | 大众性 | wrap | validateassert | Infer | InferIn | 示例计划 |
|---|---|---|---|---|---|---|
| 佐德语 | ✅ | ✅ | ✅ | ✅ | z.string() | |
| 对 | ✅ | ✅ | ✅ | ✅ | string() | |
| 乔伊 | ✅ | ✅ | ❌ | ❌ | Joi.string() | |
| 页:1 | ✅ | ✅ | ❌ | ❌ | {type: "string"} | |
| 超级结构 | ✅ | ✅ | ✅ | ❌ | string() | |
| io-ts(双胞胎) | ✅ | ✅ | ✅ | ✅ | t.string | |
| 华丽机器人 | ✅ | ✅ | ✅ | ✅ | string() | |
| 类型框 | ✅ | ✅ | ✅ | ✅ | Type.String() | |
| 伤寒 | ✅ | ✅ | ✅ | ✅ | typia.createAssert<string>() | |
| 呜[^1] | ✅ | ✅ | ✅ | ✅ | ow.string | |
| 效果 | ✅ | ✅ | ✅ | ✅ | S.string | |
| 方舟类型 | ✅ | ✅ | ✅ | ✅ | type('string') | |
| 深层 | ✅ | ✅ | ❌ | ❌ | typeOf<string>() | |
| 运行类型 | ✅ | ✅ | ✅ | ✅ | String |
[^1]:用于 呜,仅支持 v0.28.2 (sindresorhus/ow#248)
自定义验证也被支持 :
export function assertString(data: unknown): string {
if (typeof data !== 'string') {
throw new Error('Expected a string, got: ' + data);
}
return data;
}
await validate(assertString, '123'); // {success: true, data: '123'}
await validate(assertString, 123); // {success: false, issues: [`ValidationIssue`]}
await assert(assertString, '123'); // '123'
await assert(assertString, 123); // throws `AggregateError`
设置
用您选择的软件包管理器安装 TypeSchema :
| 无 | npm install @decs/typeschema |
|---|---|
| 雅伦 | yarn add @decs/typeschema |
| 小时 | pnpm add @decs/typeschema |
| 德诺 | https://deno.land/x/typeschema |
API 密码
类型
-
Schema计划通用接口
由所有支持的图书馆组成的计划类型联盟 -
TypeSchema<TOutput, TInput = TOutput>包扎图的接口,显示其所有操作
-
Infer<TSchema extends Schema>提取图案的输出类型
-
InferIn<TSchema extends Schema>提取策略的输入类型
-
ValidationIssue验证问题通用接口
包括amessage和可选path
职能
-
wrap(schema)wrap<TSchema extends Schema>( schema: TSchema, ): TypeSchema<Infer<TSchema>, InferIn<TSchema>>返回可访问其所有操作的包裹计划
-
validate(schema, data)validate<TSchema extends Schema>( schema: TSchema, data: unknown, ): Promise<ValidationResult<Infer<TSchema>>>返回已验证的数据或列表
ValidationIssue编号 -
assert(schema, data)assert<TSchema extends Schema>( schema: TSchema, data: unknown, ): Promise<Infer<TSchema>>返回已验证的数据或丢出一个
AggregateError
鸣谢
- 灵感来自 tRPC 主题是因为 输入输出验证符( o)
- 由 @ ecyrbe (英语)是因为 建议
- API 定义灵感来自 @colinhacks (英语)是因为 建议
- 日志设计 平方厘米