Rule to Validate Schema Data Types Based on Paths #2300
-
I am looking for some help with a custom Spectral rule that I'm trying to write. Essentially the rule I'm working on should validate that schemas return the expected data type based on the path. Examples: The OpenAPI spec file that I am validating looks something like this (when it comes to paths & schemas): `paths: components: This is what the rule I have looks like so far:
The problem I am having is that the second given selector for the rule is selecting all GET paths (regardless if the path contains "/{id}" or not. Is there a way to change the selector for the rule so that it will only apply to the GET paths that contain "{id}"? For example, I want to the rule above to only apply if the path is "GET resources/{id}". But I don't want it the rule to apply if the path is "GET resources/" Is anyone able to help me with this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Update: I found a way to do this after more research online: Rules: #This rule finds GET paths without the /id (ex. GET resources/) and ensures that the schema for the 200 response returns type "array". schemas-return-array-data-types-when-expected: #This rule ensures that certain paths return "object" in the schema for the 200 response. (Ex. "GET resources/{id}", "POST resources/", and "PUT resources/{id}") schemas-return-object-data-types-when-expected: |
Beta Was this translation helpful? Give feedback.
Update: I found a way to do this after more research online:
Rules:
#This rule finds GET paths without the /id (ex. GET resources/) and ensures that the schema for the 200 response returns type "array".
schemas-return-array-data-types-when-expected:
description: Schemas should return the correct data types
message: "{{error}} : '{{path}}' (Schemas should return the correct data types. See API Standards page in Confluence (Route Verb Purposes section) for more information.)"
severity: warn
given:
$.paths[?( @property.match('^((?!.}$).)$') )].get.responses['200'][[schema]]
then:
field: type
function: pattern
functionOptions:
match: "array"
#This rule ensures that certain paths return "objec…