3

I need to validate the schema of certain JSON input I receive. I am not clear about how to go about the whole thing. But this is what I have gathered so far :

  1. I need to prepare a schema for all sorts of input using something like http://json-schema.org/implementations.html

  2. Then I need a validator like https://github.com/fge/json-schema-validator

  3. I need to give the json input to and the schema to the validator and get the result.

However my question is that I need to use a jar which I can import and use of the json-schema-validator https://github.com/fge/json-schema-validator . Also I am not clear on how to use it. I don't know the format it accepts , the classes and methods needed etc.

  1. How good the validator's support is for Scala?
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
joanOfArc
  • 467
  • 1
  • 6
  • 16
  • 1
    for Scala - try: https://github.com/nparry/orderly4jvm , there is examples how to work with – dk14 Jan 15 '15 at 06:41

3 Answers3

1

There is Orderly, liftweb-json-based JSON validator implementation for Scala:

import com.nparry.orderly._
import net.liftweb.json.JsonAST._

val orderly = Orderly("integer {0,100};")

val noProblems = orderly.validate(JInt(50))
val notAllowed = orderly.validate(JInt(200))

Use net.liftweb.json.parse(s: String): JValue to obtain JValue from String.

dk14
  • 22,206
  • 4
  • 51
  • 88
  • How to validate JSon against JSon-schema with Orderly? – Alexander Myltsev Oct 17 '15 at 05:00
  • @AlexanderMyltsev you can't, but [Orderly](http://orderly-json.org/)-schema itself would be a good alternative. You can export Orderly as Json-schema and vice versa: http://orderly-json.org/tryit – dk14 Oct 17 '15 at 10:04
  • @dk, Orderly seems really cool: short and clean. But unfortunately it is dead, and seems like not supporting recent version of json-schema. – Alexander Myltsev Oct 19 '15 at 11:02
  • @AlexanderMyltsev last release from scala-repo was 2 monthes before my answer, last github update - 3 monthes ago. I wouldn't say that JSON-schema is too much alive (in comparision with xsd/relax-ng) and also there is no scala-oriented solutions for validators. Talking about modern JSONSchema, orderly converts fine to draft v4, maybe not vice-versa. Anyway, it's still good alternative for simple validations, regardless of being 6-years old. – dk14 Oct 19 '15 at 14:58
  • Yes, Orderly4jvm seems alive. Why are you saying that JSON Schema is not alive? – Alexander Myltsev Oct 19 '15 at 19:32
  • @AlexanderMyltsev I'm not, where did I say that??! I'm saying that imho it's evolving slowly and definetly still a draft. – dk14 Oct 20 '15 at 02:18
1

I would not go through the pain of manually collecting the jars of json schema validator (done that, not fun). It is better use a tool for that (like maven, sbt, gradle or ivy). In case you want to use it in an OSGi environment, you might need to use a different (probably not up-to-date) version.

Usage:

val factory: JsonSchemaFactory = JsonSchemaFactory.getDefault
val validator: JsonValidator = factory.getValidator
val schemaJson: com.fasterxml.jackson.databind.JsonNode = yourJsonSchemaInJackson2Format
val report: ProcessingReport = validator.validate(schemaJson, yourJsonInJackson2Format)
//check your report.

PS.: In case you want to collect the dependencies manually, you can go through the dependencies transitively starting on this page.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
0

I noticed that orderly4jvm does not support the latest JSON Schema version 4, which causes problems if you want to use it to generate the JSON schema.

Allan
  • 81
  • 10