Schema: accept a Standard Schema (Zod, Valibot, ArkType) as an Effect schema

未关闭
#10,853 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
22/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript
领域
api

调研方向

Read Schema.toStandardSchemaV1 and Schema.declare in packages/effect/src/Schema.ts first, since the inbound adapter mirrors the outbound one and must carry a JSON Schema where a declaration carries none. Then look at SchemaRepresentation.fromJsonSchemaDocument to see how a JSON Schema document is attached to a schema, since the OpenAPI output depends on it. Done means Schema.fromStandardSchemaV1 exists with InferOutput/InferInput types, decoding delegates to ~standard.validate with issue paths mapped, JSON Schema comes from ~standard.jsonSchema.input, and new tests in packages/effect/test cover Zod/Valibot/ArkType and the empty {} fallback.

由索引模型根据 Issue 内容生成。

描述

What is the problem this feature would solve?

Effect can turn its schemas into Standard Schemas (Schema.toStandardSchemaV1, Schema.toStandardJSONSchemaV1) but can't take one in. HttpApi, Rpc and AI tools only accept Effect schemas, so a team with existing Zod, Valibot or ArkType schemas has to rewrite them first.

Wrapping one in a declaration handles validation, but the JSON Schema is lost. A declaration has no JSON Schema of its own, so the field becomes {} even though Zod knows the shape ([email protected], [email protected]):

import { Schema } from "effect"
import * as z from "zod"

const User = z.object({ name: z.string(), age: z.number().int().min(18) })
const Body = Schema.Struct({
  user: Schema.declare((u): u is z.output<typeof User> => User.safeParse(u).success)
})

console.log(JSON.stringify(User["~standard"].jsonSchema.input({ target: "draft-2020-12" })))
console.log(JSON.stringify(Schema.toJsonSchemaDocument(Body).schema))
{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer","minimum":18,"maximum":9007199254740991}},"required":["name","age"]}
{"type":"object","properties":{"user":{}},"required":["user"],"additionalProperties":true}

HttpApi's OpenAPI output has the same {} for a payload wrapped this way.

What is the feature you are proposing to solve the problem?

An inbound adapter, something like Schema.fromStandardSchemaV1(schema), with the name and shape up to you.

  • The type is InferOutput and the encoded type is InferInput.
  • Decoding calls ~standard.validate, sync or async, and maps each issue to its path.
  • JSON Schema comes from ~standard.jsonSchema.input({ target: "draft-2020-12" }) when the schema has it, otherwise {}. Zod 4 and ArkType 2.2 have it, and Valibot has it through toStandardJsonSchema from @valibot/to-json-schema.
  • Standard Schema has no encode, so encoding either fails or passes the value through unchanged. Passthrough is right when the input and output types match, but that's your call.

The JSON Schema part is what userland can't do today, since a declaration can't supply its own. Happy to send a PR if you'd take one.

What alternatives have you considered?
  • A Schema.declareConstructor wrapper that maps issues. Validation works, including nested paths and async refinements, but the JSON Schema is {} as above.
  • Rebuilding the encoded side from the library's JSON Schema with SchemaRepresentation.fromJsonSchemaDocument, then Schema.decodeTo the declared type. The JSON Schema is right, but every value is validated twice. Effect's rebuilt schema runs first and stops at its first issue, so users see "Expected a value greater than or equal to 18" instead of the library's message, and pattern has to be ignored.
  • An Effect integration shipped by Zod. I maintain Zod and would rather not, since it'd hit the same JSON Schema problem and only cover Zod users.
主要语言
TypeScript
星标
16.7k
派生
808
平均合并
21 小时 22 分钟
30 天内合并 PR
486

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Effect-TS/effect 的其他 Issue

查看 Effect-TS/effect 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。