Skip to content

Commit dde062e

Browse files
authored
Merge pull request #1571 from shockey/bug/1560-ref-formatting-validation
$ref formatting validation with whole-document paths
2 parents 162dc72 + f2935b3 commit dde062e

File tree

8 files changed

+818
-32
lines changed

8 files changed

+818
-32
lines changed

src/plugins/validation/get-timestamp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default function getTimestamp() {
22
if(typeof performance !== "undefined" && performance.now) {
33
return performance.now()
4-
} else if(typeof self.performance !== "undefined" && self.performance.now) {
4+
} else if(typeof self !== "undefined" && typeof self.performance !== "undefined" && self.performance.now) {
55
return self.performance.now()
66
} else {
77
return Date.now()

src/plugins/validation/semantic-validators/hook.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,15 @@ import { transformPathToArray } from "../path-translator"
55

66
import assign from "lodash/assign"
77
import getTimestamp from "../get-timestamp"
8-
let request = require.context("./validators/", true, /\.js$/)
8+
import rawValidators from "./validators"
99
let semanticValidators = []
1010

1111
let LOG_SEMVAL_PERF = process.env.NODE_ENV !== "production"
1212

13-
request.keys().forEach( function( key ){
14-
if( key === "./hook.js" ) {
15-
return
16-
}
17-
18-
if( !key.match(/js$/) ) {
19-
return
20-
}
21-
22-
if( key.slice(2).indexOf("/") > -1) {
23-
// skip files in subdirs
24-
return
25-
}
26-
13+
Object.keys(rawValidators).forEach( function( key ) {
2714
semanticValidators.push({
2815
name: toTitleCase(key).replace(".js", "").replace("./", ""),
29-
validate: request(key).validate
16+
validate: rawValidators[key]
3017
})
3118
})
3219

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { validate as dummy } from "./dummy"
2+
import { validate as form_data } from "./form-data"
3+
import { validate as items_required_for_array_objects } from "./items-required-for-array-objects"
4+
import { validate as operation_ids } from "./operation-ids"
5+
import { validate as operations } from "./operations"
6+
import { validate as parameters } from "./parameters"
7+
import { validate as paths } from "./paths"
8+
import { validate as refs } from "./refs"
9+
import { validate as schema } from "./schema"
10+
import { validate as security_definitions } from "./security-definitions"
11+
import { validate as security } from "./security"
12+
import { validate as walker } from "./walker"
13+
14+
export default {
15+
dummy,
16+
form_data,
17+
items_required_for_array_objects,
18+
operation_ids,
19+
operations,
20+
parameters,
21+
paths,
22+
refs,
23+
schema,
24+
security_definitions,
25+
security,
26+
walker
27+
}

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ import filter from "lodash/filter"
66
import startsWith from "lodash/startsWith"
77
import each from "lodash/each"
88

9-
export function validate({ jsSpec , specStr }) {
9+
function collectRefs(val, key, arr = []) {
10+
if(key === "$ref") {
11+
return arr.push(val)
12+
}
13+
14+
if(typeof val !== "object") {
15+
return
16+
}
17+
18+
Object.keys(val).map(k => collectRefs(val[k], k, arr))
19+
20+
return arr
21+
}
22+
23+
export function validate({ jsSpec }) {
1024
let errors = []
1125
let warnings = []
1226

1327
// Assertation 1
14-
// This is a "creative" way to approach the problem of collecting used $refs,
15-
// but other solutions required walking the jsSpec recursively to detect $refs,
16-
// which can be quite slow.
17-
let refRegex = /\$ref.*["'](.*)["']/g
18-
let match = refRegex.exec(specStr)
19-
let refs = []
20-
while(match !== null) {
21-
refs.push(match[1])
22-
match = refRegex.exec(specStr)
23-
}
28+
const refs = collectRefs(jsSpec, "")
2429

2530
// de-dupe the array, and filter out non-definition refs
2631
let definitionsRefs = filter(uniq(refs), v => startsWith(v, "#/definitions"))

src/plugins/validation/semantic-validators/validators/walker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ export function validate({ jsSpec }) {
8383
// eslint-disable-next-line no-unused-vars
8484
const [refUrl, refPath] = value.split("#")
8585

86-
if(!refPath || refPath[0] !== "/") {
86+
if(refPath && refPath[0] !== "/") {
8787
errors.push({
8888
path,
89-
message: "$refs must begin with `#/`"
89+
message: "$ref paths must begin with `#/`"
9090
})
9191
}
9292

0 commit comments

Comments
 (0)