SuperAPI AI 工具集

tanstack-db-skills-tanstack-db-schemas

数据与分析

查看项目

更新于 10-16 00:02

技能介绍

用途:在 TanStack DB 中通过 schema 对客户端变更(insert/update)进行校验与类型转换,schema 可选但推荐用于类型安全与数据完整性。

主要能力:支持任意 StandardSchema 兼容库,如 Zod、Valibot、ArkType、Effect Schema。可定义基础校验、默认值、类型转换、计算字段,并自动推断 TypeScript 类型。

使用方式:在 createCollection 的 queryCollectionOptions 中传入 schema,例如 schema: todoSchema。校验失败会抛出 SchemaValidationError,可通过 error.type、error.message、error.issues 获取详情,便于表单集成。

关键概念:TInput 与 TOutput。若做类型转换(如 string → Date),TInput 必须是 TOutput 的超集,推荐用 z.union([A, B]) 作为输入,否则 collection.update() 传入 Date 时会校验失败。转换在 handler 运行前完成,handler 收到的是 TOutput。

限制:schema 只校验客户端变更,不校验服务端数据;提供 schema 后不要再传显式类型参数;转换应保持简单,因为每次变更都会同步执行。

详细参考:references/validation.md、references/transformations.md、references/tinput-toutput.md、references/error-handling.md。