SuperAPI AI 工具集

contributing-to-z-schema

通用与其它

查看项目

更新于 10-15 23:40

技能介绍

用途:这是一份面向 z-schema 贡献者的开发指引,帮助理解代码库结构、本地环境搭建与提交流程。z-schema 是一个用 TypeScript 编写的 JSON Schema 校验器,支持 draft-04 至 draft-2020-12。

仓库初始化:

git clone --recursive https://github.com/zaggino/z-schema.git cd z-schema npm install

若未带 --recursive 克隆(json-schema-spec/ 子模块需要),执行:

git submodule update --init --recursive

质量检查(推送前):npm run check(ultracite check,含 oxlint 与 oxfmt)、npm run build(TypeScript + Rollup)、npm run build:tests、npm test(Vitest,node + browser)。提交前钩子会自动对暂存文件运行 lint 与 format,推送前钩子运行 build 与类型检查。

代码库结构:src/index.ts 为公共 API 导出;z-schema.ts 为工厂及 ZSchema/ZSchemaSafe/ZSchemaAsync/ZSchemaAsyncSafe;z-schema-base.ts 为核心校验编排;schema-compiler.ts 负责 $ref 解析、id 收集与 schema 编译;schema-validator.ts 做 schema 级校验;json-validation.ts 做数据校验编排;validation/ 下按类别拆分关键字校验器(type、numeric、string、array、object、combinators、ref);schema-cache.ts、errors.ts、format-validators.ts、report.ts、json-schema.ts、json-schema-versions.ts、z-schema-options.ts、utils/、schemas/ 等。校验流程分为 schema 编译、schema 校验、JSON 数据校验与错误累积。

常见任务:添加错误码(在 src/errors.ts 的 Errors 对象中加入,使用 report.addError,并写测试);添加格式校验器(在 src/format-validators.ts 中写函数并注册到 inbuiltValidators,测试位于 test/spec/format-validators.spec.ts);添加选项(在 z-schema-options.ts 中添加并设默认值,strictMode 下需在 normalizeOptions 中添加,文档 docs/options.md);实现新关键字(在对应 validation/*.ts 模块中写逻辑,按 draft 版本守卫,取消排除相关测试条目,运行 JSON Schema Test Suite 验证,并通过 src/index.ts 导出新类型)。

测试使用 Vitest,分 node 与 browser(Playwright 覆盖 Chromium、Firefox、WebKit)两个项目,文件位于 test/spec/,命名后缀决定运行环境。官方 JSON Schema Test Suite 通过 test/spec/json-schema-test-suite.common.ts 加载。代码约定包括 TypeScript strict、ESM、导入顺序、格式规范与命名规范。PR 清单要求从 main 分支、通过全部检查、新功能带测试、更新文档并取消相应测试排除。限制说明:以上内容仅描述贡献流程与代码结构,未提供收费、价格或效果信息。