liveBook · Manning

Spectral truthy function

As explained in Automating API Delivery

The truthy function reports a violation when its input is false, empty, zero, null, or undefined.

The Spectral truthy function reports a rule violation when its input is false, an empty string, 0, null, or undefined. Applied to $.info with field: description, it checks that an API definition provides info.description. section 3.6

Require info.description

A Spectral rule can set given to $.info, field to description, and function to truthy. Running spectral lint minimal.oas.yaml --ruleset simple.ruleset.yaml reports an error when the description is absent, allowing the rule severity to determine pipeline failure behavior. section 3.6

Listing 3.1 A Spectral rule to check that info.description is present
rules:
  info-description-is-required:      #1
    message: "API definition files must 
 have info.description defined"        #2
    severity: error                  #3
    given: $.info                 #4
    then:
      field: description      #5
      function: truthy      #6

Inspect the complete rule definition, including its message, error severity, target object, field, and truthy function. section 3.6

How Automating API Delivery covers it

How deep it goes
Defines the falsey values checked and demonstrates rule configuration, but does not explain function internals or custom implementations.
Examples
Uses an info.description requirement, a minimal API definition, and Spectral CLI error output.
What it assumes
Assumes familiarity with API definition files and basic Spectral rule properties such as given, then, field, and severity.
What you can do after
Apply it via the Spectral CLI to detect missing or falsey required values in API definitions.
How it connects
Connects Spectral rules, API linting, OpenAPI definitions, rule severity, and Spectral CLI failure thresholds.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Automating API Delivery. The section links point to the parts of the book it draws on.