Can't use "if" condition in a pattern match when defining validator with DSL
webblin034 opened this issue · 1 comments
webblin034 commented
Official Document :
" Accord supports several native Scala control structures, notably ifs and pattern matching "
in http://wix.github.io/accord/dsl.html
Library Dependencies :
libraryDependencies ++= Seq(
"com.wix" %% "accord-core" % "0.7.6" % Test,
"org.scalatest" %% "scalatest" % "3.2.2" % Test
)
Input Data :
case class InputData(
id: String,
name: Option[String] = None,
description: Option[String] = None,
items: Seq[String] = Seq.empty
)
Unit Test :
val data = InputData("", None, None, Seq.empty)
val result = validate(data)
result.isFailure shouldBe true
result.toFailure.value.violations.size shouldBe 4
Compilation Failed !
implicit val inputDataValidator: Validator[InputData] = validator[InputData] { request =>
request.name match {
case None => {
request.id is notBlank
if (request.description.isEmpty) {
request.name.mkString is notBlank
request.description.mkString is notBlank
}
request.items is notEmpty
}
case Some(_) =>
}
()
}
Test Compilation Error :
[error] not found: value request
[error] if (request.description.isEmpty) {
[error] ^
[error] one error found
Test Failed, Shouldn't Ignore All Assertions !
implicit val inputDataValidator: Validator[InputData] = validator[InputData] { request =>
request.name match {
case None => {
if (request.description.isEmpty) {
request.id is notBlank
request.name.mkString is notBlank
request.description.mkString is notBlank
request.items is notEmpty
}
}
case Some(_) =>
}
()
}
noam-almog commented
you should probably use .each operator,
for example:
request.description.each is notBlank