wix-incubator / accord

Accord: A sane validation library for Scala
http://wix.github.io/accord/
Other
529 stars 55 forks source link

Can't use "if-else" in very first position when defining validator with DSL #158

Closed webblin034 closed 3 years ago

webblin034 commented 4 years ago

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
)

Test Compilation OK :

  implicit val inputDataValidator: Validator[InputData] = validator[InputData] { request =>
    if (request.name.isDefined) {
    } else {
      request.name.mkString is notBlank
      request.id is notBlank
      request.description.mkString is notBlank
      request.items is notEmpty
    }
    ()
  }

Test Compilation OK :

  implicit val inputDataValidator: Validator[InputData] = validator[InputData] { request =>
    request.name.mkString is notBlank
    if (request.name.isDefined) {
    } else {
      request.id is notBlank
      request.description.mkString is notBlank
      request.items is notEmpty
    }
    ()
  }

Test Compilation Failed :

  implicit val inputDataValidator: Validator[InputData] = validator[InputData] { request =>
    if (request.name.isDefined) {
      request.name.mkString is notBlank
    } else {
      request.id is notBlank
      request.description.mkString is notBlank
      request.items is notEmpty
    }
    ()
  }

Test Compilation Error :

  [error] not found: value request
  [error] if (request.name.isDefined) {
  [error]     ^
  [error] one error found
noam-almog commented 3 years ago

this should solve things for you

        r.name.each is notBlank
        r.id is notBlank
        r.description.each is notBlank
        r.items is notEmpty
    }