liveBook · Manning

Given property

As explained in Automating API Delivery

A given property is a Spectral rule property containing JSONPath expressions that select the document elements to which the rule applies.

A given property is a Spectral rule property containing a JSONPath expression or collection of expressions. It selects the document elements where the rule is applied. A JSONPath query may return zero or more elements; when it returns none, Spectral does not evaluate the rule. section 3.6

Use the property to define the rule’s scope:

  • $ refers to the document root, while dot or bracket notation selects descendants such as $.info or $.info.version. section 3.6
  • $.info selects the entire info object, allowing then to check one of its properties. section 3.6
  • The given property selects the target; then supplies the function and optional field evaluated against that target. section 3.6

Require info.description

Set given to $.info and then.field to description. Spectral then applies the truthy function to that field, reporting an error when the description is absent or empty. 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 YAML rule shows given: $.info alongside then.field: description and then.function: truthy. section 3.6

How Automating API Delivery covers it

How deep it goes
Defines JSONPath targeting, zero-match behavior, and interaction with then, stopping short of comprehensive Spectral syntax.
Examples
Uses $.info to target an info object and $.info.version to illustrate descendant value selection.
What it assumes
Assumes basic Spectral rule structure while defining JSONPath root, child notation, and zero-match behavior.
What you can do after
Builds a Spectral rule that applies validation functions to JSONPath-selected API definition elements.
How it connects
Connects JSONPath with Spectral rulesets, rule evaluation, and the then property.

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.