From b19ccc7325469a5dea091349bda4ae2ec00ec865 Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Tue, 18 Aug 2026 18:21:20 +0200 Subject: [PATCH] feat(schema): add JsonObject schema (#7337) --- .changeset/tidy-json-objects.md | 6 ++++ packages/effect/src/Schema.ts | 22 ++++++++++++ packages/effect/src/SchemaRepresentation.ts | 7 ++-- packages/effect/src/unstable/ai/McpSchema.ts | 36 +++++++++---------- .../ai/internal/mcpProtocol/v2025_11_25.ts | 2 +- .../ai/internal/mcpSchema/v2024_11_05.ts | 2 +- .../ai/internal/mcpSchema/v2025_03_26.ts | 4 +-- .../ai/internal/mcpSchema/v2025_06_18.ts | 4 +-- .../ai/internal/mcpSchema/v2025_11_25.ts | 4 +-- .../test/schema/toJsonSchemaDocument.test.ts | 4 +-- packages/effect/typetest/schema/Schema.tst.ts | 5 +++ 11 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 .changeset/tidy-json-objects.md diff --git a/.changeset/tidy-json-objects.md b/.changeset/tidy-json-objects.md new file mode 100644 index 0000000000..80e7463706 --- /dev/null +++ b/.changeset/tidy-json-objects.md @@ -0,0 +1,6 @@ +--- +"effect": patch +--- + +Add `Schema.JsonObject` for readonly string-keyed records containing JSON-compatible values. This provides a canonical, +reusable schema instead of requiring callers to repeatedly compose `Schema.Record(Schema.String, Schema.Json)`. diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index 0b1583d3d9..fee280161f 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -16222,6 +16222,28 @@ export const Json: Codec = make(SchemaAST.annotate(SchemaAST.Json, { }) })) +/** + * Schema for readonly string-keyed records whose values are JSON-compatible. + * + * **When to use** + * + * Use when you need to validate a JSON object rather than any JSON value. + * + * **Example** (Validating a JSON object) + * + * ```ts import.meta.vitest + * import { Option, Schema } from "effect" + * + * Schema.decodeUnknownOption(Schema.JsonObject)({ key: [1, true, null] }) // => Option.some({ key: [1, true, null] }) + * Schema.decodeUnknownOption(Schema.JsonObject)([1, 2, 3]) // => Option.none() + * ``` + * + * @see {@link Json} for a schema that also accepts JSON arrays and primitive values + * @category schemas + * @since 4.0.0 + */ +export const JsonObject = Record(String, Json) + /** * Reviver for persisted `Json` declarations. * diff --git a/packages/effect/src/SchemaRepresentation.ts b/packages/effect/src/SchemaRepresentation.ts index d77058a4a0..20ce755505 100644 --- a/packages/effect/src/SchemaRepresentation.ts +++ b/packages/effect/src/SchemaRepresentation.ts @@ -839,9 +839,6 @@ const CheckRepresentationAnnotationSchema = Schema.Struct({ schemas: Schema.optional(RepresentationsSchema) }) -const LiveAnnotationsSchema = Schema.Record(Schema.String, Schema.Unknown) -const JsonAnnotationsSchema = Schema.Record(Schema.String, Schema.Json) - function pruneAnnotations( annotations: Readonly> ): Option.Option>> { @@ -854,8 +851,8 @@ function pruneAnnotations( return Object.keys(out).length === 0 ? Option.none() : Option.some(out) } -const AnnotationsSchema = Schema.optional(LiveAnnotationsSchema).pipe( - Schema.encodeTo(Schema.optionalKey(JsonAnnotationsSchema), { +const AnnotationsSchema = Schema.optional(Schema.Record(Schema.String, Schema.Unknown)).pipe( + Schema.encodeTo(Schema.optionalKey(Schema.JsonObject), { decode: SchemaGetter.passthroughSubtype(), encode: SchemaGetter.transformOptional((annotations) => Option.isNone(annotations) || annotations.value === undefined diff --git a/packages/effect/src/unstable/ai/McpSchema.ts b/packages/effect/src/unstable/ai/McpSchema.ts index 462433ed35..2c44f02387 100644 --- a/packages/effect/src/unstable/ai/McpSchema.ts +++ b/packages/effect/src/unstable/ai/McpSchema.ts @@ -173,7 +173,7 @@ export class ResultMeta extends Schema.Opaque()(Schema.Struct({ * This result property is reserved by the protocol to allow clients and * servers to attach additional metadata to their responses. */ - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -192,7 +192,7 @@ export class NotificationMeta extends Schema.Opaque()(Schema.S * This parameter name is reserved by MCP to allow clients and servers to * attach additional metadata to their notifications. */ - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -919,7 +919,7 @@ export class Resource extends Schema.Class( * This parameter name is reserved by MCP to allow clients and servers to * attach additional metadata to resources. */ - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) }) {} /** @@ -969,7 +969,7 @@ export class ResourceTemplate extends Schema.Class( /** * Optional additional metadata for the client. */ - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) }) {} /** @@ -990,7 +990,7 @@ export class ResourceContents extends Schema.Opaque()(Schema.S /** * Optional additional metadata for the client. */ - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -1235,7 +1235,7 @@ export class Prompt extends Schema.Class( * Icons that clients can display for this prompt. */ icons: optional(Schema.Array(Icon)), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) }) {} /** @@ -1254,7 +1254,7 @@ export class TextContent extends Schema.Opaque()(Schema.Struct({ * Optional annotations for the client. */ annotations: optional(Annotations), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -1278,7 +1278,7 @@ export class ImageContent extends Schema.Opaque()(Schema.Struct({ * Optional annotations for the client. */ annotations: optional(Annotations), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -1302,7 +1302,7 @@ export class AudioContent extends Schema.Opaque()(Schema.Struct({ * Optional annotations for the client. */ annotations: optional(Annotations), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -1323,7 +1323,7 @@ export class EmbeddedResource extends Schema.Opaque()(Schema.S * Optional annotations for the client. */ annotations: optional(Annotations), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -1544,10 +1544,10 @@ export type ToolJsonSchema = Schema.JsonObject & { export const ToolJsonSchema: Schema.Codec = Schema.StructWithRest( Schema.Struct({ type: Schema.Literal("object"), - properties: optional(Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Json))), + properties: optional(Schema.Record(Schema.String, Schema.JsonObject)), required: optional(Schema.Array(Schema.String)) }), - [Schema.Record(Schema.String, Schema.Json)] + [Schema.JsonObject] ) /** @@ -1592,7 +1592,7 @@ export class Tool extends Schema.Class( * This parameter name is reserved by MCP to allow clients and servers to * attach additional metadata to resources. */ - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) }) {} /** @@ -1814,7 +1814,7 @@ export class ToolUseContent extends Schema.Class("@effect/ai/Mcp * Arguments supplied to the tool. */ input: Schema.Record(Schema.String, Schema.Unknown), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) }) {} /** @@ -1841,7 +1841,7 @@ export class ToolResultContent extends Schema.Class("@effect/ * Whether tool execution ended in an error. */ isError: optional(Schema.Boolean), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) }) {} /** @@ -1867,7 +1867,7 @@ export const SamplingMessageContentBlock = Schema.Union([ export class SamplingMessage extends Schema.Opaque()(Schema.Struct({ role: Role, content: Schema.Union([SamplingMessageContentBlock, Schema.Array(SamplingMessageContentBlock)]), - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) })) {} /** @@ -1984,7 +1984,7 @@ export class CreateMessageResult extends Schema.Class( )({ role: Role, content: Schema.Union([SamplingMessageContentBlock, Schema.Array(SamplingMessageContentBlock)]), - _meta: optional(Schema.Record(Schema.String, Schema.Json)), + _meta: optional(Schema.JsonObject), /** * The name of the model that generated the message. */ @@ -2196,7 +2196,7 @@ export class Root extends Schema.Class( /** * Optional additional metadata associated with the root. */ - _meta: optional(Schema.Record(Schema.String, Schema.Json)) + _meta: optional(Schema.JsonObject) }) {} /** diff --git a/packages/effect/src/unstable/ai/internal/mcpProtocol/v2025_11_25.ts b/packages/effect/src/unstable/ai/internal/mcpProtocol/v2025_11_25.ts index 7033db4664..e37feca1ee 100644 --- a/packages/effect/src/unstable/ai/internal/mcpProtocol/v2025_11_25.ts +++ b/packages/effect/src/unstable/ai/internal/mcpProtocol/v2025_11_25.ts @@ -104,7 +104,7 @@ const projectContent = Effect.fnUntraced(function*(content: typeof PublicMcpSche }) const projectStructuredContent = Effect.fnUntraced(function*(content: Schema.Json | undefined) { - if (content === undefined || Schema.is(Schema.Record(Schema.String, Schema.Json))(content)) { + if (content === undefined || Schema.is(Schema.JsonObject)(content)) { return content } return yield* new McpCore.UnsupportedByProtocol({ diff --git a/packages/effect/src/unstable/ai/internal/mcpSchema/v2024_11_05.ts b/packages/effect/src/unstable/ai/internal/mcpSchema/v2024_11_05.ts index 43d0bb05b9..ea4dc4742a 100644 --- a/packages/effect/src/unstable/ai/internal/mcpSchema/v2024_11_05.ts +++ b/packages/effect/src/unstable/ai/internal/mcpSchema/v2024_11_05.ts @@ -23,7 +23,7 @@ export const optional = ( encode: SchemaGetter.transformOptional(Option.flatMap(Option.fromUndefinedOr)) }) ) -const JsonObject = Schema.Record(Schema.String, Schema.Json) +const JsonObject = Schema.JsonObject export const RequestId = Schema.Union([Schema.String, Schema.Finite]) export const ProgressToken = Schema.Union([Schema.String, Schema.Finite]) diff --git a/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_03_26.ts b/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_03_26.ts index ee23ed9722..64b4bdbd86 100644 --- a/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_03_26.ts +++ b/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_03_26.ts @@ -132,7 +132,7 @@ export class CallTool extends Rpc.make("tools/call", { payload: { ...Previous.RequestMeta.fields, name: Schema.String, - arguments: optional(Schema.Record(Schema.String, Schema.Json)) + arguments: optional(Schema.JsonObject) } }) {} @@ -148,7 +148,7 @@ export class CreateMessage extends Rpc.make("sampling/createMessage", { temperature: optional(Schema.Finite), maxTokens: Schema.Finite, stopSequences: optional(Schema.Array(Schema.String)), - metadata: optional(Schema.Record(Schema.String, Schema.Json)) + metadata: optional(Schema.JsonObject) } }) {} diff --git a/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_06_18.ts b/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_06_18.ts index 1d064bc2a5..e741e868a7 100644 --- a/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_06_18.ts +++ b/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_06_18.ts @@ -16,7 +16,7 @@ export * from "./v2025_03_26.ts" export const protocolVersion = "2025-06-18" const optional = Previous.optional -const JsonObject = Schema.Record(Schema.String, Schema.Json) +const JsonObject = Schema.JsonObject const Meta = optional(JsonObject) export const Implementation = Schema.Struct({ @@ -152,7 +152,7 @@ const ToolJsonSchema = Schema.StructWithRest( properties: optional(Schema.Record(Schema.String, JsonObject)), required: optional(Schema.Array(Schema.String)) }), - [Schema.Record(Schema.String, Schema.Json)] + [Schema.JsonObject] ) export const Tool = Schema.Struct({ diff --git a/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_11_25.ts b/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_11_25.ts index 889034f747..fbbce35621 100644 --- a/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_11_25.ts +++ b/packages/effect/src/unstable/ai/internal/mcpSchema/v2025_11_25.ts @@ -16,7 +16,7 @@ export * from "./v2025_06_18.ts" export const protocolVersion = "2025-11-25" const optional = Previous.optional -const JsonObject = Schema.Record(Schema.String, Schema.Json) +const JsonObject = Schema.JsonObject const Meta = optional(JsonObject) export const Icon = Schema.Struct({ @@ -337,7 +337,7 @@ export const URLElicitationRequiredError = Schema.Struct({ Schema.Struct({ elicitations: Schema.Array(ElicitRequestURLParams) }), - [Schema.Record(Schema.String, Schema.Json)] + [Schema.JsonObject] ) }) }) diff --git a/packages/effect/test/schema/toJsonSchemaDocument.test.ts b/packages/effect/test/schema/toJsonSchemaDocument.test.ts index 7d02ff598c..7c104dbd86 100644 --- a/packages/effect/test/schema/toJsonSchemaDocument.test.ts +++ b/packages/effect/test/schema/toJsonSchemaDocument.test.ts @@ -2837,8 +2837,8 @@ describe("toJsonSchemaDocument", () => { ) }) - it("Record(String, Json)", () => { - const schema = Schema.Record(Schema.String, Schema.Json) + it("JsonObject", () => { + const schema = Schema.JsonObject assertJsonSchemaDocument( schema, { diff --git a/packages/effect/typetest/schema/Schema.tst.ts b/packages/effect/typetest/schema/Schema.tst.ts index 5afd42204c..729751b218 100644 --- a/packages/effect/typetest/schema/Schema.tst.ts +++ b/packages/effect/typetest/schema/Schema.tst.ts @@ -378,6 +378,11 @@ describe("Schema", () => { >() }) + it("JsonObject", () => { + const schema = Schema.JsonObject + expect(schema).type.toBe>>() + }) + it("NonEmptyArray", () => { const schema = Schema.NonEmptyArray(Schema.FiniteFromString.pipe(Schema.brand("a"))) expect(schema.make).type.toBe<