Code Monkey home page Code Monkey logo

Comments (2)

edgarmueller avatar edgarmueller commented on June 12, 2024 2

@AlexTo Hi, please note that there was a minor mistake when using ids which I've fixed today with 0.9.5-M3. So for the following to work, please update. To answer your original question: you can use addSchema to make all involved schemas known to the validator. Here's a sample test case for starters, which can be improved upon (this assumes that the references JSON files are stored within a tincan resource folder on the classpath):

class TinCanSpec extends Specification { self =>

  import com.eclipsesource.schema.drafts.Version4

  val instance = JsonSource.fromString(
    """
      |{
      |  "actor": {
      |    "name": "Sally Glider",
      |    "mbox": "mailto:[email protected]"
      |  },
      |  "verb": {
      |    "id": "http://adlnet.gov/expapi/verbs/experienced",
      |    "display": { "en-US": "experienced" }
      |  },
      |  "object": {
      |    "id": "http://example.com/activities/solo-hang-gliding",
      |    "definition": {
      |      "name": { "en-US": "Solo Hang Gliding" }
      |    }
      |  }
      |}
    """.stripMargin).get


  "Tin Can Spec" should {

    import Version4._

    def readSchema(filename: String) =
      JsonSource.schemaFromStream(self.getClass.getResourceAsStream(s"/tincan/$filename.json")).get

    "validate basic statement object" in {

      val validator = SchemaValidator(Some(Version4))
        .addSchema("#agent", readSchema("agent"))
        .addSchema("#group", readSchema("group"))
        .addSchema("#inversefunctional", readSchema("inversefunctional"))
        .addSchema("#mbox", readSchema("mbox"))
        .addSchema("#statement_base", readSchema("statement_base"))
        .addSchema("#statement_object", readSchema("statement_object"))
        .addSchema("#verb", readSchema("verb"))
        .addSchema("#languagemap", readSchema("languagemap"))
        .addSchema("#activity", readSchema("activity"))
        .addSchema("#activity_definition", readSchema("activity_definition"))
        .addSchema("#activityid", readSchema("activityid"))

      val result = validator.validate(readSchema("statement_base"), instance)
      result.isSuccess must beTrue
    }
  }
}

from play-json-schema-validator.

AlexTo avatar AlexTo commented on June 12, 2024

Thanks a lot, this seems to be a better solution.
After creating this issue, I ended up, modifying all the "$ref" in all the files.

For e.g.
"$ref": "#someschema" => "$ref": "classpath:/path/someschema.json"
The problem is I have to update all the JSON schema files which I don't want.

Also, there are some ref which I don't know how to deal with for .e.g I have the followign in activity.json
"id": {"$ref": "#activityid!core"},
And this is activityid.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "#activityid",
    "type": "object",
    "required": ["activityId"],
    "properties": {
        "activityId": {
        "id": "#activityid!core",
        "type": "string",
        "format": "iri"
        }
    }
}

I will try your approach, thanks again

from play-json-schema-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.