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
