api-quality-spectral-ruleset 1.3.0-beta.2 → 1.3.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/apq-spectral.yaml CHANGED
@@ -16,6 +16,7 @@ functions:
16
16
  - apq-custom-field
17
17
  - apq-check-examples-coverage
18
18
  - apq-path-depth
19
+ - apq-forbidden-characters
19
20
  - apq-path-param-query-conflict
20
21
  - apq-security-required-response
21
22
  - apq-check-ambiguous-path
@@ -65,9 +66,7 @@ rules:
65
66
  severity: "error"
66
67
  given: "$.x-wso2-security.apim.x-wso2-scopes[*].roles"
67
68
  then:
68
- function: "pattern"
69
- functionOptions:
70
- match: "^[A-Za-z0-9_]+$"
69
+ function: apq-forbidden-characters
71
70
  apiq:OAR005:
72
71
  description: "A wrong scope may cause problems to import the API definition into WSO2 or allow all users to call the endpoint."
73
72
  message: "OAR005: Scope in the operation must be defined correctly and match an existing scope."
@@ -106,9 +105,9 @@ rules:
106
105
  message: "OAR008: Only the REST standard verbs are allowed (POST, GET, PUT, PATCH and DELETE)."
107
106
  documentationUrl: "https://github.com/apiaddicts/apquality-spectral/blob/main/docs/resources/OAR008.md"
108
107
  severity: error
109
- given: "$.paths[*][?(@property == 'get' || @property == 'post' || @property == 'put' || @property == 'delete' || @property == 'patch')]"
108
+ given: "$.paths[*][head,options,trace]"
110
109
  then:
111
- function: truthy
110
+ function: falsy
112
111
  apiq:OAR009:
113
112
  description: "Default request media type should be defined for operations."
114
113
  message: "OAR009: Default request media type is mandatory."
@@ -177,9 +176,11 @@ rules:
177
176
  severity: error
178
177
  given: "$.paths.*~"
179
178
  then:
180
- function: pattern
179
+ function: apq-path-depth
181
180
  functionOptions:
182
- match: "^\/[^\/]*((\/{[^}]*})*\/[^\/]*(\/{[^}]*})*){0,4}\/?$"
181
+ maxDepth: 5
182
+ ignoreSegments:
183
+ - me
183
184
  apiq:OAR016:
184
185
  description: "Numeric types requires a valid format."
185
186
  message: "OAR016: Numeric types must use a valid format for their type."
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @param {string|string[]} targetVal
3
+ * @param {object} options
4
+ * @param {import('@stoplight/spectral-core').RulesetFunctionContext} context
5
+ */
6
+ module.exports = (targetVal, _options, context) => {
7
+ const pattern = /^[a-zA-Z0-9_\-., ]+$/;
8
+ const errors = [];
9
+
10
+ if (typeof targetVal === 'string') {
11
+ if (!pattern.test(targetVal)) {
12
+ errors.push({ message: context.rule.message });
13
+ }
14
+ } else if (Array.isArray(targetVal)) {
15
+ targetVal.forEach((role, index) => {
16
+ if (typeof role === 'string' && !pattern.test(role)) {
17
+ errors.push({ message: context.rule.message, path: [index] });
18
+ }
19
+ });
20
+ }
21
+
22
+ return errors;
23
+ };
@@ -6,14 +6,15 @@
6
6
  module.exports = (targetVal, options, context) => {
7
7
  const maxDepth = options.maxDepth ?? 3
8
8
  const ignore = options.ignoreSegments ?? []
9
-
10
9
  const segments = targetVal
11
10
  .split('/')
12
11
  .filter(Boolean)
13
12
  .filter(segment => !segment.startsWith('{') && !segment.endsWith('}'))
14
13
  .filter(segment => !ignore.includes(segment))
15
14
 
16
- if (segments.length > maxDepth) {
15
+ const depth = segments.length;
16
+
17
+ if (depth > maxDepth) {
17
18
  return [
18
19
  { message: context.rule.message }
19
20
  ]
@@ -6,8 +6,14 @@
6
6
  module.exports = (given, options, context) => {
7
7
  if (given === null || given === undefined) return [];
8
8
 
9
- const VALID_IN_VALUES = ['query', 'header', 'path', 'cookie', 'body', 'formData'];
10
- const VALID_SCHEMA_TYPES = ['string', 'number', 'integer', 'boolean', 'array', 'object', 'null'];
9
+ const doc = context.document?.data ?? {};
10
+ const isOAP2 = typeof doc.swagger === 'string';
11
+
12
+ const VALID_IN_VALUES_OAP2 = ['query', 'header', 'path', 'formData', 'body'];
13
+ const VALID_IN_VALUES_OAP3 = ['path', 'query', 'header', 'cookie'];
14
+ const VALID_IN_VALUES = isOAP2 ? VALID_IN_VALUES_OAP2 : VALID_IN_VALUES_OAP3;
15
+
16
+ const VALID_SCHEMA_TYPES = ['object', 'string', 'number', 'integer', 'boolean', 'array', 'null'];
11
17
 
12
18
  const nodePath = context.path || [];
13
19
  const fieldName = nodePath[nodePath.length - 1];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-quality-spectral-ruleset",
3
- "version": "1.3.0-beta.2",
3
+ "version": "1.3.0-beta.4",
4
4
  "description": "Spectral ruleset by API Quality",
5
5
  "main": "apq-spectral.yaml",
6
6
  "files": [