endpoints.documented.algebra

JsonSchemas

trait JsonSchemas extends AnyRef

An algebra interface for describing algebraic data types. Such descriptions can be interpreted to produce a JSON schema of the data type, a JSON encoder, a JSON decoder, etc.

A description contains the fields of a case class and their type, and the constructor names of a sealed trait.

For instance, consider the following record type:

case class User(name: String, age: Int)

Its description is the following:

object User {
implicit val schema: JsonSchema[User] = (
  field[String]("name") zip
  field[Int]("age")
).invmap((User.apply _).tupled)(Function.unlift(User.unapply))
}

The description says that the record type has two fields, the first one has type String and is named “name”, and the second one has type Int and name “age”.

To describe sum types you have to explicitly “tag” each alternative:

sealed trait Shape
case class Circle(radius: Double) extends Shape
case class Rectangle(width: Double, height: Double) extends Shape

object Shape {
  implicit val schema: JsonSchema[Shape] = {
    val circleSchema = field[Double]("radius").invmap(Circle)(Function.unlift(Circle.unapply))
    val rectangleSchema = (
      field[Double]("width") zip
      field[Double]("height")
    ).invmap((Rectangle.apply _).tupled)(Function.unlift(Rectangle.unapply))
    (circleSchema.tagged("Circle") orElse rectangleSchema.tagged("Rectangle"))
      .invmap[Shape] {
        case Left(circle) => circle
        case Right(rect)  => rect
      } {
        c: Circle    => Left(c)
        r: Rectangle => Right(r)
      }
  }
}
Source
JsonSchemas.scala
Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. JsonSchemas
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. abstract type JsonSchema[A]

    The JSON schema of a type A

  2. implicit final class JsonSchemaOps[A] extends AnyRef

    Convenient infix operations

  3. abstract type Record[A] <: JsonSchema[A]

    The JSON schema of a record type (case class) A

  4. implicit final class RecordOps[A] extends AnyRef

    Convenient infix operations

  5. abstract type Tagged[A] <: JsonSchema[A]

    A JSON schema containing the name of the type A.

    A JSON schema containing the name of the type A. Tagged schemas are useful to describe sum types (sealed traits).

  6. implicit final class TaggedOps[A] extends AnyRef

Abstract Value Members

  1. implicit abstract def arrayJsonSchema[C[X] <: Seq[X], A](implicit jsonSchema: JsonSchema[A], cbf: CanBuildFrom[_, A, C[A]]): JsonSchema[C[A]]

    A JSON schema for sequences

  2. implicit abstract def bigdecimalJsonSchema: JsonSchema[BigDecimal]

    A JSON schema for type BigDecimal

  3. implicit abstract def booleanJsonSchema: JsonSchema[Boolean]

    A JSON schema for type Boolean

  4. abstract def choiceTagged[A, B](taggedA: Tagged[A], taggedB: Tagged[B]): Tagged[Either[A, B]]

    The JSON schema of a coproduct made of the given alternative tagged records

  5. implicit abstract def doubleJsonSchema: JsonSchema[Double]

    A JSON schema for type Double

  6. abstract def field[A](name: String, documentation: Option[String] = None)(implicit tpe: JsonSchema[A]): Record[A]

    The JSON schema of a record with a single field name of type A

  7. implicit abstract def intJsonSchema: JsonSchema[Int]

    A JSON schema for type Int

  8. abstract def invmapJsonSchema[A, B](jsonSchema: JsonSchema[A], f: (A) ⇒ B, g: (B) ⇒ A): JsonSchema[B]

    Transforms the type of the JSON schema

  9. abstract def invmapRecord[A, B](record: Record[A], f: (A) ⇒ B, g: (B) ⇒ A): Record[B]

    Transforms the type of the JSON schema

  10. abstract def invmapTagged[A, B](taggedA: Tagged[A], f: (A) ⇒ B, g: (B) ⇒ A): Tagged[B]

    Transforms the type of the JSON schema

  11. implicit abstract def longJsonSchema: JsonSchema[Long]

    A JSON schema for type Long

  12. abstract def optField[A](name: String, documentation: Option[String] = None)(implicit tpe: JsonSchema[A]): Record[Option[A]]

    The JSON schema of a record with a single optional field name of type A

  13. implicit abstract def stringJsonSchema: JsonSchema[String]

    A JSON schema for type String

  14. abstract def taggedRecord[A](recordA: Record[A], tag: String): Tagged[A]

    Tags a schema for type A with the given tag name

  15. abstract def zipRecords[A, B](recordA: Record[A], recordB: Record[B]): Record[(A, B)]

    The JSON schema of a record merging the fields of the two given records

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  18. def toString(): String

    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped