From 32a876ba18588609706fc5e817d0599a93757885 Mon Sep 17 00:00:00 2001 From: Andrew Tatomyr Date: Tue, 16 Aug 2022 10:14:37 +0300 Subject: [PATCH] fix: add assertions severity 'off' (#806) --- .../assertions-non-empty-off/.redocly.yaml | 13 +++++++++ .../assertions-non-empty-off/openapi.yaml | 29 +++++++++++++++++++ .../lint/assertions-non-empty-off/snapshot.js | 11 +++++++ .../assertions-non-empty-warn/.redocly.yaml | 13 +++++++++ .../assertions-non-empty-warn/openapi.yaml | 29 +++++++++++++++++++ .../assertions-non-empty-warn/snapshot.js | 26 +++++++++++++++++ packages/core/src/walk.ts | 26 ++++++++++------- 7 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 __tests__/lint/assertions-non-empty-off/.redocly.yaml create mode 100644 __tests__/lint/assertions-non-empty-off/openapi.yaml create mode 100644 __tests__/lint/assertions-non-empty-off/snapshot.js create mode 100644 __tests__/lint/assertions-non-empty-warn/.redocly.yaml create mode 100644 __tests__/lint/assertions-non-empty-warn/openapi.yaml create mode 100644 __tests__/lint/assertions-non-empty-warn/snapshot.js diff --git a/__tests__/lint/assertions-non-empty-off/.redocly.yaml b/__tests__/lint/assertions-non-empty-off/.redocly.yaml new file mode 100644 index 000000000..373207bcb --- /dev/null +++ b/__tests__/lint/assertions-non-empty-off/.redocly.yaml @@ -0,0 +1,13 @@ +apis: + main: + root: ./openapi.yaml + +styleguide: + rules: + assert/summary-non-empty: + subject: Operation + property: summary + message: Operation summary should not be empty + nonEmpty: true + severity: off + extends: [] \ No newline at end of file diff --git a/__tests__/lint/assertions-non-empty-off/openapi.yaml b/__tests__/lint/assertions-non-empty-off/openapi.yaml new file mode 100644 index 000000000..96aa64eef --- /dev/null +++ b/__tests__/lint/assertions-non-empty-off/openapi.yaml @@ -0,0 +1,29 @@ +openapi: 3.1.0 +servers: + - url: http://example.xyz +info: + license: + name: test licence + url: http://example.xyz + title: Example OpenAPI 3 definition. + description: Example description + version: '1.0' + contact: + name: qa + url: https://swagger.io/specification/#definitions + email: email@redoc.ly + +paths: + /pet/findByStatus: + get: + operationId: example + summary: + tags: + - foo + - baz + - bar + responses: + '200': + description: example description + '404': + description: example description diff --git a/__tests__/lint/assertions-non-empty-off/snapshot.js b/__tests__/lint/assertions-non-empty-off/snapshot.js new file mode 100644 index 000000000..d8aaa4c9b --- /dev/null +++ b/__tests__/lint/assertions-non-empty-off/snapshot.js @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`E2E lint assertions-non-empty-off 1`] = ` + +validating /openapi.yaml... +/openapi.yaml: validated in ms + +Woohoo! Your OpenAPI definition is valid. 🎉 + + +`; diff --git a/__tests__/lint/assertions-non-empty-warn/.redocly.yaml b/__tests__/lint/assertions-non-empty-warn/.redocly.yaml new file mode 100644 index 000000000..d6badee0d --- /dev/null +++ b/__tests__/lint/assertions-non-empty-warn/.redocly.yaml @@ -0,0 +1,13 @@ +apis: + main: + root: ./openapi.yaml + +styleguide: + rules: + assert/summary-non-empty: + subject: Operation + property: summary + message: Operation summary should not be empty + nonEmpty: true + severity: warn + extends: [] \ No newline at end of file diff --git a/__tests__/lint/assertions-non-empty-warn/openapi.yaml b/__tests__/lint/assertions-non-empty-warn/openapi.yaml new file mode 100644 index 000000000..96aa64eef --- /dev/null +++ b/__tests__/lint/assertions-non-empty-warn/openapi.yaml @@ -0,0 +1,29 @@ +openapi: 3.1.0 +servers: + - url: http://example.xyz +info: + license: + name: test licence + url: http://example.xyz + title: Example OpenAPI 3 definition. + description: Example description + version: '1.0' + contact: + name: qa + url: https://swagger.io/specification/#definitions + email: email@redoc.ly + +paths: + /pet/findByStatus: + get: + operationId: example + summary: + tags: + - foo + - baz + - bar + responses: + '200': + description: example description + '404': + description: example description diff --git a/__tests__/lint/assertions-non-empty-warn/snapshot.js b/__tests__/lint/assertions-non-empty-warn/snapshot.js new file mode 100644 index 000000000..c1601cdb4 --- /dev/null +++ b/__tests__/lint/assertions-non-empty-warn/snapshot.js @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`E2E lint assertions-non-empty-warn 1`] = ` + +validating /openapi.yaml... +[1] openapi.yaml:20:7 at #/paths/~1pet~1findByStatus/get/summary + +Operation summary should not be empty + +18 | get: +19 | operationId: example +20 | summary: + | ^^^^^^^^ +21 | tags: +22 | - foo + +Warning was generated by the summary-non-empty assertion rule. + + +/openapi.yaml: validated in ms + +Woohoo! Your OpenAPI definition is valid. 🎉 +You have 1 warning. + + +`; diff --git a/packages/core/src/walk.ts b/packages/core/src/walk.ts index cc0fe901f..25762da4e 100644 --- a/packages/core/src/walk.ts +++ b/packages/core/src/walk.ts @@ -18,6 +18,8 @@ import { import { pushStack, popStack } from './utils'; import { OasVersion } from './oas-types'; import { NormalizedNodeType, isNamedType } from './types'; +import type { RuleSeverity } from './config'; + type NonUndefined = string | number | boolean | symbol | bigint | object | Record; export type ResolveResult = @@ -66,7 +68,7 @@ export type Problem = { suggest?: string[]; location?: Partial | Array>; from?: LocationObject; - forceSeverity?: ProblemSeverity; + forceSeverity?: RuleSeverity; ruleId?: string; }; @@ -409,17 +411,19 @@ export function walkDocument(opts: { : [opts.location] : [{ ...currentLocation, reportOnKey: false }]; - ctx.problems.push({ - ruleId: opts.ruleId || ruleId, - severity: opts.forceSeverity || severity, - ...opts, - suggest: opts.suggest || [], - location: loc.map((loc: any) => { - return { ...currentLocation, reportOnKey: false, ...loc }; - }), - }); + const ruleSeverity = opts.forceSeverity || severity; + if (ruleSeverity !== 'off') { + ctx.problems.push({ + ruleId: opts.ruleId || ruleId, + severity: ruleSeverity, + ...opts, + suggest: opts.suggest || [], + location: loc.map((loc: any) => { + return { ...currentLocation, reportOnKey: false, ...loc }; + }), + }); + } } - function getVisitorDataFn(ruleId: string) { ctx.visitorsData[ruleId] = ctx.visitorsData[ruleId] || {}; return ctx.visitorsData[ruleId];