liveBook · Manning

Then property

As explained in Automating API Delivery

A then property is a Spectral rule property that specifies the function and optional field used to evaluate selected input.

The then property defines the check Spectral performs on each value selected by given. It names a function and can optionally identify a field within the selected object. section 3.6

Configure then with two keywords:

  • function names the Spectral function that evaluates the input and reports a violation when it finds a problem. section 3.6
  • field is optional and narrows evaluation to a property of the selected object, such as description within $.info. section 3.6
  • given supplies the JSONPath selection; if it returns no elements, Spectral does not evaluate then. 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

A complete rule uses $.info as given, then targets the description field with Spectral’s truthy function. section 3.6

How Automating API Delivery covers it

How deep it goes
Defines function and optional field behavior, but does not detail custom-function implementation.
Examples
Checks that the description field exists under $.info using the truthy function.
What it assumes
Uses JSONPath selection and Spectral functions while explaining JSONPath root and property notation.
What you can do after
Build it from scratch by assigning a Spectral function and optional field to a JSONPath-selected object.
How it connects
Connects Spectral rulesets with JSONPath, Spectral functions, and OpenAPI definitions.

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.