0

I am following **Play For Scala ** for validation and parsing of Json

I am receiving a request in controller after converting it to JsValue like this

 val jsonRequest = request.body.asJson.get

i am trying to validate it like this

jsonRequest.validate[ReadArtWorkCaseClass].fold(

valid = {    
          readArtWorkCaseClass =>
            log.info("valid block")

          Ok("validation successful" )
         },     
invalid = {
           log.info("invalid block")
           errors => {               
             log.info("error block")
             BadRequest(JsError.toFlatJson(errors))
            }           
          }                    
        )

i have implemented Read for this

case class ReadArtWorkCaseClass(artworkid :String,
                                artistid :String,
                                institutionid :String ,
                                status :String,
                                groupactivityid:String,
                                details:String,
                                pricehistoryid :String,
                                sku :String,
                                dimensions :String,
                                artworkname :String,
                                artworkseries :String ,
                                workclassifier :String ,
                                genreid :String,
                                artworktype :String,
                                createddate:String)
    object ReadArtWorkCaseClass {

   implicit val artworkReads: Reads[ReadArtWorkCaseClass] = (
(JsPath \ "artWorkid").read[String] and
(JsPath \ "artistid").read[String] and
(JsPath \ "institutionid").read[String] and
(JsPath \ "activationStatus").read[String] and
(JsPath \ "groupactivityid").read[String] and
(JsPath \ "details").read[String] and
(JsPath \ "pricehistoryid").read[String] and
(JsPath \ "sku").read[String] and
(JsPath \ "dimensions").read[String] and
(JsPath \ "artworkname").read[String] and
(JsPath \ "artworkseries").read[String] and
(JsPath \ "workclassifier").read[String] and
(JsPath \ "genreid").read[String] and
(JsPath \ "artworktype").read[String] and
(JsPath \ "createddate").read[String]
)(ReadArtWorkCaseClass.apply _)
}

when i tried to validate jsonrequest by inputing empty fields it does not go into the invalid block instead runs the valid block

please guide me what is my mistake

4
  • What do you mean by 'empty fields'? If you're putting empty strings as values it is correct. An empty string is still a string. You should paste a sample of your test data. Commented Apr 27, 2015 at 14:13
  • i am submitting form with empty fileds i think you are right but when i try to specify the length of a particular string like this (JsPath \ "artWorkid").read[String](minLength[String](10)) it gives me following error method minLength: (length: Int)play.api.data.validation.Constraint[String] does not take type parameters. Commented Apr 27, 2015 at 14:19
  • Based on the error message I would try minLength(10) instead. It looks like the type parameter is already set to String and you don't need to specify it when calling the function. Commented Apr 27, 2015 at 14:50
  • i have tried it like this (JsPath \ "artWorkid").read[String](minLength(10)) but it gives error on this part "(JsPath \ "artWorkid").read[String]" and the error is "overloaded method value read with alternatives: (t: String)play.api.libs.json.Reads[String] <and> (implicit r: play.api.libs.json.Reads[String])play.api.libs.json.Reads[String] cannot be applied to (play.api.data.validation.Constraint[String])" , is there some import that i am missing or something else? Commented Apr 28, 2015 at 7:24

1 Answer 1

1
(JsPath \ "artWorkid").read(minLength[String](1))

it worked for me

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.