joanOfArc
joanOfArc

Reputation: 467

JSON schema validator in Scala

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?

Upvotes: 3

Views: 12509

Answers (3)

Allan
Allan

Reputation: 121

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.

Upvotes: 0

Gábor Bakos
Gábor Bakos

Reputation: 9100

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.

Upvotes: 1

dk14
dk14

Reputation: 22374

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.

Upvotes: 1

Related Questions