@ 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。 。 。 .