@grest-ts/schema

中文文档
0.0.101发布于 2026/11/02MITTypeScriptES Module零依赖近 30 天下载 7394

TypeScript 的型式安全计划验证、序列化和合同定义

中文使用文档
文档目录5 节
  1. 特征
  2. 快速使用示例
  3. 合同实例
  4. 权限( 引用)
  5. 文档

部分 灰尘 框架. 文档 | 所有软件包

@ grest-ts/ schema (英语)

Grest Framework 的类型安全运行时间验证库.

特征

  • 世界最快的弦乐、验证、清洁和胁迫都归功于AOT.
  • 具有品牌类型支持的型号安全验证器
  • 初步验证器( IsString、 Is Number、 IsBoolean)
  • 复合验证器(对象、IsArray、IsUnion、IsAs-Discriminated)
  • 丰富的品牌验证器集(IsInt, IsEmail, IsUrl, IsDate等)
  • 强制支持 API 输入解析
  • 带有路径的详细校验错误收集
  • 零起跑时间挂牌
  • 通过@grest-ts/intl 支持内部化/翻译

快速使用示例

import {IsString, IsNumber, IsObject, IsInt, IsEmail, GGIssuesList} from "@grest-ts/schema"

// Define a validator for your type
const IsUser = IsObject({
    id: IsInt,
    name: IsString,
    email: IsEmail,
    age: IsInt.orUndefined
});

// ---
// Parse with coercion and error collection
const result = IsUser.safeParse(data, true) // set last argument to false to skip coercion.
if (result.success === false) {
    // Handle validation errors
    console.log(result.issues.toJSON())
}

// ---
// Type guard - returns boolean
if (IsUser.is(user)) {
    // data is typed as User  (but can contain extra properties, not safe for database insert for example. Use parse instead)
}

// ---
// Assert - throws on validation failure
IsUser.assert(data)

// ---
// Branded types for domain modeling
const IsUserId = IsInt.brand<"UserId">();
const userId = IsUserId.parse(rawId, true); // parse with coercion, throws on error
type tUserId = typeof IsUserId.infer;

合同实例

具有有效输入、输出和打入错误的类型安全类 :

import {EXISTS, GGContractClass, IsEmail, IsInt, IsObject, IsString, NOT_FOUND} from "@grest-ts/schema"

// Define a contract class with multiple methods
const UserContract = new GGContractClass("User", {
    get: {
        input: IsObject({userId: IsInt}),
        success: IsObject({id: IsInt, name: IsString}),
        errors: [NOT_FOUND]
    },
    create: {
        input: IsObject({name: IsString, email: IsEmail}),
        success: IsObject({id: IsInt, name: IsString}),
        errors: [EXISTS]
    }
});

// Implement the contract
const UserService = UserContract.implement((db) => ({
    async get(data) {
        const user = await db.users.find(data.userId)
        if (!user) throw new NOT_FOUND()
        return user
    },
    async create(data) {
        return db.users.create(data)
    }
}));

// Create instance and call methods - errors are typed!
const userService = new UserService(database);
const result = await userService.get({userId: 123}).asResult();
if (result.success) {
    console.log(result.data.name);  // typed as string
} else {
    console.log(result.type);  // "NOT_FOUND" | "VALIDATION_ERROR" | "SERVER_ERROR"
}

见 合同文件 对于自定义错误, GGPromise 帮助器, 以及更多.

权限( 引用)

合同方法可以宣布为任择方法 permission 字段。 呼叫器的瞄准镜来自电线 计划 .use()s (每条智能线) permissions() ——见 @grest-ts/http ——"许可". 严格模式是每个服务器:服务器上的任何路径宣布的时刻 permission,该服务器上的每个路径必须声明一条(使用) GG_NO_PERMISSIONS 故意使用公共路线。 检查在服务器启动时运行 。 从 @grest-ts/schema数字 :

  • GG_NO_PERMISSIONS ——明确公开终点.
  • GG_ANY_PERMISSION - 任何经认证的身份(电话员必须至少有一个范围).
  • 一个赤裸的 string ——OAuth型单机瞄准镜.
  • {allOf: [...]} 编号 : {anyOf: [...]} - 和/或组合器(非空拖拉).
  • satisfies(required, scopes) 门和门都使用纯检查器 GGPermissionChecker.has(...)。 。 。 .
  • GGPermissionChecker - 覆盖一个瞄准镜 allOf编号 :anyOf 检查使用相同的 satisfies(...) 门用.
  • validatePermission(tree) - 形而上学 GGContractClass编号 :GGContractFunction 设置权限时的构造器 .

跑步时机工——出闸布置,运输接线,严格模式起动检查——参见: @grest-ts/http 财务报告和审定财务报表 @grest-ts/websocket。 。 。 .

文档

  • 使用量 - 如何界定和使用验证器
  • 合同 - 有打字错误的RPC型安全合同
  • 代码c - 双向数据转换和计划文件
  • 本地化 - 如何将错误消息本地化.
  • 扩展 - 如何创建自定义验证器

版本与发布记录

@grest-ts/schema 共发布 84 个版本,首次发布于 2026/02/24;过去 12 个月发布了 84 个版本。

版本发布日期距今
0.0.101latest2026/11/0214 天前
0.0.1002026/10/3017 天前
0.0.992026/10/3018 天前
0.0.982026/10/2820 天前
0.0.972026/10/2720 天前
0.0.962026/10/2721 天前

关于 @grest-ts/schema 的常见问题

@grest-ts/schema 是什么?

TypeScript 的型式安全计划验证、序列化和合同定义

如何安装 @grest-ts/schema?

在项目目录运行 npm install @grest-ts/schema,或使用 pnpm add / yarn add / bun add。

@grest-ts/schema 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@grest-ts/schema 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@grest-ts/schema 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@grest-ts/schema 可以商用吗?

@grest-ts/schema 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@grest-ts/schema 的下载量有多少?

根据 npm 官方下载统计,@grest-ts/schema 在 2026/10/17 – 2026/11/15 这 30 天内被下载 7,394 次,日均约 246 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@grest-ts/schema 还在维护吗?

最新版本 0.0.101 发布于 2026/11/02(14 天前);至今共发布 84 个版本,其中过去 12 个月发布了 84 个;首次发布于 2026/02/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。