You can make sure that before performing your own decoding, the object does not contain additional keys:
import play.api.data.validation.ValidationError def onlyFields(allowed: String*): Reads[JsObject] = Reads.filter( ValidationError("One or more extra fields!") )(_.keys.forall(allowed.contains))
Or, if you don't like the error messages (and this is not very useful, anyway):
def onlyFields(allowed: String*): Reads[JsObject] = Reads.verifying(_.keys.forall(allowed.contains))
And then:
implicit val stuffReads: Reads[Stuff] = onlyFields("name", "value") andThen ( (__ \ 'name).read[String] and (__ \ 'value).readNullable[Boolean] )(Stuff)
Repetition is not very nice, but it works.
Travis brown
source share