Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document comprehension based validation #10

Open
carlpulley opened this issue Feb 11, 2017 · 0 comments
Open

Document comprehension based validation #10

carlpulley opened this issue Feb 11, 2017 · 0 comments

Comments

@carlpulley
Copy link
Owner

carlpulley commented Feb 11, 2017

Currently, validated-config uses the free structure of the case class ASTs to perform validation. In some validation scenarios, we may need to ensure (for example) that certain combinations of case class fields are acceptable (c.f. comprehension).

As a concrete example, imagine the following case class:

final case class Example(hostname: String, port: Int)

we may want port >= 0 in general, but when hostname matches localhost we want port >= 1024.

As we can achieve this functionality here by using flatMap on functions of type Example => Try[Example] - we need to ensure this type of usage is documented.

So, for the example code above, we'd write something like:

case object ShouldBePositive extends Exception
case object LocalhostPortShouldBeNonPrivileged extends Exception

validateConfig("application.conf") { implicit config =>
  build[Example](
    unchecked[String]("hostname"),
    validate[port]("port", ShouldBePositive)(_ >= 0)
  )
}.flatMap {
  case eg @ Example("localhost", port) if port >= 1024 =>
    Success(eg)
  case Example("localhost", _) =>
    Failure(LocalhostPortShouldBeNonPrivileged)
  case eg: Example =>
    Success(eg)
}
@carlpulley carlpulley changed the title Implement comprehension based validation Document comprehension based validation Feb 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant