Last active
December 15, 2015 08:38
-
-
Save mandubian/5231877 to your computer and use it in GitHub Desktop.
Play Framework Json sample: Copy JsObject only if passwords equal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.libs.json._ | |
import play.api.data.validation._ | |
import play.api.libs.functional.syntax._ | |
val json = Json.obj( | |
"name" -> "John", | |
"email" -> "[email protected]", | |
"password" -> "password", | |
"confirmPassword" -> "password" | |
) | |
val json2 = Json.obj( | |
"name" -> "John", | |
"email" -> "[email protected]", | |
"password" -> "password", | |
"confirmPassword" -> "password2" | |
) | |
val passwordValidator = __.json.pick[JsObject] keepAnd ( | |
( | |
(__ \ "password").read[String] and | |
(__ \ "confirmPassword").read[String] | |
).tupled | |
.filter(ValidationError("password not confirmed")){ case(pwd, confPwd) => pwd == confPwd } | |
) | |
scala> passwordValidator.reads(json) | |
res8: play.api.libs.json.JsResult[play.api.libs.json.JsObject] = | |
JsSuccess({"name":"John","email":"[email protected]","password":"password","confirmPassword":"password"},) | |
scala> passwordValidator.reads(json2) | |
res9: play.api.libs.json.JsResult[play.api.libs.json.JsObject] = | |
JsError(List((,List(ValidationError(password not confirmed,WrappedArray()))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment