Skip to main content

Field

Library is called Fields solely because it is built around Field data type.

Field[P] has path of type FieldPath and value of type P.

All validations are defined throught syntax available for Field

Various ways to create and transform Field is described in Syntax section

Syntax​

Create​

import jap.fields._
import jap.fields.DefaultAccumulateVM._

case class Request(name: String)
val request = Request("Ann")
// request: Request = Request(name = "Ann")
Field(request.name)
// res0: Field[String] = Field(path = FieldPath(parts = List()), value = "Ann")
Field(FieldPath.parse("request.name"),request.name)
// res1: Field[String] = Field(
// path = FieldPath(parts = List(Path(value = "request"), Path(value = "name"))),
// value = "Ann"
// )
Field.from(request.name)
// res2: Field[String] = Field(
// path = FieldPath(parts = List(Path(value = "request"), Path(value = "name"))),
// value = "Ann"
// )
Field.sub(request.name)
// res3: Field[String] = Field(path = FieldPath(parts = List(Path(value = "name"))), value = "Ann")

Transform​

case class B()
case class A(b: B)
val a = Field(FieldPath.fromPath("a"), A(B()))
// a: Field[A] = Field(path = FieldPath(parts = List(Path(value = "a"))), value = A(b = B()))
a.sub(_.b)
// res4: Field[B] = Field(
// path = FieldPath(parts = List(Path(value = "a"), Path(value = "b"))),
// value = B()
// )
a.down("b", a.value.b)
// res5: Field[B] = Field(
// path = FieldPath(parts = List(Path(value = "a"), Path(value = "b"))),
// value = B()
// )
a.down("b", _.b)
// res6: Field[B] = Field(
// path = FieldPath(parts = List(Path(value = "a"), Path(value = "b"))),
// value = B()
// )
a.map(_.b)
// res7: Field[B] = Field(path = FieldPath(parts = List(Path(value = "a"))), value = B())
a.mapPath(_ + "A")
// res8: Field[A] = Field(
// path = FieldPath(parts = List(Path(value = "a"), Path(value = "A"))),
// value = A(b = B())
// )
a.named("A")
// res9: Field[A] = Field(path = FieldPath(parts = List(Path(value = "A"))), value = A(b = B()))
a.withPath(FieldPath.fromPath("b"))
// res10: Field[A] = Field(path = FieldPath(parts = List(Path(value = "b"))), value = A(b = B()))
a.withValue(3)
// res11: Field[Int] = Field(path = FieldPath(parts = List(Path(value = "a"))), value = 3)

Special​

Field(1 -> "2").first
// res12: Field[Int] = Field(path = FieldPath(parts = List()), value = 1)
Field(1 -> "2").second
// res13: Field[String] = Field(path = FieldPath(parts = List()), value = "2")
Field(Option(1)).option
// res14: Option[Field[Int]] = Some(value = Field(path = FieldPath(parts = List()), value = 1))