# gatling Custom validations

**URL:** <https://community.gatling.io/t/gatling-custom-validations/4001>\
**Category:** Gatling (Open-Source)\
**Created:** [October 18, 2017, 2:31pm UTC](https://community.gatling.io/t/gatling-custom-validations/4001 "2017-10-18T14:31:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sreelakshmi\_Ck](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Sreelakshmi\_Ck](https://community.gatling.io/u/Sreelakshmi_Ck)\
**Post date:** [October 18, 2017, 2:31pm UTC](https://community.gatling.io/t/gatling-custom-validations/4001/1 "2017-10-18T14:31:08Z")

</div>

Hi,

I am new to gatling and scala.

I want to fail a request, if the json response didn’t match the json schema.  
This is my schema validator function.

def validateJsonAgainstSchema(jsonResponseExpr:Expression[String], schemaFile:String)(session: Session): Validation[Session] = {

val jsonResponse=jsonResponseExpr(session)  
val jsonSchema = testUtilsObj.readFile(schemaFile)  
val schema: JsonNode = asJsonNode(parse(jsonSchema))  
val instance: JsonNode = asJsonNode(parse(jsonResponse.get))  
val validator = JsonSchemaFactory.byDefault().getValidator  
val processingReport = validator.validate(schema, instance)

if (processingReport.isSuccess) {  
Success(session)  
} else {  
println(“ **validation Failed** \*\*\*”)  
println(processingReport.map(_.asJson()).mkString("\n"))  
Failure(processingReport.map(_.asJson()).mkString("\n"))  
}  
}

I call this function in exec block as below, and the gatling test passes even if the function returns failure.  
.exec(  
valObj.validateJsonAgainstSchema("${jsonResponse}",“Schemas/searchQueryResponse.json”) \_  
)

How to make the call to ‘validateJsonAgainstSchema’ in the request check block. Can some one give me example of implementing custom validations?

Thanks,  
Sreelakshmi
