Created
March 3, 2019 18:28
-
-
Save afranzi/70ceeba5acb5e390d6258e39a6e865ae to your computer and use it in GitHub Desktop.
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 SchemaValidationListener._ | |
import org.everit.json.schema.Schema | |
import org.everit.json.schema.event.{CombinedSchemaMatchEvent, SchemaReferencedEvent, ValidationListener} | |
import scala.collection.mutable.ListBuffer | |
import scala.util.matching.Regex | |
import scala.collection.JavaConverters._ | |
object SchemaValidationListener { | |
case class SchemaReferenced(location: String, schemaId: String) | |
val SchemaId = "schemaId" | |
val schemaRefRe: Regex = "^\\{\"\\$ref\":\"([\\w\\/._-]+)\"\\}$".r(SchemaId) | |
def extractSchemaReferenced(schema: String): Option[String] = { | |
schemaRefRe | |
.findFirstMatchIn(schema) | |
.map(_.group(SchemaId)) | |
} | |
} | |
case class SchemaValidationListener() extends ValidationListener { | |
val schemasReferencedMatching: ListBuffer[SchemaReferenced] = ListBuffer.empty | |
override def schemaReferenced(event: SchemaReferencedEvent): Unit = { | |
val subSchema: Schema = event.getReferredSchema | |
val schemaReferenced = Option(subSchema.getId).getOrElse(subSchema.getSchemaLocation) | |
val path = event.getPath.asScala.mkString("/") | |
val reference = SchemaReferenced(path, schemaReferenced) | |
schemasReferencedMatching.append(reference) | |
} | |
override def combinedSchemaMatch(event: CombinedSchemaMatchEvent): Unit = { | |
val subSchema: Schema = event.getSubSchema | |
val path = event.getPath.asScala.mkString("/") | |
extractSchemaReferenced(subSchema.toString) | |
.foreach { schemaId => | |
val reference = SchemaReferenced(path, schemaId) | |
schemasReferencedMatching.append(reference) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment