question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Relative $ref pointer not working for files

See original GitHub issue

I have a nodejs application and a folder where all schema reside . The folder are different for both as given below :

Node js application run here – /home/ubuntu/work/node_webhook/webhook.js

Json schema files are stored here :

-- /home/ubuntu/work/analytics/data_types/
------- mobile_schema
------------- test_schema.json
------------- test_2_schema.json
-------- enums.json
-------- common_data_types.json

test_schema.json looks like this :

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "amount": {
      "type": "integer"
    },
    "created_at_date": {
      "$ref": "../common_data_types.json#/common_types/date_time"
    },
    "current_stage": {
       "$ref": "../enum_server.json#/all_enum/CURRENT_STAGE"
    }
  },
  "required": [
    "amount",
    "created_at_date",
    "current_stage"
  ]
}

common_data_types.json looks like this :

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "common_types": {
    "date_time": {
      "type": "string",
      "format": "date-time"
    }
  }
}

enums.json looks like this

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "all_enum": {
    "PAYMENT_METHOD": {
      "type": "string",
      "enum": [
        "ONLINE",
        "OFFLINE"
      ]
    },
    "CURRENT_STAGE": {
      "type": "string",
      "enum": [
        "ONE",
        "TWO",
        "THREE"
      ]
    }
  }
}

AND NODE JS APLICATION FILE : webhook.js looks like this :

var express = require('express');
var app = express(); 
var $RefParser = require('json-schema-ref-parser');
var Validator = require('jsonschema').Validator;

var v = new Validator();
var parser = new $RefParser();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var router = express.Router();              // get an instance of the express Router


// middleware to use for all requests
router.use(function(req, res, next) {
    console.log('Got a request.');
    next(); // make sure we go to the next routes and don't stop here
});


function validateEventSchema(req,res){
//console.log(req.body); 
var event_data = req.body.properties;
var parser = new $RefParser();
var event_schema = parser.bundle("/home/travel/work/trips3m/lib/analytics/data_types/mobile_schema/test_schema.json");

parser.dereference(event_schema, function(err, schema) {
  console.log(parser.$refs.paths())
  valid = false;
  if (err) {
    console.error(err);
  }
  else {
    //console.log(schema.properties.ts.type);
    c = v.validate(event_data, schema);
    if(c.errors.length > 0 ){
      console.log("ERROR OCCUR");
    }else{
      valid = true ; 
    }
  }

  console.log(valid)
}

router.route('/dump_thy_data')
  .post(function(req, res) {
      validateEventSchema(req,res)
  });

app.use('/', router);
app.listen(process.env.APP_PORT);

Getting error as it try to resolve ref pointers in current directory in which nodejs application is running . That is : /home/ubuntu/work/node_webhook/ So it cannot find /home/ubuntu/work/node_webhook/enums.json /home/ubuntu/work/node_webhook/common_data_types.json

How will i provide it a base directory to load all json files from . Or how to use relative file path ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sorohancommented, Jun 30, 2017

@BigstickCarpet. I think according to the schema additional properties should be ignored: http://json-schema.org/latest/json-schema-core.html#rfc.section.8

“All other properties in a “$ref” object MUST be ignored.”

In any case, it seems to be breaking the file resolution. If you run with the test I added (npm run mocha) you should see an error:

Error: Error resolving $ref pointer "...json-schema-ref-parser/test/specs/two/id.json".
"...json-schema-ref-parser/test/specs/two/id.json" not found.

And removing the ‘title’ property stops the error.

1reaction
sul4bhcommented, Apr 28, 2017

@BigstickCarpet I have run into similar issue regarding “$ref” that points to a file location.

My question is, what is the base path for a relative path in $ref? Is the base path relative to where the json file is or is it relative to process.cwd()?

The code that I have right now behaves as if process.cwd() is the base path. Is it the right behavior? Does the spec say anything about it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relative $ref pointer not working for files #35 - GitHub
I have a nodejs application and a folder where all schema reside . The folder are different for both as given below :...
Read more >
How to use JSON references ($refs) - Redocly
Within a separate file we need a relative path to the file and then the JSON Pointer path to the object separated by...
Read more >
Relative Pointers - gingerBill
Absolute Pointers. Pointers are a value type in programming languages that store a memory address. A pointer references a location in memory ...
Read more >
What is Relative Pointers? - YouTube
Donate: https://donate.thedigital.gov.ua/
Read more >
Excel Formulas: Relative and Absolute Cell References
Relative references change when a formula is copied to another cell. Absolute references, on the other hand, remain constant no matter where they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found