Note that there are some explanatory texts on larger screens.

plurals
  1. POScala play json api: can't implicitly convert JsArray to JsValueWrapper
    primarykey
    data
    text
    <p>I have json rest api application based on play framework and want to receive information about validation errors when I parse incoming requests. Everything is working fine except conversion from json array to json value. Json structure I want to achieve: </p> <pre><code>{ "errors": { "name": ["invalid", "tooshort"], "email": ["invalid"] } } </code></pre> <p>When I tried to implement a sample it worked perfectly: </p> <pre><code>def error = Action { BadRequest(obj( "errors" -&gt; obj( "name" -&gt; arr("invalid", "tooshort"), "email" -&gt; arr("invalid") ) )) } </code></pre> <p>When I tried to extract the changing part like this:</p> <pre><code>def error = Action { val e = Seq("name" -&gt; arr("invalid", "tooshort"), "email" -&gt; arr("invalid")) // in real app it will be Seq[(JsPath, Seq[ValidationError])] BadRequest(obj( "errors" -&gt; obj(e: _*) )) } </code></pre> <p>I got a compiler error: </p> <blockquote> <p>type mismatch; found : Seq[(String, play.api.libs.json.JsArray)] required: Seq[(String, play.api.libs.json.Json.JsValueWrapper)]</p> </blockquote> <p>Maybe there is some implicit conversion I'm missing from JsArray to JsValueWrapper? But then, why does the sample works fine in the same file with the same imports?</p> <p>Play 2.1.1, Scala 2.10.0</p> <p><strong>UPDATE</strong>: Problem resolved thanks to Julien Lafont, the final code:</p> <pre><code>implicit val errorsWrites = new Writes[Seq[(JsPath, Seq[ValidationError])]] { /** * Maps validation result of Ember.js json request to json validation object, which Ember can understand and parse as DS.Model 'errors' field. * * @param errors errors collected after json validation * @return json in following format: * * { * "errors": { * "error1": ["message1", "message2", ...], * "error2": ["message3", "message4", ...], * ... * } * } */ def writes(errors: Seq[(JsPath, Seq[ValidationError])]) = { val mappedErrors = errors.map { e =&gt; val fieldName = e._1.toString().split("/").last // take only last part of the path, which contains real field name val messages = e._2.map(_.message) fieldName -&gt; messages } obj("errors" -&gt; toJson(mappedErrors.toMap)) // Ember requires root "errors" object } } </code></pre> <p>Usage:</p> <pre><code>def create = Action(parse.json) { // method in play controller request =&gt; fromJson(request.body) match { case JsSuccess(pet, path) =&gt; Ok(obj("pet" -&gt; Pets.create(pet))) case JsError(errors) =&gt; UnprocessableEntity(toJson(errors)) // Ember.js expects 422 error code in case of errors } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload