From d5776a309f4a20f3fbd75accfba8d0c7a7e0fdd5 Mon Sep 17 00:00:00 2001 From: Edd Steel Date: Fri, 6 May 2016 23:16:04 -0700 Subject: [PATCH] Change error message for Play JSON to be like Play (#35) 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). --- .../src/main/scala/enumeratum/EnumFormats.scala | 4 ++-- .../src/test/scala/enumeratum/EnumFormatsSpec.scala | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) 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 + ) }