Skip to content

Additional $ref tests for draft-04/06 #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ var assert = require('assert');
var refs = {
'http://localhost:1234/integer.json': require('./remotes/integer.json'),
'http://localhost:1234/subSchemas.json': require('./remotes/subSchemas.json'),
'http://localhost:1234/folder/folderInteger.json': require('./remotes/folder/folderInteger.json')
'http://localhost:1234/folder/folderInteger.json': require('./remotes/folder/folderInteger.json'),
'http://localhost:1234/name.json': require('./remotes/name.json')
};

runTest(4);
runTest(6);

function runTest(draft) {
var opts = {addUsedSchema: false};
var opts = {};
if (draft == 4) opts.meta = false;
var ajv = new Ajv(opts);
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
Expand Down
11 changes: 11 additions & 0 deletions remotes/name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"definitions": {
"orNull": {
"anyOf": [
{"type": "null"},
{"$ref": "#"}
]
}
},
"type": "string"
}
88 changes: 88 additions & 0 deletions tests/draft4/ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,93 @@
"valid": false
}
]
},
{
"description": "Recursive references between schemas",
"schema": {
"id": "http://localhost:1234/tree",
"description": "tree of nodes",
"type": "object",
"properties": {
"meta": {"type": "string"},
"nodes": {
"type": "array",
"items": {"$ref": "node"}
}
},
"required": ["meta", "nodes"],
"definitions": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see definitions used in this schema.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Relequestual {"$ref": "node"} points to http://localhost:1234/node which is defined in #/definitions/node

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How exactly does {"$ref": "node"} points to http://localhost:1234/node ? I don't follow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base URI is http://localhost:1234/tree when you resolve relative uri node it becomes http://localhost:1234/node.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's section 5.2 of RFC 3986

"node": {
"id": "http://localhost:1234/node",
"description": "node",
"type": "object",
"properties": {
"value": {"type": "number"},
"subtree": {"$ref": "tree"}
},
"required": ["value"]
}
}
},
"tests": [
{
"description": "valid tree",
"data": {
"meta": "root",
"nodes": [
{
"value": 1,
"subtree": {
"meta": "child",
"nodes": [
{"value": 1.1},
{"value": 1.2}
]
}
},
{
"value": 2,
"subtree": {
"meta": "child",
"nodes": [
{"value": 2.1},
{"value": 2.2}
]
}
}
]
},
"valid": true
},
{
"description": "invalid tree",
"data": {
"meta": "root",
"nodes": [
{
"value": 1,
"subtree": {
"meta": "child",
"nodes": [
{"value": "string is invalid"},
{"value": 1.2}
]
}
},
{
"value": 2,
"subtree": {
"meta": "child",
"nodes": [
{"value": 2.1},
{"value": 2.2}
]
}
}
]
},
"valid": false
}
]
}
]
103 changes: 100 additions & 3 deletions tests/draft4/refRemote.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
]
},
{
"description": "change resolution scope",
"description": "base URI change",
"schema": {
"id": "http://localhost:1234/",
"items": {
Expand All @@ -60,15 +60,112 @@
},
"tests": [
{
"description": "changed scope ref valid",
"description": "base URI change ref valid",
"data": [[1]],
"valid": true
},
{
"description": "changed scope ref invalid",
"description": "base URI change ref invalid",
"data": [["a"]],
"valid": false
}
]
},
{
"description": "base URI change - change folder",
"schema": {
"id": "http://localhost:1234/scope_change_defs1.json",
"type" : "object",
"properties": {
"list": {"$ref": "#/definitions/baz"}
},
"definitions": {
"baz": {
"id": "folder/",
"type": "array",
"items": {"$ref": "folderInteger.json"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is folderInteger.json? Maybe I just don't understand this schema or the use of id, I don't know =/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Relequestual there is a convention already used in other tests to assume that schemas at http://localhost:1234/ are files in remotes folder, folderInteger.json is in remotes/folder, its full URI is http://localhost:1234/folder/folderInteger.json (and there is another existing test that points to it).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
},
"tests": [
{
"description": "number is valid",
"data": {"list": [1]},
"valid": true
},
{
"description": "string is invalid",
"data": {"list": ["a"]},
"valid": false
}
]
},
{
"description": "base URI change - change folder in subschema",
"schema": {
"id": "http://localhost:1234/scope_change_defs2.json",
"type" : "object",
"properties": {
"list": {"$ref": "#/definitions/baz/definitions/bar"}
},
"definitions": {
"baz": {
"id": "folder/",
"definitions": {
"bar": {
"type": "array",
"items": {"$ref": "folderInteger.json"}
}
}
}
}
},
"tests": [
{
"description": "number is valid",
"data": {"list": [1]},
"valid": true
},
{
"description": "string is invalid",
"data": {"list": ["a"]},
"valid": false
}
]
},
{
"description": "root ref in remote ref",
"schema": {
"id": "http://localhost:1234/object",
"type": "object",
"properties": {
"name": {"$ref": "name.json#/definitions/orNull"}
}
},
"tests": [
{
"description": "string is valid",
"data": {
"name": "foo"
},
"valid": true
},
{
"description": "null is valid",
"data": {
"name": null
},
"valid": true
},
{
"description": "object is invalid",
"data": {
"name": {
"name": null
}
},
"valid": false
}
]
}
]
88 changes: 88 additions & 0 deletions tests/draft6/ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,93 @@
"valid": false
}
]
},
{
"description": "Recursive references between schemas",
"schema": {
"$id": "http://localhost:1234/tree",
"description": "tree of nodes",
"type": "object",
"properties": {
"meta": {"type": "string"},
"nodes": {
"type": "array",
"items": {"$ref": "node"}
}
},
"required": ["meta", "nodes"],
"definitions": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see definitions used in this schema.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Relequestual it's the same test as above, just in draft6 folder - please see the comment above

"node": {
"$id": "http://localhost:1234/node",
"description": "node",
"type": "object",
"properties": {
"value": {"type": "number"},
"subtree": {"$ref": "tree"}
},
"required": ["value"]
}
}
},
"tests": [
{
"description": "valid tree",
"data": {
"meta": "root",
"nodes": [
{
"value": 1,
"subtree": {
"meta": "child",
"nodes": [
{"value": 1.1},
{"value": 1.2}
]
}
},
{
"value": 2,
"subtree": {
"meta": "child",
"nodes": [
{"value": 2.1},
{"value": 2.2}
]
}
}
]
},
"valid": true
},
{
"description": "invalid tree",
"data": {
"meta": "root",
"nodes": [
{
"value": 1,
"subtree": {
"meta": "child",
"nodes": [
{"value": "string is invalid"},
{"value": 1.2}
]
}
},
{
"value": 2,
"subtree": {
"meta": "child",
"nodes": [
{"value": 2.1},
{"value": 2.2}
]
}
}
]
},
"valid": false
}
]
}
]
Loading