Skip to content

Using functions in the ruleset #8

@UniperMaster

Description

@UniperMaster

Do you have an example of using function in the workset

I tried with the following ruleset and function but it fails to run

# Source: https://docs.stoplight.io/docs/spectral/4dec24461f3af-open-api-rules
extends:
 - spectral:oas
functionsDir: "./functions"
functions:
 - consistent-response-body
rules:
 info-contact: off
 no-$ref-siblings: off
 oas2-api-host: off
 oas2-api-schemes: off
 openapi-tags: off
 operation-description: off
 operation-tags: off
 operation-tag-defined: off

 # Note: The Spectral VSCode extension will not display "hint" messages, so
 # use "info" rather than "hint".
 # just a note here

 az-additional-properties-and-properties:
   description: Don't specify additionalProperties as a sibling of properties.
   severity: warn
   formats: ["oas2", "oas3"]
   given: $..[?(@object() && @.type === 'object' && @.properties)]
   then:
     field: additionalProperties
     function: falsy


 az-consistent-response-body:
   description: Ensure the get, put, and patch response body schemas are consistent.
   message: "{{error}}"
   severity: warn
   formats: ["oas2"]
   given: $.paths.*
   then:
     function: consistent-response-body

and function

// If put or patch is a create (returns 201), then verify that put, get, and patch response body
// schemas are consistent.

// path Item should be a [path item object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathItemObject).
// This function assumes it is running on a resolved doc.
module.exports = (pathItem, _opts, paths) => {
if (pathItem === null || typeof pathItem !== 'object') {
  return [];
}
const path = paths.path || paths.target || [];

const errors = [];

// resource schema is create operation response schema
const createResponseSchema = ((op) => op?.responses?.['201']?.schema);
const resourceSchema = createResponseSchema(pathItem.put) || createResponseSchema(pathItem.patch);
if (resourceSchema) {
  ['put', 'get', 'patch'].forEach((method) => {
    const responseSchema = pathItem[method]?.responses?.['200']?.schema;
    if (responseSchema && responseSchema !== resourceSchema) {
      errors.push({
        message: 'Response body schema does not match create response body schema.',
        path: [...path, method, 'responses', '200', 'schema'],
      });
    }
  });
}

return errors;
};

This is based upon the azure ruleset azure-api-style-guide

Ive also attached the azure fucntions logs
query_data.csv
log

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions