Reference Structured data
Schema[A]
The JSON Schema of a type with its circe codec. parse and
Tool use it; case classes and sealed traits get one automatically.
Reference#
Schema[A]#
trait Schema[A]
def node: SchemaNode
def decoder: Decoder[A]
def encoder: Encoder[A]
def jsonSchema: JsonObject
def strict: ValidatedNel[SchemaIssue, JsonObject]
def emap[B](f: A => Either[String, B])(g: B => A): Schema[B]
def imap[B](f: A => B)(g: B => A): Schema[B]
def withCodec(d: Decoder[A], e: Encoder[A]): Schema[A]
jsonSchema: the JSON Schema sent to the API.strict: the schema, or every reason it cannot be used strictly.emap, imap: a schema for a type built from this one, with or without validation.withCodec: the same schema with your own decoder and encoder.
Derivation#
implicit def derived[A]: Schema[A]
Caveats
- A case class becomes a closed object with every field required. An
Optionfield is nullable, but still required. - A sealed trait of case classes becomes
anyOf, tagged by atypefield. A sealed trait of case objects becomes a string enum. - Recursive types and
Maphave no strict schema.
Annotations#
final class description(val text: String) extends StaticAnnotation
final class name(val value: String) extends StaticAnnotation
final class discriminator(val field: String) extends StaticAnnotation
@description: a description of a field or type, which the model reads.@name: a different name on the wire.@discriminator: a sealed trait's tag field, instead oftype.
Hand-written schemas#
def apply[A](implicit schema: Schema[A]): Schema[A]
def instance[A](n: SchemaNode, d: Decoder[A], e: Encoder[A]): Schema[A]
def unchecked[A](schema: JsonObject)(implicit d: Decoder[A], e: Encoder[A]): Schema[A]
Caveats
uncheckedtakes a JSON Schema as it is, without checking it is strict.
Usage#
Describing a type#
final case class Book(@description("Full title") title: String, year: Option[Int], genre: Genre)
client.messages.parse[Book](request) // no instance to declare
With Play JSON formats, use PlaySchema instead.
Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.