使用 SPectral 扩展规则

Extending rules using SPectral

我正在关注 Spectral's documentation,我正在尝试将我的自定义规则添加为 oas3 规则集的扩展,看起来文档中的规则被完全忽略了。

我的 OpenApi 3.0 规范文件:

openapi: 3.0.2
info:
  title: Project info
  description: |
    Project description
  contact:
    name: Test Testable
    email: test@test.test
  version: 1.0.0
servers:
- url: http://localhost:8080
tags:
- name: test-tag
paths:
  /test:
    get:
      tags:
      - test-tag
      summary: Some summary
      operationId: operationId
      description: Operation description
      responses:
        200:
          description: OKK

我的 .spectral.yml 文件:

extends: spectral:oas3
rules:
  my-rule-name:
    description: Tags must have a description.
    given: $.tags[*]
    then:
      field: description
      function: truthy

我的 API 规范包含 tags 但是那里的标签没有任何描述所以它应该失败但它没有:

>spectral lint api.yml

OpenAPI 3.x detected
No errors or warnings found!

即使我尝试更改为 function: falsy,我预计在这两种情况之一中它应该会失败 - 仍然没有警告也没有错误。这条规则似乎根本没有应用。

我找到了。规则定义中需要一个字段recommended: true。文档提到了它,但不知何故过于简短:

已更新.spectral.yml

extends: spectral:oas3
rules:
  my-rule-name:
    description: Tags must have a description.
    given: $.tags[*]
    recommended: true
    then:
      field: description
      function: truthy

并输出:

13:3  warning  my-rule-name  Tags must have a description.

✖ 1 problem (0 errors, 1 warning, 0 infos)