使用SPectral扩展规则

时间:2019-09-30 12:28:00

标签: yaml static-analysis jsonpath openapi 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,我也希望它在这两种情况之一中都将失败-仍然没有警告和错误。似乎根本没有应用此规则。

1 个答案:

答案 0 :(得分:0)

我找到了。规则定义中需要一个字段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)