Skip to main content

Quickstart

Fields is a Scala validation library featuring:

  • DSL syntax for defining validation rules
  • Possibility to use Effects like ZIO, Cats, Future, etc.
  • Ability to use your own error types
  • You can use validation structure of choice
  • Interop with Circe
  • fields-value module for complex validation cases
  • fields-lens module using lenses for validation

Getting started​

To get started with sbt, simply add the following line to your build.sbt file.

libraryDependencies ++= List(
"io.github.0lejk4" %% "fields-lens" % "1.0.0", // Lens based validation
"io.github.0lejk4" %% "fields-value" % "1.0.0", // Value based validation
"io.github.0lejk4" %% "fields-zio" % "1.0.0", // Optional: ZIO interop
"io.github.0lejk4" %% "fields-cats" % "1.0.0", // Optional: Cats interop
)

Example​

Define model

case class User(username: String, password: String, passwordRepeat: Option[String])
case class Request(user: User)
import fields.value.FieldsDsl.default._

val policy: Policy[Request] =
Policy[Request]
.subRule(_.user.username)(_.nonBlank, _.minSize(4))
.subRule(_.user.password)(_.nonBlank, _.minSize(8), _.maxSize(30))
.subRule(_.user.password, _.user.passwordRepeat)((p, pr) => pr.some(_ === p))
// policy: Function1[Field[Request], RuleK[Sync, Accumulate, ValidationMessage]] = fields.PolicyK$$$Lambda/0x0000007002e14000@15a9bdbd

policy.validate(Request(User("Ann", "1234", Some(""))))
// res0: RuleK[[A >: Nothing <: Any] => A, [E >: Nothing <: Any] => List[E], ValidationMessage] = List(
// ValidationMessage(
// path = .user.username,
// error = "must have minimum size of 4"
// ),
// ValidationMessage(
// path = .user.password,
// error = "must have minimum size of 8"
// ),
// ValidationMessage(
// path = .user.passwordRepeat,
// error = "must be equal to .user.password"
// )
// )
import fields.lens.FieldsDsl.default._

val policy: Policy[Request] =
Policy[Request]
.subRule(_.user.username)(_.nonBlank, _.minSize(4))
.subRule(_.user.password)(_.nonBlank, _.minSize(8), _.maxSize(30))
.subRule(_.user.password, _.user.passwordRepeat)((p, pr) => pr.some(_ === p))
// policy: Function1[Request, RuleK[Sync, Accumulate, ValidationMessage]] = fields.PolicyK$$$Lambda/0x0000007002fdaf50@26e31600

policy(Request(User("Ann", "1234", Some(""))))
// res1: RuleK[Sync, Accumulate, ValidationMessage] = List(
// ValidationMessage(
// path = .user.username,
// error = "must have minimum size of 4"
// ),
// ValidationMessage(
// path = .user.password,
// error = "must have minimum size of 8"
// ),
// ValidationMessage(
// path = .user.passwordRepeat,
// error = "must be equal to .user.password"
// )
// )

For a complete module comparison, see Modules and compatibility.

Adopters​

Is your company using Fields and want to be listed here?

We will be happy to feature your company here, but in order to do that, we'll need written permission to avoid any legal misunderstandings.

Please open new Github Issue and provide us with your company name, logo and legal permission to add your company as.

Sponsors​

Development and maintenance of Fields is sponsored by Jap

License​

Licensed under the Apache License 2.0. Refer to the license file.