Skip to content

Commit 512a01d

Browse files
authored
Merge pull request #1606 from shockey/bug/1601-z-anchor-semantic-validation
Add semantic validator for "\Z" anchors in regex patterns
2 parents 183e578 + bc032af commit 512a01d

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/plugins/validate/validators/schema.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,23 @@ export const validateReadOnlyPropertiesNotRequired = () => (system) => {
9191
}, [])
9292
})
9393
}
94+
95+
// See https://github.com/swagger-api/swagger-editor/issues/1601
96+
export const validateSchemaPatternHasNoZAnchors = () => (system) => {
97+
return system.validateSelectors
98+
.allSchemas()
99+
.then(nodes => {
100+
return nodes.reduce((acc, node) => {
101+
const schemaObj = node.node
102+
const { pattern } = schemaObj || {}
103+
if(typeof pattern === "string" && pattern.indexOf("\\Z") > -1) {
104+
acc.push({
105+
message: `"\\Z" anchors are not allowed in regular expression patterns`,
106+
path: [...node.path, "pattern"],
107+
level: "error",
108+
})
109+
}
110+
return acc
111+
}, [])
112+
})
113+
}

test/plugins/validate/schema.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,83 @@ describe("validation plugin - semantic - schema", function() {
509509
}
510510
}
511511

512+
return expectNoErrorsOrWarnings(spec)
513+
})
514+
})
515+
describe("'pattern' Z anchors", function() {
516+
it("should return an error when a schema has a Z anchor in its pattern", () => {
517+
518+
// Given
519+
const spec = {
520+
paths: {
521+
$ref: "#/definitions/asdf"
522+
},
523+
"definitions": {
524+
"asdf": {
525+
type: "string",
526+
pattern: "^[-a-zA-Z0-9_]+\\Z"
527+
}
528+
}
529+
}
530+
531+
// When
532+
return validateHelper(spec)
533+
.then(system => {
534+
535+
// Then
536+
expect(system.errSelectors.allErrors().count()).toEqual(1)
537+
const firstError = system.errSelectors.allErrors().first().toJS()
538+
expect(firstError.message).toEqual(`"\\Z" anchors are not allowed in regular expression patterns`)
539+
expect(firstError.path).toEqual(["definitions", "asdf", "pattern"])
540+
})
541+
542+
})
543+
it("should return an error when a subschema has a Z anchor in its pattern", () => {
544+
545+
// Given
546+
const spec = {
547+
paths: {
548+
$ref: "#/definitions/asdf"
549+
},
550+
"definitions": {
551+
"asdf": {
552+
type: "object",
553+
properties: {
554+
slug: {
555+
type: "string",
556+
pattern: "^[-a-zA-Z0-9_]+\\Z"
557+
}
558+
}
559+
}
560+
}
561+
}
562+
563+
// When
564+
return validateHelper(spec)
565+
.then(system => {
566+
567+
// Then
568+
expect(system.errSelectors.allErrors().count()).toEqual(1)
569+
const firstError = system.errSelectors.allErrors().first().toJS()
570+
expect(firstError.message).toEqual(`"\\Z" anchors are not allowed in regular expression patterns`)
571+
expect(firstError.path).toEqual(["definitions", "asdf", "properties", "slug", "pattern"])
572+
})
573+
574+
})
575+
576+
it("should not return an error when a regex pattern doesn't use a Z anchor", () => {
577+
const spec = {
578+
paths: {
579+
$ref: "#/definitions/asdf"
580+
},
581+
"definitions": {
582+
"asdf": {
583+
type: "string",
584+
pattern: "^[-a-zA-Z0-9_]+"
585+
}
586+
}
587+
}
588+
512589
return expectNoErrorsOrWarnings(spec)
513590
})
514591
})

0 commit comments

Comments
 (0)