Skip to content

Commit 295ba14

Browse files
committed
Merge branch 'master' of github.com:swagger-api/swagger-editor
2 parents 5033f74 + e957a03 commit 295ba14

File tree

2 files changed

+79
-13
lines changed
  • src/plugins/validate-semantic/validators
  • test/plugins/validate-semantic

2 files changed

+79
-13
lines changed

src/plugins/validate-semantic/validators/refs.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import get from "lodash/get"
22

33
export const validateRefHasNoSiblings = () => system => {
4-
return Promise.all([
5-
system.validateSelectors.all$refArtifacts(),
6-
]).then(([nodes]) => {
4+
return system.validateSelectors.all$refArtifacts()
5+
.then((nodes) => {
76
const immSpecJson = system.specSelectors.specJson()
87
const specJson = immSpecJson.toJS ? immSpecJson.toJS() : {}
98

@@ -28,16 +27,9 @@ export const validateRefHasNoSiblings = () => system => {
2827

2928
// Add warnings for unused definitions
3029
export const validateUnusedDefinitions = () => (system) => {
31-
return Promise.all([
32-
system.validateSelectors.all$refs(),
33-
system.validateSelectors.all$refArtifacts()
34-
]).then(([refs, refArtifacts]) => {
35-
const references = (
36-
(refs.length ? refs : null)
37-
|| (refArtifacts.length ? refArtifacts : null)
38-
|| []
39-
).map(node => node.node)
40-
30+
return system.validateSelectors.all$refArtifacts()
31+
.then((nodes) => {
32+
const references = nodes.map(node => node.node)
4133
const errors = []
4234

4335
system.specSelectors.definitions()
@@ -55,3 +47,29 @@ export const validateUnusedDefinitions = () => (system) => {
5547
return errors
5648
})
5749
}
50+
51+
export const validateRefPathFormatting = () => (system) => {
52+
return system.validateSelectors.all$refArtifacts()
53+
.then((refArtifacts) => {
54+
55+
const errors = []
56+
refArtifacts.forEach((node) => {
57+
const value = node.node
58+
if(typeof value === "string") {
59+
// eslint-disable-next-line no-unused-vars
60+
const [refUrl, refPath] = value.split("#")
61+
62+
if(!refPath || refPath[0] !== "/") {
63+
errors.push({
64+
// $ref instead of $$ref
65+
path: [...node.path.slice(0, -1), "$ref"],
66+
message: "$ref paths must begin with `#/`",
67+
level: "error"
68+
})
69+
}
70+
}
71+
})
72+
73+
return errors
74+
})
75+
}

test/plugins/validate-semantic/refs.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,54 @@ describe("validation plugin - semantic - refs", function() {
5858
})
5959
})
6060

61+
})
62+
describe("Malformed $ref values", () => {
63+
it("should return an error when a JSON pointer lacks a leading `#/`", () => {
64+
const spec = {
65+
paths: {
66+
"/CoolPath": {
67+
$ref: "#myObj/abc"
68+
}
69+
},
70+
myObj: {
71+
abc: {
72+
type: "string"
73+
}
74+
}
75+
}
76+
77+
return validateHelper(spec)
78+
.then(system => {
79+
const allErrors = system.errSelectors.allErrors().toJS()
80+
expect(allErrors.length).toEqual(1)
81+
const firstError = allErrors[0]
82+
expect(firstError.message).toMatch("$ref paths must begin with `#/`")
83+
expect(firstError.level).toEqual("error")
84+
expect(firstError.path).toEqual(["paths", "/CoolPath", "$ref"])
85+
})
86+
})
87+
88+
it("should return no errors when a JSON pointer is a well-formed remote reference", () => {
89+
const spec = {
90+
paths: {
91+
"/CoolPath": {
92+
$ref: "http://google.com#/myObj/abc"
93+
}
94+
},
95+
myObj: {
96+
abc: {
97+
type: "string"
98+
}
99+
}
100+
}
101+
102+
return validateHelper(spec)
103+
.then(system => {
104+
const allErrors = system.errSelectors.allErrors().toJS()
105+
expect(allErrors).toEqual([])
106+
})
107+
})
108+
61109
})
62110
describe.skip("Refs are restricted in specific areas of a spec", () => {
63111
describe("Response $refs", () => {

0 commit comments

Comments
 (0)