Drop unused ID Zod statics (#26740)
This commit is contained in:
parent
d8060dc9ad
commit
5ef72e1101
4 changed files with 16 additions and 17 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
|
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
|
||||||
import { SessionID } from "@/session/schema"
|
import { SessionID } from "@/session/schema"
|
||||||
|
import { Schema } from "effect"
|
||||||
|
|
||||||
|
const decodeSessionID = Schema.decodeUnknownSync(SessionID)
|
||||||
|
|
||||||
export async function validateSession(input: {
|
export async function validateSession(input: {
|
||||||
url: string
|
url: string
|
||||||
|
|
@ -10,9 +13,11 @@ export async function validateSession(input: {
|
||||||
}) {
|
}) {
|
||||||
if (!input.sessionID) return
|
if (!input.sessionID) return
|
||||||
|
|
||||||
const result = SessionID.zod.safeParse(input.sessionID)
|
let sessionID: SessionID
|
||||||
if (!result.success) {
|
try {
|
||||||
throw new Error(`Invalid session ID: ${result.error.issues.at(0)?.message ?? "unknown error"}`)
|
sessionID = decodeSessionID(input.sessionID)
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Invalid session ID: ${error instanceof Error ? error.message : "unknown error"}`, { cause: error })
|
||||||
}
|
}
|
||||||
|
|
||||||
await createOpencodeClient({
|
await createOpencodeClient({
|
||||||
|
|
@ -20,5 +25,5 @@ export async function validateSession(input: {
|
||||||
directory: input.directory,
|
directory: input.directory,
|
||||||
fetch: input.fetch,
|
fetch: input.fetch,
|
||||||
headers: input.headers,
|
headers: input.headers,
|
||||||
}).session.get({ sessionID: result.data }, { throwOnError: true })
|
}).session.get({ sessionID }, { throwOnError: true })
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
|
|
||||||
import { Identifier } from "@/id/id"
|
import { Identifier } from "@/id/id"
|
||||||
import { zod } from "@opencode-ai/core/effect-zod"
|
|
||||||
import { withStatics } from "@opencode-ai/core/schema"
|
import { withStatics } from "@opencode-ai/core/schema"
|
||||||
|
|
||||||
const workspaceIdSchema = Schema.String.check(Schema.isStartsWith("wrk")).pipe(Schema.brand("WorkspaceID"))
|
const workspaceIdSchema = Schema.String.check(Schema.isStartsWith("wrk")).pipe(Schema.brand("WorkspaceID"))
|
||||||
|
|
@ -11,6 +10,5 @@ export type WorkspaceID = typeof workspaceIdSchema.Type
|
||||||
export const WorkspaceID = workspaceIdSchema.pipe(
|
export const WorkspaceID = workspaceIdSchema.pipe(
|
||||||
withStatics((schema: typeof workspaceIdSchema) => ({
|
withStatics((schema: typeof workspaceIdSchema) => ({
|
||||||
ascending: (id?: string) => schema.make(Identifier.ascending("workspace", id)),
|
ascending: (id?: string) => schema.make(Identifier.ascending("workspace", id)),
|
||||||
zod: zod(schema),
|
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,12 @@
|
||||||
import { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
|
|
||||||
import { Identifier } from "@/id/id"
|
import { Identifier } from "@/id/id"
|
||||||
import { zod } from "@opencode-ai/core/effect-zod"
|
|
||||||
import { withStatics } from "@opencode-ai/core/schema"
|
import { withStatics } from "@opencode-ai/core/schema"
|
||||||
|
|
||||||
export const SessionID = Schema.String.check(Schema.isStartsWith("ses")).pipe(
|
export const SessionID = Schema.String.check(Schema.isStartsWith("ses")).pipe(
|
||||||
Schema.brand("SessionID"),
|
Schema.brand("SessionID"),
|
||||||
withStatics((s) => ({
|
withStatics((s) => ({
|
||||||
descending: (id?: string) => s.make(Identifier.descending("session", id)),
|
descending: (id?: string) => s.make(Identifier.descending("session", id)),
|
||||||
zod: zod(s),
|
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -18,7 +16,6 @@ export const MessageID = Schema.String.check(Schema.isStartsWith("msg")).pipe(
|
||||||
Schema.brand("MessageID"),
|
Schema.brand("MessageID"),
|
||||||
withStatics((s) => ({
|
withStatics((s) => ({
|
||||||
ascending: (id?: string) => s.make(Identifier.ascending("message", id)),
|
ascending: (id?: string) => s.make(Identifier.ascending("message", id)),
|
||||||
zod: zod(s),
|
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,7 +25,6 @@ export const PartID = Schema.String.check(Schema.isStartsWith("prt")).pipe(
|
||||||
Schema.brand("PartID"),
|
Schema.brand("PartID"),
|
||||||
withStatics((s) => ({
|
withStatics((s) => ({
|
||||||
ascending: (id?: string) => s.make(Identifier.ascending("part", id)),
|
ascending: (id?: string) => s.make(Identifier.ascending("part", id)),
|
||||||
zod: zod(s),
|
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,20 +15,20 @@ import { WorkspaceID } from "../../src/control-plane/schema"
|
||||||
// schema we assert:
|
// schema we assert:
|
||||||
// 1. The Effect decoder (`Schema.decodeUnknownSync`) accepts valid input.
|
// 1. The Effect decoder (`Schema.decodeUnknownSync`) accepts valid input.
|
||||||
// 2. The derived Zod (`X.zod.parse`) accepts the same input and returns the
|
// 2. The derived Zod (`X.zod.parse`) accepts the same input and returns the
|
||||||
// same shape.
|
// same shape for schemas that still expose Zod statics.
|
||||||
// 3. Clearly-invalid input is rejected by both paths.
|
// 3. Clearly-invalid input is rejected by both paths where both exist.
|
||||||
//
|
//
|
||||||
// The point is to lock down the Schema <-> Zod bridge so a future edit to
|
// The point is to lock down the Schema <-> Zod bridge so a future edit to
|
||||||
// any input schema can't silently drop or widen a field on one side.
|
// any input schema can't silently drop or widen a field on one side.
|
||||||
|
|
||||||
// Representative valid IDs — the branded schemas require the right prefix
|
// Representative valid IDs — the branded schemas require the right prefix
|
||||||
// (see src/id/id.ts).
|
// (see src/id/id.ts).
|
||||||
const sessionID = SessionID.zod.parse("ses_01J5Y5H0AH4Q4NXJ6P4C3P5V2K")
|
const sessionID = Schema.decodeUnknownSync(SessionID)("ses_01J5Y5H0AH4Q4NXJ6P4C3P5V2K")
|
||||||
const sessionIDChild = SessionID.zod.parse("ses_01J5Y5H0AH4Q4NXJ6P4C3P5V2L")
|
const sessionIDChild = Schema.decodeUnknownSync(SessionID)("ses_01J5Y5H0AH4Q4NXJ6P4C3P5V2L")
|
||||||
const messageID = MessageID.zod.parse("msg_01J5Y5H0AH4Q4NXJ6P4C3P5V2M")
|
const messageID = Schema.decodeUnknownSync(MessageID)("msg_01J5Y5H0AH4Q4NXJ6P4C3P5V2M")
|
||||||
const partID = PartID.zod.parse("prt_01J5Y5H0AH4Q4NXJ6P4C3P5V2N")
|
const partID = Schema.decodeUnknownSync(PartID)("prt_01J5Y5H0AH4Q4NXJ6P4C3P5V2N")
|
||||||
const projectID = ProjectID.zod.parse("proj-alpha")
|
const projectID = ProjectID.zod.parse("proj-alpha")
|
||||||
const workspaceID = WorkspaceID.zod.parse("wrk-primary")
|
const workspaceID = Schema.decodeUnknownSync(WorkspaceID)("wrk-primary")
|
||||||
|
|
||||||
function decodeUnknown<S extends Schema.Top>(schema: S) {
|
function decodeUnknown<S extends Schema.Top>(schema: S) {
|
||||||
const decode = Schema.decodeUnknownSync(schema as any)
|
const decode = Schema.decodeUnknownSync(schema as any)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue