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 authored Oct 17, 2023
1 parent 2b2429b commit d703d22
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/shiny-months-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@redocly/openapi-core': patch
'@redocly/cli': patch
---

Changed the report location for `pattern` and `notPattern` assertions to be more precise.
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
18 changes: 18 additions & 0 deletions __tests__/lint/assertions-pattern-report-location/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
openapi: 3.1.0
paths:
/one:
description: A correct path item name
get:
parameters:
- name: Correct
in: header
- name: Wrong
in: header
/two/test:
description: A wrong path item name
/two/ref:
$ref: '#/components/paths/'
components:
pathItems:
ReferencedPathItem:
description: Referenced from a wrong path item name
19 changes: 19 additions & 0 deletions __tests__/lint/assertions-pattern-report-location/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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/parameters-name:
subject:
type: Parameter
property: name
assertions:
pattern: /Correct/
message: Parameters name should contain the `Correct` word
54 changes: 54 additions & 0 deletions __tests__/lint/assertions-pattern-report-location/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E lint assertions-pattern-report-location 1`] = `
validating /openapi.yaml...
[1] openapi.yaml:11:3 at #/paths/~1two~1test
Two consecutive path segments don't have a variable
9 | - name: Wrong
10 | in: header
11 | /two/test:
| ^^^^^^^^^
12 | description: A wrong path item name
13 | /two/ref:
Error was generated by the rule/restful-paths rule.
[2] openapi.yaml:13:3 at #/paths/~1two~1ref
Two consecutive path segments don't have a variable
11 | /two/test:
12 | description: A wrong path item name
13 | /two/ref:
| ^^^^^^^^
14 | $ref: '#/components/paths/'
15 | components:
Error was generated by the rule/restful-paths rule.
[3] openapi.yaml:9:17 at #/paths/~1one/get/parameters/1/name
Parameters name should contain the \`Correct\` word
7 | - name: Correct
8 | in: header
9 | - name: Wrong
| ^^^^^
10 | in: header
11 | /two/test:
Error was generated by the rule/parameters-name rule.
/openapi.yaml: validated in <test>ms
❌ Validation failed with 3 errors.
run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file.
`;
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

1 comment on commit d703d22

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 75.95% 4010/5280
🟡 Branches 65.91% 2140/3247
🟡 Functions 67.95% 651/958
🟡 Lines 76.13% 3758/4936

Test suite run success

641 tests passing in 93 suites.

Report generated by 🧪jest coverage report action from d703d22

Please sign in to comment.