Skip to content

Instantly share code, notes, and snippets.

@afranzi
Created March 13, 2019 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afranzi/b3199c9b360083d42740114c1ca75126 to your computer and use it in GitHub Desktop.
Save afranzi/b3199c9b360083d42740114c1ca75126 to your computer and use it in GitHub Desktop.
JSON Schema Validation
def getSchemaRef(event: JSONObject): String = {
JsonObjectFields.getSchemaRef(event, defaultSchema)
}
def validateEvent(schema: Schema, event: JSONObject): ValidationResult[JSONObject] = {
val validationListener: SchemaValidationListener = SchemaValidationListener()
val validator: Validator = Validator
.builder
.withListener(validationListener)
.build()
validator.performValidation(schema, event)
val schemasReferenced: Seq[SchemaReferenced] = validationListener
.schemasReferencedMatching
.distinct
ValidationResult(event, schemasReferenced)
}
def validateEvent(event: JSONObject): ValidationResult[JSONObject] = {
val schemaRef = getSchemaRef(event)
val schema: Schema = Try(schemaCache.get(schemaRef)).recover {
case _: Exception =>
logger.error(s"Error loading schema [$schemaRef]")
throw SchemaNotFoundException(schemaRef)
}.get
validateEvent(schema, event)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment