feat(schema): add JsonObject schema (#7337)

This commit is contained in:
Giulio Canti
2026-08-18 18:21:20 +02:00
committed by GitHub
parent 44d66f8d7c
commit b19ccc7325
11 changed files with 63 additions and 33 deletions
+6
View File
@@ -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)`.
+22
View File
@@ -16222,6 +16222,28 @@ export const Json: Codec<Json> = 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.
*
+2 -5
View File
@@ -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<Record<string, unknown>>
): Option.Option<Readonly<Record<string, Schema.Json>>> {
@@ -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
+18 -18
View File
@@ -173,7 +173,7 @@ export class ResultMeta extends Schema.Opaque<ResultMeta>()(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<NotificationMeta>()(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<Resource>(
* 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<ResourceTemplate>(
/**
* 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<ResourceContents>()(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<Prompt>(
* 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<TextContent>()(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<ImageContent>()(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<AudioContent>()(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<EmbeddedResource>()(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<ToolJsonSchema> = 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<Tool>(
* 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<ToolUseContent>("@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<ToolResultContent>("@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<SamplingMessage>()(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<CreateMessageResult>(
)({
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<Root>(
/**
* Optional additional metadata associated with the root.
*/
_meta: optional(Schema.Record(Schema.String, Schema.Json))
_meta: optional(Schema.JsonObject)
}) {}
/**
@@ -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({
@@ -23,7 +23,7 @@ export const optional = <S extends Schema.Constraint>(
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])
@@ -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)
}
}) {}
@@ -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({
@@ -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]
)
})
})
@@ -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,
{
@@ -378,6 +378,11 @@ describe("Schema", () => {
>()
})
it("JsonObject", () => {
const schema = Schema.JsonObject
expect(schema).type.toBe<Schema.$Record<Schema.String, Schema.Codec<Schema.Json>>>()
})
it("NonEmptyArray", () => {
const schema = Schema.NonEmptyArray(Schema.FiniteFromString.pipe(Schema.brand("a")))
expect(schema.make).type.toBe<