|
| 1 | +# CAMARA Project - linting ruleset - documentation avaialable here: |
| 2 | +# https://github.com/camaraproject/Commonalities/blob/main/documentation/Linting-rules.md |
| 3 | +# 31.01.2024 - initial version |
| 4 | + |
| 5 | +extends: "spectral:oas" |
| 6 | +functions: |
| 7 | + - camara-reserved-words |
| 8 | + - camara-language-avoid-telco |
| 9 | + - camara-security-no-secrets-in-path-or-query-parameters |
| 10 | +functionsDir: "./lint_function" |
| 11 | +rules: |
| 12 | + # Built-in OpenAPI Specification ruleset. Each rule then can be enabled individually. |
| 13 | + # The severity keyword is optional in rule definition and can be error, warn, info, hint, or off. The default value is warn. |
| 14 | + contact-properties: false |
| 15 | + duplicated-entry-in-enum: true |
| 16 | + info-contact: true |
| 17 | + info-description: true |
| 18 | + info-license: true |
| 19 | + license-url: true |
| 20 | + no-$ref-siblings: error |
| 21 | + no-eval-in-markdown: true |
| 22 | + no-script-tags-in-markdown: true |
| 23 | + openapi-tags: false |
| 24 | + openapi-tags-alphabetical: false |
| 25 | + openapi-tags-uniqueness: error |
| 26 | + operation-description: true |
| 27 | + operation-operationId: true |
| 28 | + operation-operationId-unique: error |
| 29 | + operation-operationId-valid-in-url: true |
| 30 | + operation-parameters: true |
| 31 | + operation-singular-tag: true |
| 32 | + operation-success-response: true |
| 33 | + operation-tags: true |
| 34 | + operation-tag-defined: true |
| 35 | + path-declarations-must-exist: true |
| 36 | + path-keys-no-trailing-slash: true |
| 37 | + path-not-include-query: true |
| 38 | + path-params: error |
| 39 | + tag-description: false |
| 40 | + typed-enum: true |
| 41 | + oas3-api-servers: true |
| 42 | + oas3-examples-value-or-externalValue: true |
| 43 | + oas3-operation-security-defined: false |
| 44 | + oas3-parameter-description: false |
| 45 | + oas3-schema: true |
| 46 | + oas3-server-not-example.com: false |
| 47 | + oas3-server-trailing-slash: true |
| 48 | + oas3-unused-component: true |
| 49 | + oas3-valid-media-example: true |
| 50 | + oas3-valid-schema-example: true |
| 51 | + # oas3-server-variables: true |
| 52 | + |
| 53 | + # Custom Rules Utilizing Spectral's Built-in Functions and JavaScript Implementations |
| 54 | + |
| 55 | + camara-language-avoid-telco: |
| 56 | + message: "{{error}}" |
| 57 | + severity: hint |
| 58 | + description: | |
| 59 | + This rule checks for telco-specific terminology in your API definitions and suggests more inclusive terms. |
| 60 | + given: "$..*.*" |
| 61 | + then: |
| 62 | + function: camara-language-avoid-telco |
| 63 | + recommended: false # Set to true/false to enable/disable this rule |
| 64 | + |
| 65 | + camara-oas-version: |
| 66 | + message: "OpenAPI Version Error: The OpenAPI specification must adhere to version 3.0.3." |
| 67 | + severity: error |
| 68 | + description: | |
| 69 | + This rule validates the OpenAPI version in your specification and requires compliance with version 3.0.3. |
| 70 | + given: "$" |
| 71 | + then: |
| 72 | + field: openapi |
| 73 | + function: pattern |
| 74 | + functionOptions: |
| 75 | + match: 3.0.3 |
| 76 | + recommended: true # Set to true/false to enable/disable this rule |
| 77 | + |
| 78 | + camara-path-param-id: |
| 79 | + message: "Path Parameter Naming Warning: Use 'resource_id' instead of just 'id' in path parameters." |
| 80 | + severity: warn |
| 81 | + description: | |
| 82 | + This rule ensures consistent and descriptive naming for path parameters in your OpenAPI specification. |
| 83 | + Please use 'resource_id' instead of just 'id' for your path parameters. |
| 84 | + given: "$..parameters[?(@.in == 'path')]" |
| 85 | + then: |
| 86 | + field: name |
| 87 | + function: pattern |
| 88 | + functionOptions: |
| 89 | + notMatch: \b(id|Id|ID|iD)\b |
| 90 | + recommended: true # Set to true/false to enable/disable this rule |
| 91 | + |
| 92 | + camara-security-no-secrets-in-path-or-query-parameters: |
| 93 | + message: "Sensitive data found in path: {{error}} Consider avoiding the use of Sesentive data " |
| 94 | + severity: warn |
| 95 | + description: | |
| 96 | + This rule checks for sensitive data ('MSISDN' and 'IMSI') in API paths and suggests avoiding their use. |
| 97 | + given: |
| 98 | + - "$.paths" |
| 99 | + then: |
| 100 | + function: camara-security-no-secrets-in-path-or-query-parameters |
| 101 | + recommended: true # Set to true/false to enable/disable this rule |
| 102 | + |
| 103 | + camara-http-methods: |
| 104 | + description: "Ensure that all path URLs have valid HTTP methods (GET, PUT, POST, DELETE, PATCH, OPTIONS)." |
| 105 | + message: "Invalid HTTP method for '{{path}}'. Must be one of get, put, post, delete, patch, options." |
| 106 | + severity: error |
| 107 | + given: $.paths[*][*]~ |
| 108 | + then: |
| 109 | + function: pattern |
| 110 | + functionOptions: |
| 111 | + match: "^(get|put|post|delete|patch|options)$" |
| 112 | + recommended: true # Set to true/false to enable/disable this rule |
| 113 | + |
| 114 | + camara-get-no-request-body: |
| 115 | + message: There must be no request body for Get and DELETE |
| 116 | + severity: error |
| 117 | + given: |
| 118 | + - "$.paths.*.get" |
| 119 | + - "$.paths.*.delete" |
| 120 | + then: |
| 121 | + field: requestBody |
| 122 | + function: falsy |
| 123 | + recommended: true # Set to true/false to enable/disable this rule |
| 124 | + |
| 125 | + camara-reserved-words: |
| 126 | + message: "Reserved words found {{error}} Consider avoiding the use of reserved word " |
| 127 | + severity: warn |
| 128 | + description: | |
| 129 | + This rule checks Reserved words must not be used in the following parts of an API specification [Paths, Request Body properties, Component, Operation Id, Security Schema] |
| 130 | + given: |
| 131 | + - "$.paths" # Paths |
| 132 | + - "$..parameters[*]" # Path or Query Parameter Names: |
| 133 | + - "$..components.schemas.*.properties.*" # Request and Response body parameter |
| 134 | + - "$.paths.*." # Path and Operation Names: |
| 135 | + - "$.components.securitySchemes" # Security Schemes: |
| 136 | + - "$.components.*.*" # Component Names: |
| 137 | + - "$.paths.*.*.operationId" # OperationIds: |
| 138 | + then: |
| 139 | + function: camara-reserved-words |
| 140 | + recommended: true # Set to true/false to enable/disable this rule |
| 141 | + |
| 142 | + camara-routes-description: |
| 143 | + message: "Functionality method description Warning: Each method should have description." |
| 144 | + severity: warn |
| 145 | + description: | |
| 146 | + This rule checks if each operation (POST, GET, DELETE, PUT, PATCH, OPTIONS) in your API specification has a description. |
| 147 | + Ensure that you have added a 'summary' field for each operation in your OpenAPI specification. |
| 148 | + given: |
| 149 | + - "$.paths.*.post" |
| 150 | + - "$.paths.*.get" |
| 151 | + - "$.paths.*.delete" |
| 152 | + - "$.paths.*.put" |
| 153 | + - "$.paths.*.patch" |
| 154 | + - "$.paths.*.options" |
| 155 | + then: |
| 156 | + field: description |
| 157 | + function: truthy |
| 158 | + recommended: true # Set to true/false to enable/disable this rule |
| 159 | + |
| 160 | + camara-parameters-descriptions: |
| 161 | + message: "Parameter description is missing or empty: {{error}}" |
| 162 | + severity: warn |
| 163 | + description: | |
| 164 | + This Spectral rule ensures that each path parameter in the API specification has a descriptive and meaningful description. |
| 165 | + given: |
| 166 | + - "$.paths..parameters.*" |
| 167 | + then: |
| 168 | + field: description |
| 169 | + function: truthy |
| 170 | + recommended: true # Set to true/false to enable/disable this rule |
| 171 | + |
| 172 | + camara-response-descriptions: |
| 173 | + message: "Parameter description is missing or empty: {{error}}" |
| 174 | + severity: warn |
| 175 | + description: | |
| 176 | + This Spectral rule ensures that each responese object in the API specification has a descriptive and meaningful description. |
| 177 | + given: |
| 178 | + - "$.paths..responses.*" |
| 179 | + then: |
| 180 | + field: description |
| 181 | + function: truthy |
| 182 | + recommended: true # Set to true/false to enable/disable this rule |
| 183 | + |
| 184 | + camara-properties-descriptions: |
| 185 | + message: "Property description is missing or empty: {{error}}" |
| 186 | + severity: warn |
| 187 | + description: | |
| 188 | + This Spectral rule ensures that each propoerty within objects in the API specification has a descriptive and meaningful description. |
| 189 | + given: |
| 190 | + - "$.components.*.*" |
| 191 | + - "$.components.*.*.properties.*" |
| 192 | + then: |
| 193 | + field: description |
| 194 | + function: truthy |
| 195 | + recommended: true # Set to true/false to enable/disable this rule |
| 196 | + |
| 197 | + camara-operation-summary: |
| 198 | + message: "Operation Summary Warning: Each operation should include a short summary for better understanding." |
| 199 | + severity: warn |
| 200 | + description: | |
| 201 | + This rule checks if each operation (POST, GET, DELETE, PUT, PATCH, OPTIONS) in your API specification has a meaningful summary. |
| 202 | + Ensure that you have added a 'summary' field for each operation in your OpenAPI specification. |
| 203 | + given: |
| 204 | + - "$.paths.*.post" |
| 205 | + - "$.paths.*.get" |
| 206 | + - "$.paths.*.delete" |
| 207 | + - "$.paths.*.put" |
| 208 | + - "$.paths.*.patch" |
| 209 | + - "$.paths.*.options" |
| 210 | + then: |
| 211 | + field: summary |
| 212 | + function: truthy |
| 213 | + recommended: true # Set to true/false to enable/disable this rule |
| 214 | + |
| 215 | + camara-discriminator-use: |
| 216 | + description: | |
| 217 | + Ensure that API definition YAML files with oneOf or anyOf sections include a discriminator object for serialization, deserialization, and validation. |
| 218 | + severity: hint |
| 219 | + given: "$..[?(@.oneOf || @.anyOf)]" |
| 220 | + then: |
| 221 | + field: discriminator |
| 222 | + function: truthy |
| 223 | + description: "Discriminator object is required when using oneOf or anyOf." |
| 224 | + recommended: true # Set to true/false to enable/disable this rule |
| 225 | + |
| 226 | + camara-operationid-casing-convention: |
| 227 | + message: Operation Id must be in Camel case "{{error}}" |
| 228 | + severity: hint |
| 229 | + description: | |
| 230 | + This rule checks Operation ids should follow a specific case convention: camel case. |
| 231 | + given: "$.paths.*.*.operationId" |
| 232 | + then: |
| 233 | + function: casing |
| 234 | + functionOptions: |
| 235 | + type: camel |
| 236 | + recommended: true # Set to true/false to enable/disable this rule |
| 237 | + |
| 238 | + camara-schema-casing-convention: |
| 239 | + description: This rule checks schema should follow a specific case convention pascal case. |
| 240 | + message: "{{property}} should be pascal case (UppperCamelCase)" |
| 241 | + severity: warn |
| 242 | + given: $.components.schemas[*]~ |
| 243 | + then: |
| 244 | + function: casing |
| 245 | + functionOptions: |
| 246 | + type: pascal |
| 247 | + recommended: true # Set to true/false to enable/disable this rule |
| 248 | + |
| 249 | + camara-parameter-casing-convention: |
| 250 | + description: Paths should be kebab-case. |
| 251 | + severity: error |
| 252 | + message: "{{property}} is not kebab-case: {{error}}" |
| 253 | + given: $.paths[*]~ |
| 254 | + then: |
| 255 | + function: pattern |
| 256 | + functionOptions: |
| 257 | + match: "^\/([a-z0-9]+(-[a-z0-9]+)*)?(\/[a-z0-9]+(-[a-z0-9]+)*|\/{.+})*$" # doesn't allow /asasd{asdas}sadas pattern or not closed braces |
| 258 | + recommended: true # Set to true/false to enable/disable this rule |
0 commit comments