简体   繁体   中英

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))
  }   
}

您可以使用平面图来组合JsResults。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM