Ali Salehi
Ali Salehi

Reputation: 6999

Play2 - Merging two JsResult objects

I would like to know if it is possible to merge JsResult objects, something similar to ~> operator in play 2.1+. In the following code I want to validate two inputs and then update user information accordingly.

The and operator below between two validate method calls is not valid. Is there a way in play to combine two JsResult objects together in the following scenario ?

def update(uid:String) =  Action { request=>
  ( JsString(uid).validate[BSONObjectID] **and** request.body.validate[User]) match {
    case JsSuccess(user,_) =>  Async {
      collection.update(Json.obj("_id"->uid),v).map{
        case someError:LastError if someError.err.isDefined =>  ....
        case noError => ...
      }
    }
    case errors:JsError => Ok(JsError.toFlatJson(errors))
  }   
}

Upvotes: 1

Views: 351

Answers (2)

Felix
Felix

Reputation: 1277

This is how you can join two JsResult into one:

import play.api.libs.json._
import play.api.libs.functional.syntax._

def update(uid:String) =  Action { request =>
  (JsString(uid).validate[BSONObjectID] and request.body.validate[User]).tupled 
    match {
      case JsSuccess((uid,user),_) =>  Async {

tupled and and is part of the play.api.libs.functional package.

Upvotes: 0

Julien Tournay
Julien Tournay

Reputation: 544

You can use flatmap to combine the JsResults.

Upvotes: 0

Related Questions