zio.blocks.schema.Validation
See theValidation companion object
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
- Known subtypes
In this article