build.sbt
name := "swagger-validator"
libraryDependencies ++= Seq(
"com.github.fge" % "json-schema-validator" % "2.1.7"
)src/main/scala/Validate.scala
import com.fasterxml.jackson.databind.JsonNode
import com.github.fge.jsonschema.exceptions.ProcessingException
import com.github.fge.jsonschema.main.{ JsonSchema, JsonSchemaFactory}
import com.github.fge.jsonschema.report.ProcessingReport
import com.github.fge.jackson.JsonLoader
import com.fasterxml.jackson.databind.ObjectMapper
import scala.io._
object Validator {
def main (args:Array[String]) = {
val data = JsonLoader.fromString(Source.fromFile("data.json").mkString)
val mapper = new ObjectMapper
val schema = mapper.readTree(Source.fromURL("https://raw.github.com/wordnik/swagger-core/master/schemas/api-declaration-schema.json").mkString)
val factory = JsonSchemaFactory.byDefault()
val jsonSchema = factory.getJsonSchema(schema)
val report = jsonSchema.validate(data)
println(report)
}
}run as:
sbt
console
Validator.main(null)