Azure / Azure/APICenter-Analyzer

Using functions in the ruleset

Open
#8 2 comments 0 reactions 0 assignees View on GitHub
Investigating
Dominant language
Bicep
Stars
25
Forks
28
PR merge metrics
No merged PRs in 30d

Description

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](https://github.com/Azure/azure-api-style-guide)

Ive also attached the azure fucntions logs
[query_data.csv](https://github.com/Azure/APICenter-Analyzer/files/14865097/query_data.csv)
log

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.