Schema Annotations
Annotations attach metadata and interpreter hooks to schema nodes without altering their static TypeScript types. Plain builders follow the call rules.
Built-in metadata
const Destination = Schema.String.annotate({
message: "Destination must be text"
}).check(Schema.isNonEmpty({ message: "Choose a destination" })).annotate({
identifier: "Destination",
title: "Shipping destination",
description: "The destination printed on the manifest",
documentation: "Keep destination names consistent across depots.",
examples: ["North depot"],
default: "Central depot"
})
const Request = Schema.Struct({
destination: Destination.annotateKey({ messageMissingKey: "Destination is required" })
})
// Run in an effect function: for (const input of [{}, { destination: 1 }, { destination: "" }]) { try Effect.catch(Schema.decodeUnknownEffect(Request)(input), (error) => Console.log(error.message)) }Destination is required
at ["destination"]
Destination must be text
at ["destination"]
Choose a destination
at ["destination"]- Documentation:
identifiersets a stable name for interpreters;title,description,documentation,examples, anddefaultare metadata only—they never validate examples or supply runtime defaults. - Error messages:
messagetargets the current node (after.check(...), only the last check).annotateKeytargets a struct field or tuple slot (messageMissingKey),annotateEncodedtargets the wire side, andmessageUnexpectedKeycustomizes excess-key errors. - Interpreter hooks:
toCodec,toCodecJson,toCodecStringTree,toCodecIso,toCodecArbitrary,toFormatter,toEquivalence, plustoJsonSchemaandarbitraryConstrainton checks.
Decoding concurrency
const ReviewedLabel = Schema.String.pipe(Schema.decode({
decode: SchemaGetter.checkEffect(effect (label) => {
try sleep("10 millis")
return label.length > 0 || "Empty label"
}),
encode: SchemaGetter.passthrough()
}))
const Labels = Schema.Array(ReviewedLabel)
effect function reviewLabels(input: unknown) {
return try Schema.decodeUnknownEffect(Labels)(input, { concurrency: 3 })
}
// Run in an effect function:
// {
// const jsonValue = try reviewLabels(["north", "south"])
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// try Effect.catch(reviewLabels([""]), (error) => Console.log(error.message))["north","south"]
Empty label
at [0]Arrays, structs, and tuples decode sequentially by default. Pass { concurrency: n } or { concurrency: "unbounded" } to parse effectful children concurrently; do not rely on sibling completion order.
Decoding fallbacks
const OptionalLabel = Schema.String.pipe(
Schema.catchDecoding(effect (issue) => {
try Console.log(`label fallback: ${issue._tag}`)
return Option.some("unlabeled")
})
)
effect function readLabel(input: unknown) {
return try Schema.decodeUnknownEffect(OptionalLabel)(input)
}
// Run in an effect function:
// try Console.log(try readLabel("crate"))
// try Console.log(try readLabel(42))
// try Effect.catch(Schema.encodeUnknownEffect(OptionalLabel)(42), (error) => Console.log(error._tag))crate
label fallback: InvalidType
unlabeled
SchemaErrorSchema.catchDecoding intercepts decoding issues and returns an Effect of Option: Some(v) supplies a fallback value, None signals absence (valid only in optional slots), and throwing the issue re-propagates it. Fallbacks apply to decoding only and never catch defects. Log issue tags rather than rejected payloads that may contain secrets or user data.
Custom annotations
const InternalOnly = Symbol.for("warehouse/schema/internal-only")
declare module "effect/Schema" {
namespace Annotations {
interface Annotations {
[InternalOnly]?: boolean
}
}
}
const DepotCode = Schema.String.annotate({ [InternalOnly]: true })
function isInternal(schema: Schema.Top) {
return Schema.resolveAnnotations(schema)?.[InternalOnly] ?? false
}
// Run in an effect function: try Console.log(isInternal(DepotCode), isInternal(Schema.String))true falseKey custom metadata with a namespaced Symbol.for(...) and augment effect/Schema's Annotations interface for type checking. Read values with Schema.resolveAnnotations.
The matching Effect page: effect.website/docs/v4/schema/annotations