voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.54k stars 243 forks source link

No such file or directory for JSON pointers. #289

Open panSarin opened 8 years ago

panSarin commented 8 years ago

Can i avoid loading json pointers as a file with non-relative paths?

I have my JSON schema file. Where i use $ref option

that is part of my object schema;

"resource_name_create_response": {
      "description": "response that user get after creating resource object",
      "properties": {
        "_id": { "type": "string", "example": "567bc35d5605f67f41000038" },
        "author_id": { "type": "string", "example": "567bc35c5605f67f4100000f" },
        "author_name": { "type": "string", "example": "Doctor" },
         "questions_information": {
          "type": "array",
          "items": { "$ref": "/schemata/resource_name#/definitions/question_detailed"}
        }
      }

this schema is autogenerated with PRMD gem, just filled some details. It works , prmd gem verify it as a proper .

But if I try to use

    schema_directory = "#{Dir.pwd}/schema/schemata"
    schema_path = "#{schema_directory}/#{resource_name}.json"
    schema = JSON.parse(File.read(schema_path))['definitions'][schema_def_name]
   JSON::Validator.validate!(schema, response, parse_data: false)

(that just load json, select this part of it that i need and pass it to validate), it fails on $ref with:

  No such file or directory - /schemata/resource_name

I am not sure why it even try to load /schemata/resource_name file since he get json with that data inside anyway..

Am I doing something wrong or it is like TODO? Or maybe it is because i pass data instead of path for #validate method ?

panSarin commented 8 years ago

I forked and debugged. It seems like it will never find file basing on json pointer, until you pass '.json' in pointer. But in JSON pointers we shouldn't pass it. Is it a bug , or I am still doing something wrong?

panSarin commented 8 years ago

Ok i think my question should be : can we use hyper-schema in JSON::Validator, or only Core JSON Schemas?

iainbeeston commented 8 years ago

I think there is some confusion here.

Hyper schema is a way of defining links in your schema, so that given a json text, you can find other related json texts. But what you're describing is json reference (ie. $ref properties).

The way that json references work is that the uri is substituted for the json text at that uri.

I'm not sure which of these scenarios is the one you are seeing. Does that help explain why your example does not work?

davidnquach commented 8 years ago

I've also run into this problem where an error is returned as "No such file or document". The real error (which could be a syntax error in the json schema) is being suppressed by the "No such file or document" error. I did some debugging and I found out it's the addressable gem that is causing the problem. The addressable gem has been updated to 2.4. In your previous version 2.5.0 you have set the gemspec for addressable as ~> 2.3 but in the current version 2.6.0 the gemspec says ~> 2.3.8 which will not update addressable to the latest version 2.4.

The problem is Addressable::URI.parse returns 2 different things depending on the version. Version 2.4 returns a formatting error (which I believe is the real error) and version 2.3.8 returns an Addressable object which then gets through to this method in the initialize_schema method.

schema = @options[:schema_reader].read(schema_ui)

This returns the "No such file or document" error. I believe this is the problem, but you can check me on that.