Skip to content

Instantly share code, notes, and snippets.

@fehguy
Last active December 23, 2015 13:59
Show Gist options
  • Select an option

  • Save fehguy/6645523 to your computer and use it in GitHub Desktop.

Select an option

Save fehguy/6645523 to your computer and use it in GitHub Desktop.
validating your swagger spec

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment