Skip to content

Commit 6c78ceb

Browse files
Added more realistic sample files, including $refs to external files to ensure that references are still resolved correctly in the root directory
1 parent 2c49146 commit 6c78ceb

File tree

8 files changed

+327
-16
lines changed

8 files changed

+327
-16
lines changed

test/specs/absolute-root/absolute-root.spec.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,57 @@ describe("When executed in the context of root directory", () => {
3333

3434
it("should parse successfully from an absolute path", async () => {
3535
let parser = new $RefParser();
36-
const schema = await parser.parse(path.abs("specs/absolute-root/empty-object.json"));
36+
const schema = await parser.parse(path.abs("specs/absolute-root/absolute-root.yaml"));
3737
expect(schema).to.equal(parser.schema);
38-
expect(schema).to.deep.equal(parsedSchema);
39-
expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/absolute-root/empty-object.json")]);
38+
expect(schema).to.deep.equal(parsedSchema.schema);
39+
expect(parser.$refs.paths()).to.deep.equal([
40+
path.abs("specs/absolute-root/absolute-root.yaml")
41+
]);
4042
});
4143

42-
4344
it("should parse successfully from a url", async () => {
4445
let parser = new $RefParser();
45-
const schema = await parser.parse(path.url("specs/absolute-root/empty-object.json"));
46+
const schema = await parser.parse(path.url("specs/absolute-root/absolute-root.yaml"));
4647
expect(schema).to.equal(parser.schema);
47-
expect(schema).to.deep.equal(parsedSchema);
48-
expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/empty-object.json")]);
48+
expect(schema).to.deep.equal(parsedSchema.schema);
49+
expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/absolute-root.yaml")]);
4950
});
5051

5152
it("should resolve successfully from an absolute path", helper.testResolve(
52-
path.abs("specs/absolute-root/empty-object.json"),
53-
path.abs("specs/absolute-root/empty-object.json"), parsedSchema,
53+
path.abs("specs/absolute-root/absolute-root.yaml"),
54+
path.abs("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema,
55+
path.abs("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions,
56+
path.abs("specs/absolute-root/definitions/name.yaml"), parsedSchema.name,
57+
path.abs("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString
5458
));
5559

5660
it("should resolve successfully from a url", helper.testResolve(
57-
path.url("specs/absolute-root/empty-object.json"),
58-
path.url("specs/absolute-root/empty-object.json"), parsedSchema,
61+
path.url("specs/absolute-root/absolute-root.yaml"),
62+
path.url("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema,
63+
path.url("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions,
64+
path.url("specs/absolute-root/definitions/name.yaml"), parsedSchema.name,
65+
path.url("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString
5966
));
6067

6168
it("should dereference successfully", async () => {
6269
let parser = new $RefParser();
63-
const schema = await parser.dereference(path.abs("specs/absolute-root/empty-object.json"));
70+
const schema = await parser.dereference(path.abs("specs/absolute-root/absolute-root.yaml"));
6471
expect(schema).to.equal(parser.schema);
6572
expect(schema).to.deep.equal(dereferencedSchema);
73+
// Reference equality
74+
expect(schema.properties.name).to.equal(schema.definitions.name);
75+
expect(schema.definitions["required string"])
76+
.to.equal(schema.definitions.name.properties.first)
77+
.to.equal(schema.definitions.name.properties.last)
78+
.to.equal(schema.properties.name.properties.first)
79+
.to.equal(schema.properties.name.properties.last);
6680
// The "circular" flag should NOT be set
6781
expect(parser.$refs.circular).to.equal(false);
6882
});
6983

7084
it("should bundle successfully", async () => {
7185
let parser = new $RefParser();
72-
const schema = await parser.bundle(path.abs("specs/absolute-root/empty-object.json"));
86+
const schema = await parser.bundle(path.abs("specs/absolute-root/absolute-root.yaml"));
7387
expect(schema).to.equal(parser.schema);
7488
expect(schema).to.deep.equal(bundledSchema);
7589
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
title: Person
2+
type: object
3+
required:
4+
- name
5+
properties:
6+
name:
7+
$ref: "#/definitions/name"
8+
age:
9+
type: integer
10+
minimum: 0
11+
gender:
12+
type: string
13+
enum:
14+
- male
15+
- female
16+
definitions:
17+
$ref: definitions/definitions.json

test/specs/absolute-root/bundled.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
11
"use strict";
22

3-
module.exports = {};
3+
module.exports =
4+
{
5+
title: "Person",
6+
type: "object",
7+
required: [
8+
"name"
9+
],
10+
properties: {
11+
name: {
12+
$ref: "#/definitions/name"
13+
},
14+
age: {
15+
type: "integer",
16+
minimum: 0
17+
},
18+
gender: {
19+
type: "string",
20+
enum: [
21+
"male",
22+
"female"
23+
]
24+
}
25+
},
26+
definitions: {
27+
"required string": {
28+
title: "required string",
29+
type: "string",
30+
minLength: 1
31+
},
32+
string: {
33+
$ref: "#/definitions/required%20string/type"
34+
},
35+
name: {
36+
title: "name",
37+
type: "object",
38+
required: [
39+
"first",
40+
"last"
41+
],
42+
properties: {
43+
first: {
44+
$ref: "#/definitions/required%20string"
45+
},
46+
last: {
47+
$ref: "#/definitions/required%20string"
48+
},
49+
middle: {
50+
type: {
51+
$ref: "#/definitions/required%20string/type"
52+
},
53+
minLength: {
54+
$ref: "#/definitions/required%20string/minLength"
55+
}
56+
},
57+
prefix: {
58+
$ref: "#/definitions/required%20string",
59+
minLength: 3
60+
},
61+
suffix: {
62+
$ref: "#/definitions/name/properties/prefix",
63+
type: "string",
64+
maxLength: 3
65+
}
66+
}
67+
}
68+
}
69+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"required string": {
3+
"$ref": "required-string.yaml"
4+
},
5+
"string": {
6+
"$ref": "#/required%20string/type"
7+
},
8+
"name": {
9+
"$ref": "../definitions/name.yaml"
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
title: name
2+
type: object
3+
required:
4+
- first
5+
- last
6+
properties:
7+
first:
8+
$ref: ../definitions/definitions.json#/required string
9+
last:
10+
$ref: ./required-string.yaml
11+
middle:
12+
type:
13+
$ref: "#/properties/first/type"
14+
minLength:
15+
$ref: "#/properties/first/minLength"
16+
prefix:
17+
$ref: "#/properties/last"
18+
minLength: 3
19+
suffix:
20+
type: string
21+
$ref: "#/properties/prefix"
22+
maxLength: 3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
title: required string
2+
type: string
3+
minLength: 1
Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,101 @@
11
"use strict";
22

3-
module.exports = {};
3+
module.exports =
4+
{
5+
title: "Person",
6+
type: "object",
7+
required: [
8+
"name"
9+
],
10+
properties: {
11+
name: {
12+
title: "name",
13+
type: "object",
14+
required: [
15+
"first",
16+
"last"
17+
],
18+
properties: {
19+
first: {
20+
title: "required string",
21+
type: "string",
22+
minLength: 1
23+
},
24+
last: {
25+
title: "required string",
26+
type: "string",
27+
minLength: 1
28+
},
29+
middle: {
30+
type: "string",
31+
minLength: 1
32+
},
33+
prefix: {
34+
title: "required string",
35+
type: "string",
36+
minLength: 3
37+
},
38+
suffix: {
39+
title: "required string",
40+
type: "string",
41+
minLength: 3,
42+
maxLength: 3
43+
}
44+
}
45+
},
46+
age: {
47+
type: "integer",
48+
minimum: 0
49+
},
50+
gender: {
51+
type: "string",
52+
enum: [
53+
"male",
54+
"female"
55+
]
56+
}
57+
},
58+
definitions: {
59+
"required string": {
60+
title: "required string",
61+
type: "string",
62+
minLength: 1
63+
},
64+
string: "string",
65+
name: {
66+
title: "name",
67+
type: "object",
68+
required: [
69+
"first",
70+
"last"
71+
],
72+
properties: {
73+
first: {
74+
title: "required string",
75+
type: "string",
76+
minLength: 1
77+
},
78+
last: {
79+
title: "required string",
80+
type: "string",
81+
minLength: 1
82+
},
83+
middle: {
84+
type: "string",
85+
minLength: 1
86+
},
87+
prefix: {
88+
title: "required string",
89+
type: "string",
90+
minLength: 3
91+
},
92+
suffix: {
93+
title: "required string",
94+
type: "string",
95+
minLength: 3,
96+
maxLength: 3
97+
}
98+
}
99+
}
100+
}
101+
};

test/specs/absolute-root/parsed.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,83 @@
11
"use strict";
22

3-
module.exports = {};
3+
module.exports =
4+
{
5+
schema: {
6+
definitions: {
7+
$ref: "definitions/definitions.json"
8+
},
9+
required: [
10+
"name"
11+
],
12+
type: "object",
13+
properties: {
14+
gender: {
15+
enum: [
16+
"male",
17+
"female"
18+
],
19+
type: "string"
20+
},
21+
age: {
22+
minimum: 0,
23+
type: "integer"
24+
},
25+
name: {
26+
$ref: "#/definitions/name"
27+
}
28+
},
29+
title: "Person"
30+
},
31+
32+
definitions: {
33+
"required string": {
34+
$ref: "required-string.yaml"
35+
},
36+
string: {
37+
$ref: "#/required%20string/type"
38+
},
39+
name: {
40+
$ref: "../definitions/name.yaml"
41+
}
42+
},
43+
44+
name: {
45+
required: [
46+
"first",
47+
"last"
48+
],
49+
type: "object",
50+
properties: {
51+
middle: {
52+
minLength: {
53+
$ref: "#/properties/first/minLength"
54+
},
55+
type: {
56+
$ref: "#/properties/first/type"
57+
}
58+
},
59+
prefix: {
60+
minLength: 3,
61+
$ref: "#/properties/last"
62+
},
63+
last: {
64+
$ref: "./required-string.yaml"
65+
},
66+
suffix: {
67+
$ref: "#/properties/prefix",
68+
type: "string",
69+
maxLength: 3
70+
},
71+
first: {
72+
$ref: "../definitions/definitions.json#/required string"
73+
}
74+
},
75+
title: "name"
76+
},
77+
78+
requiredString: {
79+
minLength: 1,
80+
type: "string",
81+
title: "required string"
82+
}
83+
};

0 commit comments

Comments
 (0)