Created
March 13, 2019 14:12
-
-
Save afranzi/b3199c9b360083d42740114c1ca75126 to your computer and use it in GitHub Desktop.
JSON Schema Validation
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
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