Skip to content

Commit

Permalink
fix: report on child keys when using pattern or notPattern assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Oct 17, 2023
1 parent 4d66ace commit 2388697
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
6 changes: 3 additions & 3 deletions __tests__/lint/assertions-map-types/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ rule/encodingMap failed because the EncodingMap didn't meet the assertions: "his
Error was generated by the rule/encodingMap rule.
[4] openapi.yaml:37:11 at #/paths/~1pets/get/responses/200/links
[4] openapi.yaml:38:13 at #/paths/~1pets/get/responses/200/links/address
rule/linkMap failed because the LinksMap didn't meet the assertions: "address" should match a regex /^pet/
35 | historyMetadata:
36 | contentType: application/json; charset=utf-8
37 | links: # LinksMap
| ^^^^^
38 | address:
| ^^^^^^^
39 | operationId: getUserAddress
40 | parameters:
Error was generated by the rule/linkMap rule.
Expand Down
21 changes: 21 additions & 0 deletions __tests__/lint/assertions-pattern-report-location/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.1.0
paths:
/one:
description: A correct path item name
post:
requestBody:
content:
application/json:
schema:
type: string
enum:
- Correct
- Wrong
/two/test:
description: A wrong path item name
/two/ref:
$ref: '#/components/paths/'
components:
pathItems:
ReferencedPathItem:
description: Referenced from a wrong path item name
26 changes: 26 additions & 0 deletions __tests__/lint/assertions-pattern-report-location/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apis:
main:
root: ./openapi.yaml

rules:
rule/restful-paths:
subject:
type: Paths
assertions:
notPattern: '/[^{/}]+/[^{/}]+/'
message: Two consecutive path segments don't have a variable

rule/path-starts-from-correct:
subject:
type: Paths
assertions:
pattern: /^\/two/
message: Path item should start with the `two` prefix

rule/enums:
subject:
type: Schema
property: enum
assertions:
pattern: /Correct/
message: Enums should contain the `Correct` word
20 changes: 16 additions & 4 deletions packages/core/src/rules/common/assertions/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export const runOnValuesSet = new Set<keyof Asserts>([
]);

export const asserts: Asserts = {
pattern: (value: string | string[], condition: string, { baseLocation }: AssertionFnContext) => {
pattern: (
value: string | string[],
condition: string,
{ baseLocation, rawValue }: AssertionFnContext
) => {
if (typeof value === 'undefined' || isPlainObject(value)) return []; // property doesn't exist or is an object, no need to lint it with this assert
const values = Array.isArray(value) ? value : [value];
const regex = regexFromString(condition);
Expand All @@ -76,15 +80,19 @@ export const asserts: Asserts = {
(_val) =>
!regex?.test(_val) && {
message: `"${_val}" should match a regex ${condition}`,
location: runOnValue(value) ? baseLocation : baseLocation.key(),
location: runOnValue(value)
? baseLocation
: isPlainObject(rawValue)
? baseLocation.child(_val).key()
: baseLocation.key(),
}
)
.filter(isTruthy);
},
notPattern: (
value: string | string[],
condition: string,
{ baseLocation }: AssertionFnContext
{ baseLocation, rawValue }: AssertionFnContext
) => {
if (typeof value === 'undefined' || isPlainObject(value)) return []; // property doesn't exist or is an object, no need to lint it with this assert
const values = Array.isArray(value) ? value : [value];
Expand All @@ -95,7 +103,11 @@ export const asserts: Asserts = {
(_val) =>
regex?.test(_val) && {
message: `"${_val}" should not match a regex ${condition}`,
location: runOnValue(value) ? baseLocation : baseLocation.key(),
location: runOnValue(value)
? baseLocation
: isPlainObject(rawValue)
? baseLocation.child(_val).key()
: baseLocation.key(),
}
)
.filter(isTruthy);
Expand Down

0 comments on commit 2388697

Please sign in to comment.