JSON schema validator in Scala

孤者浪人 提交于 2019-12-07 14:16:22

问题


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?

回答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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/27957765/json-schema-validator-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!