diff --git a/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala b/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala index 37eadc53..1e691d93 100644 --- a/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala +++ b/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala @@ -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") } } diff --git a/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala b/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala index 883061c8..21e06fec 100644 --- a/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala +++ b/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala @@ -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 { @@ -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")) } } @@ -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")) } } @@ -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 + ) }