首页 > 解决方案 > 使用光谱扩展规则

问题描述

我正在关注Spectral 的文档,我正在尝试将我的自定义规则添加为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我希望它在这两种情况之一中失败的情况 - 仍然没有警告和错误。看起来这条规则根本不适用。

标签: yamlstatic-analysisjsonpathopenapispectral

解决方案


我找到了。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)

推荐阅读