arktype-validation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseArkType Validation
ArkType 验证
Overview
概述
ArkType is a TypeScript-native runtime validation library that defines schemas using string expressions mirroring TypeScript syntax, providing editor autocomplete, syntax highlighting, and optimized validators. Use when building type-safe APIs, validating JSON payloads, or replacing Zod with a more TypeScript-idiomatic approach. Not suitable for projects that need Zod ecosystem compatibility or JSON Schema output.
Package:
arktypeArkType是一款原生支持TypeScript的运行时验证库,它使用镜像TypeScript语法的字符串表达式来定义Schema,提供编辑器自动补全、语法高亮和经过优化的验证器。适用于构建类型安全API、验证JSON负载,或是以更贴合TypeScript风格的方式替代Zod。不适用于需要兼容Zod生态系统或输出JSON Schema的项目。
包:
arktypeQuick Reference
快速参考
| Pattern | Usage |
|---|---|
| Define object schema |
| Primitive type |
| Built-in string validators |
| Built-in string morphs (transforms) |
| Parse JSON string to validated object |
| Inline constraints |
| Union types |
| String literal unions |
| Array types |
| Optional properties |
| Default values |
| Transform output (morphs) |
| Custom validation (like Zod refine) |
| Object property selection |
| Combine object types |
| Named type scopes with cross-references |
| Generic type definitions |
| Error handling (check |
| Global defaults (from |
| Type-safe pattern matching (2.1) |
| Inline undeclared key handling |
| 模式 | 用法 |
|---|---|
| 定义对象Schema |
| 原始类型 |
| 内置字符串验证器 |
| 内置字符串转换(transforms) |
| 将JSON字符串解析为经过验证的对象 |
| 内联约束 |
| 联合类型 |
| 字符串字面量联合类型 |
| 数组类型 |
| 可选属性 |
| 默认值 |
| 转换输出(morphs) |
| 自定义验证(类似Zod的refine) |
| 对象属性选择 |
| 合并对象类型 |
| 支持交叉引用的命名类型作用域 |
| 泛型类型定义 |
| 错误处理(检查 |
| 全局默认配置(来自 |
| 类型安全的模式匹配(2.1版本新增) |
| 内联未声明键的处理方式 |
Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
| |
Checking errors with | Use |
| |
Importing from | Import |
Nested | Use |
Raw | Use |
| |
Configuring after importing | Import |
| 错误内容 | 修复方案 |
|---|---|
带括号的 | |
使用 | 使用 |
用 | |
从 | 从 |
在字符串表达式中嵌套 | 使用 |
未处理错误的原生 | 对可能抛出异常的操作使用 |
用 | |
导入 | 先导入 |
Delegation
职责划分
Use this skill for ArkType schema definitions, runtime validation, morphs/transforms, scopes, and type inference. For Zod-based validation, delegate to the zod-validation skill.
当涉及ArkType Schema定义、运行时验证、转换(morphs/transforms)、作用域和类型推断时使用本技能。对于基于Zod的验证,请委托给zod-validation技能。
References
参考资料
- Schema Types — primitives, string keywords, number constraints, objects, arrays, tuples, unions, optional, defaults
- Morphs and Scopes — pipe, morph transforms, narrow validation, scopes, recursive types, generics, global configuration, pattern matching
- Common Patterns — JSON parsing, form validation, API responses, error handling, comparison with Zod
- Schema Types — 原始类型、字符串关键字、数字约束、对象、数组、元组、联合类型、可选属性、默认值
- Morphs and Scopes — pipe、转换(morph)、narrow验证、作用域、递归类型、泛型、全局配置、模式匹配
- Common Patterns — JSON解析、表单验证、API响应、错误处理、与Zod的对比