Skip to content

Commit 003211e

Browse files
Merge remote-tracking branch 'origin/master'
2 parents ec819a7 + e328cb2 commit 003211e

File tree

13 files changed

+438
-7
lines changed

13 files changed

+438
-7
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ JSON Schema $Ref Parser
88
[![npm](https://img.shields.io/npm/v/json-schema-ref-parser.svg)](https://www.npmjs.com/package/json-schema-ref-parser)
99
[![Dependencies](https://david-dm.org/APIDevTools/json-schema-ref-parser.svg)](https://david-dm.org/APIDevTools/json-schema-ref-parser)
1010
[![License](https://img.shields.io/npm/l/json-schema-ref-parser.svg)](LICENSE)
11+
[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser)
1112

1213

1314
[![OS and Browser Compatibility](https://apitools.dev/img/badges/ci-badges-with-ie.svg)](https://github.com/APIDevTools/json-schema-ref-parser/blob/master/.github/workflows/CI-CD.yaml)
@@ -138,17 +139,21 @@ To build/test the project locally on your computer:
138139
2. __Install dependencies__<br>
139140
`npm install`
140141

141-
3. __Run the build script__<br>
142-
`npm run build`
143-
144-
4. __Run the tests__<br>
142+
3. __Run the tests__<br>
145143
`npm test`
146144

147145

146+
148147
License
149148
--------------------------
150149
JSON Schema $Ref Parser is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.
151150

151+
If you use JSON Schema $Ref Parser, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser) to thank us for our work.
152+
153+
By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. Read more about Treeware at [treeware.earth](http://treeware.earth)
154+
155+
156+
152157
Big Thanks To
153158
--------------------------
154159
Thanks to these awesome companies for their support of Open Source developers ❤

lib/util/url.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@ exports.resolve = require("url").resolve;
2929
* @returns {string}
3030
*/
3131
exports.cwd = function cwd () {
32-
return process.browser ? location.href : process.cwd() + "/";
32+
if (process.browser) {
33+
return location.href;
34+
}
35+
36+
let path = process.cwd();
37+
38+
let lastChar = path.slice(-1);
39+
if (lastChar === "/" || lastChar === "\\") {
40+
return path;
41+
}
42+
else {
43+
return path + "/";
44+
}
3345
};
3446

3547
/**

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-ref-parser",
3-
"version": "7.1.3",
3+
"version": "7.1.4",
44
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
55
"keywords": [
66
"json",
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"use strict";
2+
3+
const { expect } = require("chai");
4+
const { resolve } = require("path");
5+
const $RefParser = require("../../..");
6+
const path = require("../../utils/path");
7+
const helper = require("../../utils/helper");
8+
const url = require("../../../lib/util/url");
9+
const parsedSchema = require("./parsed");
10+
const dereferencedSchema = require("./dereferenced");
11+
const bundledSchema = require("./bundled");
12+
13+
describe("When executed in the context of root directory", () => {
14+
// Store the OS root directory
15+
const root = resolve("/");
16+
17+
// Store references to the original methods
18+
const originalProcessCwd = process.cwd;
19+
const originalUrlCwd = url.cwd;
20+
21+
/**
22+
* A mock `process.cwd()` implementation that always returns the root diretory
23+
*/
24+
function mockProcessCwd () {
25+
return root;
26+
}
27+
28+
/**
29+
* Temporarily mocks `process.cwd()` while calling the real `url.cwd()` implemenation
30+
*/
31+
function mockUrlCwd () {
32+
try {
33+
process.cwd = mockProcessCwd;
34+
return originalUrlCwd.apply(null, arguments);
35+
}
36+
finally {
37+
process.cwd = originalProcessCwd;
38+
}
39+
}
40+
41+
beforeEach("Mock process.cwd and url.cwd", () => {
42+
url.cwd = mockUrlCwd;
43+
});
44+
45+
afterEach("Restore process.cwd and url.cwd", () => {
46+
url.cwd = originalUrlCwd;
47+
process.cwd = originalProcessCwd; // already restored by the finally block above, but just in case
48+
});
49+
50+
51+
it("should parse successfully from an absolute path", async () => {
52+
let parser = new $RefParser();
53+
const schema = await parser.parse(path.abs("specs/absolute-root/absolute-root.yaml"));
54+
expect(schema).to.equal(parser.schema);
55+
expect(schema).to.deep.equal(parsedSchema.schema);
56+
expect(parser.$refs.paths()).to.deep.equal([
57+
path.abs("specs/absolute-root/absolute-root.yaml")
58+
]);
59+
});
60+
61+
it("should parse successfully from a url", async () => {
62+
let parser = new $RefParser();
63+
const schema = await parser.parse(path.url("specs/absolute-root/absolute-root.yaml"));
64+
expect(schema).to.equal(parser.schema);
65+
expect(schema).to.deep.equal(parsedSchema.schema);
66+
expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/absolute-root.yaml")]);
67+
});
68+
69+
it("should resolve successfully from an absolute path", helper.testResolve(
70+
path.abs("specs/absolute-root/absolute-root.yaml"),
71+
path.abs("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema,
72+
path.abs("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions,
73+
path.abs("specs/absolute-root/definitions/name.yaml"), parsedSchema.name,
74+
path.abs("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString
75+
));
76+
77+
it("should resolve successfully from a url", helper.testResolve(
78+
path.url("specs/absolute-root/absolute-root.yaml"),
79+
path.url("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema,
80+
path.url("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions,
81+
path.url("specs/absolute-root/definitions/name.yaml"), parsedSchema.name,
82+
path.url("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString
83+
));
84+
85+
it("should dereference successfully", async () => {
86+
let parser = new $RefParser();
87+
const schema = await parser.dereference(path.abs("specs/absolute-root/absolute-root.yaml"));
88+
expect(schema).to.equal(parser.schema);
89+
expect(schema).to.deep.equal(dereferencedSchema);
90+
// Reference equality
91+
expect(schema.properties.name).to.equal(schema.definitions.name);
92+
expect(schema.definitions["required string"])
93+
.to.equal(schema.definitions.name.properties.first)
94+
.to.equal(schema.definitions.name.properties.last)
95+
.to.equal(schema.properties.name.properties.first)
96+
.to.equal(schema.properties.name.properties.last);
97+
// The "circular" flag should NOT be set
98+
expect(parser.$refs.circular).to.equal(false);
99+
});
100+
101+
it("should bundle successfully", async () => {
102+
let parser = new $RefParser();
103+
const schema = await parser.bundle(path.abs("specs/absolute-root/absolute-root.yaml"));
104+
expect(schema).to.equal(parser.schema);
105+
expect(schema).to.deep.equal(bundledSchema);
106+
});
107+
});
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict";
2+
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

0 commit comments

Comments
 (0)