Skip to content

Commit

Permalink
Change error message for Play JSON to be like Play (#35)
Browse files Browse the repository at this point in the history
Use short, constant string names already in use in Play to better
integrate with code expecting that behavior (i.e. Play, or other systems
already using Play JSON).
  • Loading branch information
eddsteel authored and lloydmeta committed May 7, 2016
1 parent 6d7e6cf commit d5776a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ object EnumFormats {
val maybeBound = if (insensitive) enum.withNameInsensitiveOption(s) else enum.withNameOption(s)
maybeBound match {
case Some(obj) => JsSuccess(obj)
case None => JsError(s"Enumeration expected of type: '$enum', but it does not appear to contain the value: '$s'")
case None => JsError("error.expected.validenumvalue")
}
}
case _ => JsError("String value expected")
case _ => JsError("error.expected.enumstring")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package enumeratum

import org.scalatest.OptionValues._
import org.scalatest.{ FunSpec, Matchers }
import play.api.libs.json.{ JsNumber, JsString }
import play.api.libs.json.{ JsNumber, JsResult, JsString }

class EnumFormatsSpec extends FunSpec with Matchers {

Expand All @@ -15,7 +15,9 @@ class EnumFormatsSpec extends FunSpec with Matchers {

it("should create a reads that fails with invalid values") {
reads.reads(JsString("D")).isError should be(true)
errorMessages(reads.reads(JsString("D"))) should be(Seq("error.expected.validenumvalue"))
reads.reads(JsNumber(2)).isError should be(true)
errorMessages(reads.reads(JsNumber(2))) should be(Seq("error.expected.enumstring"))
}
}

Expand All @@ -29,7 +31,9 @@ class EnumFormatsSpec extends FunSpec with Matchers {

it("should create a reads that fails with invalid values") {
reads.reads(JsString("D")).isError should be(true)
errorMessages(reads.reads(JsString("D"))) should be(Seq("error.expected.validenumvalue"))
reads.reads(JsNumber(2)).isError should be(true)
errorMessages(reads.reads(JsNumber(2))) should be(Seq("error.expected.enumstring"))
}
}

Expand Down Expand Up @@ -58,4 +62,11 @@ class EnumFormatsSpec extends FunSpec with Matchers {
}
}

def errorMessages(jsResult: JsResult[_]): Seq[String] =
jsResult.fold(
_.collect {
case (path, errors) => errors.map(_.message).mkString
},
_ => Seq.empty
)
}

0 comments on commit d5776a3

Please sign in to comment.