Validation

zio.blocks.schema.Validation
See theValidation companion object
sealed trait Validation[+A]

A Validation is a declarative constraint on a primitive value, such as "positive" or "matches a pattern". It is stored in the PrimitiveType of a primitive schema.

Only DynamicSchema enforces it: check, conforms, and the decoding of the schema that toSchema returns. The JSON codec, Schema#fromDynamicValue and Schema#toJsonSchema ignore it, and Schema.derived never adds one. To reject values while decoding, check them in the to function of Schema#transform instead.

import zio.blocks.schema.binding.Binding

val pageCountType = PrimitiveType.Int(Validation.Numeric.Positive)
val pageCount: Schema[Int] =
 new Schema(new Reflect.Primitive[Binding, Int](pageCountType, pageCountType.typeId, Binding.Primitive.int))

val checked = pageCount.toDynamicSchema.check(DynamicValue.int(-5))
// Some(zio.blocks.schema.SchemaError: Validation failed: value must be positive)
val decoded = pageCount.fromDynamicValue(DynamicValue.int(-5)) // Right(-5)

Attributes

See also
Companion
object
Source
Validation.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object None
trait Numeric[A]
object Negative
object NonNegative
object NonPositive
object Positive
class Range[A]
class Set[A]
trait String
object Blank
object Empty
class Length
object NonBlank
object NonEmpty
class Pattern
Show all
In this article