Felix
Felix

Reputation: 5619

scala _.validate error message

is there away in scala to print the correct error message why the validation does not work?

request.body.asJson.foreach(f = _.validate[ProcessSteps] match {
  case JsSuccess(processSteps, _) =>
  case _ => ?? error message?

That would be a big help

Upvotes: 0

Views: 109

Answers (1)

Tarun Bansal
Tarun Bansal

Reputation: 267

Just like JsSuccess you have a JsError that you can use capture errors. Below is an example -

request.body.asJson.foreach(f = _.validate[ProcessSteps] match {
    case JsSuccess(processSteps, _) =>
    case e: JsError => println("Errors: " + JsError.toJson(e).toString())

You can also use fold method like Form validation as shown below -

val nameOption: Option[String] = nameResult.fold(
  invalid = {
    fieldErrors => fieldErrors.foreach(x => {
      println("field: " + x._1 + ", errors: " + x._2)
    })
    None
  },
  valid = {
    name => Some(name)
  }
)

For more information have a look at official documentation

Upvotes: 1

Related Questions