Spectral - usage of name() !== 'parameters' in jsonpath

(Taylor) #1

I see several of the provided rules have $..paths.*[?( name() !== 'parameters' )]
Could anyone give me a hand with understanding how name() !== 'parameters is being allowed as a filter and why its needed? What does name() evaluate to? I haven’t been able to locate any documentation around it and am not able to get it to return a match on any swagger files using jsonpath directly.

Thanks in advance!

(Ross McDonald) #2

So $..paths.*[?( name() !== 'parameters' )] roughly translates to:

  • Give everything with jsonpath $..paths.* - the values of each path item object in the spec
  • If the object key (name()) is equal to parameters, skip it - we only want operation objects

We use a custom version of jsonpath that allows for these extra filters based on the key value, which was needed since it’s not possible to use pure jsonpath for collecting only operation objects.