Relative $ref pointer not working for files
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:2
- Comments:11 (4 by maintainers)
Top GitHub Comments
@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:
And removing the ‘title’ property stops the error.
@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?