Skip to main content
Version: 2.x

Introduction to ZIO Schema

ZIO Schema is a ZIO-based library for modeling the schema of data structures as first-class values.

Development CI Badge Sonatype Releases Sonatype Snapshots javadoc ZIO Schema

Introductionโ€‹

ZIO Schema helps us solve some of the most common problems in distributed computing, such as serialization, deserialization, and data migration.

It turns a compile-time construct (the type of a data structure) into a runtime construct (a value that can be read, manipulated, and composed at runtime). A schema is a structure of a data type. ZIO Schema reifies the concept of structure for data types. It makes a high-level description of any data type and makes them first-class values.

Creating a schema for a data type helps us to write codecs for that data type. ZIO Schema ships codecs for JSON, Protobuf, Avro, BSON, MessagePack, Thrift, and XML, and the same schema drives all of them.

What Problems Does ZIO Schema Solve?โ€‹

With schema descriptions that can be automatically derived for case classes and sealed traits, ZIO Schema provides powerful features for free:

  1. Generic programming without writing macros yourself. The derivation macros inspect your types once, and everything else works on the resulting schema value:
    1. Creating serialization and deserialization codecs for any supported protocol (JSON, Protobuf, etc.)
    2. Deriving an Ordering from the structure of the data, and deriving your own type classes with a Deriver
    3. Default values for data types
  2. Automate ETL (Extract, Transform, Load) pipelines
    1. Diffing: diffing between two values of the same type
    2. Patching: applying a diff to a value to update it
    3. Migration: migrating values from one type to another
  3. Computations as data: Not only can we turn types into values, but we can also turn computations into values. This opens up a whole new world of possibilities concerning distributed computing.

When our data structures need to be serialized, deserialized, persisted, or transported across the wire, then ZIO Schema lets us focus on data modeling and automatically tackle all the low-level, messy details for us.

A growing number of ZIO libraries build on ZIO Schema, including ZIO Flow, ZIO Redis, ZIO SQL and ZIO DynamoDB.

Installationโ€‹

Every project needs two modules: zio-schema, which holds Schema itself, and zio-schema-derivation, which derives schemas for case classes and sealed traits. On the JVM, add them to build.sbt:

libraryDependencies ++= Seq(
// Schema, DynamicValue, diff and patch, migration, validation (JVM, Scala.js, Native)
"dev.zio" %% "zio-schema" % "1.9.2",

// DeriveSchema.gen and the Deriver API for your own type classes (JVM, Scala.js, Native)
"dev.zio" %% "zio-schema-derivation" % "1.9.2"
)

In a Scala.js or Scala Native project, use the %%% operator instead:

libraryDependencies ++= Seq(
"dev.zio" %%% "zio-schema" % "1.9.2",

"dev.zio" %%% "zio-schema-derivation" % "1.9.2"
)

On Scala 2, schema derivation uses macros that need scala-reflect at compile time. Scala 3 has no scala-reflect artifact, so a build that cross-compiles adds it only for Scala 2:

libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) => Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided)
case _ => Seq.empty
}
}

ZIO Schema is published for Scala 2.12, 2.13, and 3.

Scala 3 Projects Need Scala 3.9 or Later

The Scala 3 artifacts are compiled with Scala 3.9, so a Scala 3 project must use Scala 3.9 or later to depend on them.

Getting Started walks through a first program: deriving a schema, encoding JSON, and validating values. The Modules section below lists the optional modules for codecs, optics, and testing.

Modulesโ€‹

Every other feature lives in its own optional module, and we add only the modules we use. All modules share the core module's version, and each one already depends on zio-schema and zio-schema-derivation.

Codecsโ€‹

A codec module encodes and decodes values in one data format, using their schemas. On the JVM, every codec is available. Copy the block and delete the lines you don't need:

libraryDependencies ++= Seq(
// JSON codec (JVM, Scala.js, Native)
"dev.zio" %% "zio-schema-json" % "1.9.2",

// Protocol Buffers codec (JVM, Scala.js, Native)
"dev.zio" %% "zio-schema-protobuf" % "1.9.2",

// XML codec (JVM, Scala.js, Native)
"dev.zio" %% "zio-schema-xml" % "1.9.2",

// Apache Avro codec (JVM only)
"dev.zio" %% "zio-schema-avro" % "1.9.2",

// BSON codec for MongoDB (JVM only)
"dev.zio" %% "zio-schema-bson" % "1.9.2",

// MessagePack codec (JVM only)
"dev.zio" %% "zio-schema-msg-pack" % "1.9.2",

// Apache Thrift codec (JVM only)
"dev.zio" %% "zio-schema-thrift" % "1.9.2"
)

In a Scala.js or Scala Native project, only the JSON, Protocol Buffers, and XML codecs are available. The Avro, BSON, MessagePack, and Thrift codecs have no Scala.js or Scala Native artifacts:

libraryDependencies ++= Seq(
// JSON codec
"dev.zio" %%% "zio-schema-json" % "1.9.2",

// Protocol Buffers codec
"dev.zio" %%% "zio-schema-protobuf" % "1.9.2",

// XML codec
"dev.zio" %%% "zio-schema-xml" % "1.9.2"
)

Each codec has its own page: JSON, Protocol Buffers, XML, Apache Avro, BSON, MessagePack, and Apache Thrift.

Opticsโ€‹

The optics module derives lenses, prisms, and traversals from a schema, for reading and updating nested data (JVM, Scala.js, Native):

libraryDependencies += "dev.zio" %% "zio-schema-optics" % "1.9.2"

In a Scala.js or Scala Native project, use %%% instead of %%. Optics Derivation describes the module.

Testingโ€‹

The ZIO Test module derives ZIO Test generators (Gen) from a schema, for property-based tests (JVM, Scala.js, Native). Tests are its only users, so it goes in the Test configuration:

libraryDependencies += "dev.zio" %% "zio-schema-zio-test" % "1.9.2" % Test

In a Scala.js or Scala Native project, use %%% instead of %%. Derivation of ZIO Test Generators describes the module.

Internal Modulesโ€‹

The build also publishes zio-schema-macros, an internal helper that the other modules pull in, so you never add it yourself.

Exampleโ€‹

In this example, we derive a schema for Person and then encode a Person value as a stream of Protocol Buffers bytes. The example needs the zio-schema-protobuf module:

import zio._
import zio.stream._
import zio.schema.codec.{BinaryCodec, ProtobufCodec}
import zio.schema.{DeriveSchema, Schema}

import java.io.IOException

final case class Person(name: String, age: Int)

object Person {
implicit val schema: Schema[Person] = DeriveSchema.gen
val protobufCodec: BinaryCodec[Person] = ProtobufCodec.protobufCodec
}

object Main extends ZIOAppDefault {
def run: ZIO[Any, IOException, Unit] =
ZStream
.succeed(Person("John", 43))
.via(Person.protobufCodec.streamEncoder)
.runCollect
.flatMap(x =>
Console.printLine(s"Encoded data with protobuf codec: ${toHex(x)}")
)

def toHex(chunk: Chunk[Byte]): String =
chunk.map("%02X".format(_)).mkString
}

Here is the output of running the above program:

Encoded data with protobuf codec: 0A044A6F686E102B

Resourcesโ€‹

ZIO Chat

Hi, fellow coder! ๐ŸŒŸ Welcome to your ZIO adventure with the ZIO Chat Bot. ๐Ÿš€ Let's master functional Scala together. From referential transparency to concurrent prowess, I've got your back. Ready to dive in? Your questions, my insights โ€“ let's conquer ZIO! ๐Ÿ’ก๐Ÿค–