JsonSchemas
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")
).xmap((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").xmap(Circle)(Function.unlift(Circle.unapply))
val rectangleSchema = (
field[Double]("width") zip
field[Double]("height")
).xmap((Rectangle.apply _).tupled)(Function.unlift(Rectangle.unapply))
(circleSchema.tagged("Circle") orElse rectangleSchema.tagged("Rectangle"))
.xmap[Shape] {
case Left(circle) => circle
case Right(rect) => rect
} {
case c: Circle => Left(c)
case r: Rectangle => Right(r)
}
}
}
Type members
Classlikes
Documentation related methods for annotating schemas. Encoder and decoder interpreters ignore this information.
Documentation related methods for annotating schemas. Encoder and decoder interpreters ignore this information.
Inherited classlikes
Extension methods for values of type F[A]
for which there is an implicit
InvariantFunctor[F]
instance.
Extension methods for values of type F[A]
for which there is an implicit
InvariantFunctor[F]
instance.
- Inherited from:
- InvariantFunctorSyntax
- Inherited from:
- PartialInvariantFunctorSyntax
Types
A more specific type of JSON schema for enumerations, i.e. types that have a specific set of valid values
A more specific type of JSON schema for enumerations, i.e. types that have a specific set of valid values
Values of type Enum[A]
can be constructed by the operations:
- Note:
This type has implicit methods provided by the EnumOps class.
The JSON schema of a type A
The JSON schema of a type A
JSON schemas can be interpreted as encoders serializing values of type A
into JSON,
decoders de-serializing JSON documents into values of type A
, or documentation rendering
the underlying JSON schema.
The JsonSchemas
trait provides implicit definitions of JsonSchema[A]
for basic types (Int
,
Double
, String
, etc.), and operations such as field, optField, or enumeration,
which construct more complex JSON schemas.
- Note:
This type has implicit methods provided by the PartialInvariantFunctorSyntax, InvariantFunctorSyntax, and JsonSchemaOps classes.
A more specific type of JSON schema for sum types (sealed traits)
A more specific type of JSON schema for sum types (sealed traits)
“Tagged” schemas include the name of the type A
as an additional discriminator field. By default,
the name of the discriminator field is defined by the operation defaultDiscriminatorName but
it can be customized by calling the operation withDiscriminator
.
Values of type Tagged[A]
can be constructed by calling the operation tagged
on a Record[A]
.
- Note:
This type has implicit methods provided by the PartialInvariantFunctorSyntax, InvariantFunctorSyntax, and TaggedOps classes.
Value members
Abstract methods
The JSON schema of a coproduct made of the given alternative tagged records
The JSON schema of a coproduct made of the given alternative tagged records
Promotes a schema to an enumeration.
Promotes a schema to an enumeration.
- Decoder interpreters fail if the input value does not match the encoded values of any of the possible values,
- Encoder interpreters never fail, even if the value is not contained in the set of possible values,
- Documentation interpreters enrich the JSON schema with an
enum
property listing the possible values.
The JSON schema of a record with a single field name
of type A
The JSON schema of a record with a single field name
of type A
- Encoder interpreters produce a JSON object with one property of the given
name
, - Decoder interpreters fail if the JSON value is not a JSON object, or if it
doesn’t contain the
name
property, or if the property has an invalid value (according to itstpe
), - Documentation interpreters produce the JSON schema of a JSON object schema with
one required property of the given
name
.
The JSON schema of a record with a single optional field name
of type A
The JSON schema of a record with a single optional field name
of type A
- Encoder interpreters can omit the field or emit a field with a
null
value, - Decoder interpreters successfully decode
None
if the field is absent or if it is present but has the valuenull
. They fail if the field is present but contains an invalid value, - Documentation interpreters produce the JSON schema of a JSON object with an
optional property of the given
name
.
A schema that can be either schemaA
or schemaB
.
A schema that can be either schemaA
or schemaB
.
Documentation interpreter produce a oneOf
JSON schema.
Encoder interpreters forward to either schemaA
or schemaB
.
Decoder interpreters first try to decode with schemaA
, and fallback to schemaB
in case of failure.
The difference between this operation and the operation orElse
on “tagged” schemas
is that this operation does not rely on a discriminator field between the alternative
schemas. As a consequence, decoding is slower than with “tagged” schemas and provides
less precise error messages.
- Note:
Be careful to use ''disjoint'' schemas for
A
andB
(none must be a subtype of the other), otherwise, a value of typeB
might also be successfully decoded as a value of typeA
, and this could have surprising consequences.
A JSON schema for type String
.
A JSON schema for type String
.
- Value parameters:
- format
An additional semantic information about the underlying format of the string
- See also:
Add a description to the given enumeration JSON schema
Add a description to the given enumeration JSON schema
Allows to specify name of discriminator field for sum type
Allows to specify name of discriminator field for sum type
Include an example value within the given enumeration JSON schema
Include an example value within the given enumeration JSON schema
Include an example value within the given JSON schema
Include an example value within the given JSON schema
Include an example value within the given record JSON schema
Include an example value within the given record JSON schema
Include an example value within the given tagged JSON schema
Include an example value within the given tagged JSON schema
Concrete methods
A JSON schema for type BigDecimal
where certain properties, such as minimum, maximum, etc. are set.
A JSON schema for type BigDecimal
where certain properties, such as minimum, maximum, etc. are set.
Default discriminator field name for sum types.
Default discriminator field name for sum types.
It defaults to "type", but you can override it twofold:
- by overriding this field you can change default discriminator name algebra-wide
- by using
withDiscriminator
you can specify discriminator field name for specific sum type
A JSON schema for type Double
where certain properties, such as minimum, maximum, etc. are set.
A JSON schema for type Double
where certain properties, such as minimum, maximum, etc. are set.
A JSON schema for type BigDecimal
where certain properties, such as minimum, maximum, etc. are set.
A JSON schema for type BigDecimal
where certain properties, such as minimum, maximum, etc. are set.
Convenient constructor for enumerations represented by int values.
Convenient constructor for enumerations represented by int values.
A JSON schema for type Int
where certain properties, such as minimum, maximum, etc. are set.
A JSON schema for type Int
where certain properties, such as minimum, maximum, etc. are set.
Captures a lazy reference to a JSON schema currently being defined:
Captures a lazy reference to a JSON schema currently being defined:
case class Recursive(next: Option[Recursive])
val recursiveSchema: Record[Recursive] =
lazyRecord("Rec") {
optField("next")(recursiveSchema)
}.xmap(Recursive(_))(_.next)
Interpreters should return a JsonSchema value that does not evaluate
the given schema
unless it is effectively used.
- Value parameters:
- name
A unique name identifying the schema
- schema
The record JSON schema whose evaluation should be delayed
A lazy JSON schema that can references schemas currently being defined:
A lazy JSON schema that can references schemas currently being defined:
case class Recursive(next: Option[Recursive])
val recursiveSchema: JsonSchema[Recursive] = lazySchema("Rec")(
optField("next")(recursiveSchema)
).xmap(Recursive)(_.next)
Interpreters should return a JsonSchema value that does not evaluate
the given schema
unless it is effectively used.
- Value parameters:
- name
A unique name identifying the schema
- schema
The record JSON schema whose evaluation should be delayed
Captures a lazy reference to a JSON schema currently being defined.
Captures a lazy reference to a JSON schema currently being defined.
Interpreters should return a JsonSchema value that does not evaluate
the given schema
unless it is effectively used.
- Value parameters:
- name
A unique name identifying the schema
- schema
The tagged JSON schema whose evaluation should be delayed
A schema for a statically known value.
A schema for a statically known value.
- Decoder interpreters first try to decode incoming values with the given
tpe
schema, and then check that it is equal to the givenvalue
, - Encoder interpreters always produce the given
value
, encoded according totpe
, - Documentation interpreters enrich the JSON schema with a
const
property documenting its only possible value (or anenum
property with a single item).
This is useful to model schemas of objects containing extra fields that are absent from their Scala representation. For example, here is a schema for a GeoJSON point:
case class Point(lon: Double, lat: Double)
val pointSchema = (
field("type")(literal("Point")) zip
field[(Double, Double)]("coordinates")
).xmap(Point.tupled)(p => (p.lon, p.lat))
A JSON schema for type Long
where certain properties, such as minimum, maximum, etc. are set.
A JSON schema for type Long
where certain properties, such as minimum, maximum, etc. are set.
The JSON schema of a record with a single optional field with the given name
The JSON schema of a record with a single optional field with the given name
- Decoders fallback to the
defaultValue
if the field is absent from the decoded JSON object. They fail if the field is present but has an invalid value, - Encoders must emit the field if it is not
defaultValue
, but can choose to omit it when it has thedefaultValue
- Documentation interpreters produce the JSON schema of a JSON object with an
optional property of the given
name
.
The JSON schema of a coproduct that share the same parent type and thus can be widened to that parent type
The JSON schema of a coproduct that share the same parent type and thus can be widened to that parent type
Convenient constructor for enumerations represented by string values.
Convenient constructor for enumerations represented by string values.
Deprecated methods
Captures a lazy reference to a JSON schema currently being defined:
Captures a lazy reference to a JSON schema currently being defined:
case class Recursive(next: Option[Recursive])
val recursiveSchema: Record[Recursive] = (
optField("next")(lazyRecord(recursiveSchema, "Rec"))
).xmap(Recursive)(_.next)
Interpreters should return a JsonSchema value that does not evaluate
the given schema
unless it is effectively used.
- Value parameters:
- name
A unique name identifying the schema
- schema
The record JSON schema whose evaluation should be delayed
- Deprecated
Captures a lazy reference to a JSON schema currently being defined.
Captures a lazy reference to a JSON schema currently being defined.
Interpreters should return a JsonSchema value that does not evaluate
the given schema
unless it is effectively used.
- Value parameters:
- name
A unique name identifying the schema
- schema
The tagged JSON schema whose evaluation should be delayed
- Deprecated
Implicits
Implicits
A JSON schema for sequences
A JSON schema for sequences
The JSON schema of a record with no fields
The JSON schema of a record with no fields
- Encoder interpreters produce an empty JSON object,
- Decoder interpreters fail if the JSON value is not a JSON object,
- Documentation interpreters produce the JSON schema of a JSON object schema with no properties.
Provides xmap
and xmapPartial
operations.
Provides xmap
and xmapPartial
operations.
- See also:
PartialInvariantFunctorSyntax and InvariantFunctorSyntax
Provides xmap
and xmapPartial
operations.
Provides xmap
and xmapPartial
operations.
- See also:
PartialInvariantFunctorSyntax and InvariantFunctorSyntax
Provides xmap
and xmapPartial
operations.
Provides xmap
and xmapPartial
operations.
- See also:
PartialInvariantFunctorSyntax and InvariantFunctorSyntax
Inherited implicits
Extension methods for values of type F[A]
for which there is an implicit
InvariantFunctor[F]
instance.
Extension methods for values of type F[A]
for which there is an implicit
InvariantFunctor[F]
instance.
- Inherited from:
- InvariantFunctorSyntax
- Inherited from:
- PartialInvariantFunctorSyntax
A JSON schema for a tuple of 10 elements.
A JSON schema for a tuple of 10 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 11 elements.
A JSON schema for a tuple of 11 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 12 elements.
A JSON schema for a tuple of 12 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 13 elements.
A JSON schema for a tuple of 13 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 14 elements.
A JSON schema for a tuple of 14 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 15 elements.
A JSON schema for a tuple of 15 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 16 elements.
A JSON schema for a tuple of 16 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 17 elements.
A JSON schema for a tuple of 17 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 18 elements.
A JSON schema for a tuple of 18 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 19 elements.
A JSON schema for a tuple of 19 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 20 elements.
A JSON schema for a tuple of 20 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 21 elements.
A JSON schema for a tuple of 21 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 22 elements.
A JSON schema for a tuple of 22 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 2 elements.
A JSON schema for a tuple of 2 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 3 elements.
A JSON schema for a tuple of 3 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 4 elements.
A JSON schema for a tuple of 4 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 5 elements.
A JSON schema for a tuple of 5 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 6 elements.
A JSON schema for a tuple of 6 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 7 elements.
A JSON schema for a tuple of 7 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 8 elements.
A JSON schema for a tuple of 8 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas
A JSON schema for a tuple of 9 elements.
A JSON schema for a tuple of 9 elements.
Tuples are represented with JSON arrays, as documented in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation.
- Inherited from:
- TuplesSchemas