You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`input`: The current component from the OpenAPI spec. Derived from the given and field values in the rule definition.
99
-
-`path`: JSONPath array to the current component.
100
-
-`documentInventory`: The entire OpenAPI specification (use `resolved` or `unresolved` depending on rule context).
100
+
-`options`: Optional input options passed from the rule definition.
101
+
-`context`: Additional input context such as the OAS being evaluated, the following properties are commonly used:
102
+
-`context.path`: JSONPath array to the current component.
103
+
-`context.documentInventory`: The entire OpenAPI specification (use `resolved` or `unresolved` depending on rule context).
104
+
105
+
To learn more about Spectral custom functions, see the [Spectral Documentation](https://docs.stoplight.io/docs/spectral/a781e290eb9f9-custom-functions).
101
106
102
107
---
103
108
@@ -144,11 +149,14 @@ As a rule developer, you need to define:
144
149
---
145
150
#### Helper Functions
146
151
147
-
Use the following helper functions from the `collectionUtils` module:
152
+
Use the following helper function from the `collectionUtils` module:
Passing the validation errors, the name of the rule, the object (which may contain an exception), and the path to the object, the helper function will collect adoptions, violations and exceptions accordingly. If the object adopts the rule (i.e. there are no errors passed) but the object has an exception. An error message will be returned informing the developer to remove the unnecessary exception.
157
+
158
+
The helper [`evaluateAndCollectAdoptionStatusWithoutExceptions(errors, ruleName, jsonPath)`](https://github.com/mongodb/openapi/blob/cd4e085a68cb3bb6078e85dba85ad8ce1674f7da/tools/spectral/ipa/rulesets/functions/utils/collectionUtils.js#L32) is used for reporting rule adoptions and violations (exceptions will be ignored - used for rules that do not allow exceptions).
148
159
149
-
-[`collectAndReturnViolation(jsonPath, ruleName, errorData)`](https://github.com/mongodb/openapi/blob/cd4e085a68cb3bb6078e85dba85ad8ce1674f7da/tools/spectral/ipa/rulesets/functions/utils/collectionUtils.js#L14) — for reporting rule violations.
150
-
-[`collectAdoption(jsonPath, ruleName)`](https://github.com/mongodb/openapi/blob/cd4e085a68cb3bb6078e85dba85ad8ce1674f7da/tools/spectral/ipa/rulesets/functions/utils/collectionUtils.js#L32) — for marking rule adoption.
151
-
-[`collectException(object, ruleName, jsonPath)`](https://github.com/mongodb/openapi/blob/cd4e085a68cb3bb6078e85dba85ad8ce1674f7da/tools/spectral/ipa/rulesets/functions/utils/collectionUtils.js#L32) — for recording rule exceptions.
152
160
---
153
161
154
162
#### How to Decide the component level at which the rule will be processed
@@ -183,7 +191,7 @@ When designing a rule, it is important to decide at which component level the ru
183
191
}
184
192
}
185
193
```
186
-
3. In the rule implementation, use the `collectException(object, ruleName, jsonPath)` helper function to collect exceptions. The object here is what you get when you traverse the path defined by the `jsonPath`.
194
+
3. In the rule implementation, use the `evaluateAndCollectAdoptionStatus(errors, ruleName, object, jsonPath)` helper function to collect adoptions, violations and exceptions. The `object` here is what you get when you traverse the path defined by the `jsonPath`. In this example, `jsonPath` would be `['components', 'schemas', 'SchemaName]`.
187
195
188
196
Exceptions can be defined at different levels, such as:
189
197
- Resource level
@@ -274,14 +282,13 @@ A rule must collect **only one** of the following for each evaluation:
274
282
275
283
You can include **multiple error messages** for a violation. To do so:
276
284
- Gather the messages into an array
277
-
- Pass them to `collectAndReturnViolation`
285
+
- Pass them to `evaluateAndCollectAdoptionStatus`
278
286
279
287
###### Considerations
280
288
281
289
- Use the **same `jsonPath`** for:
282
-
- `collectAndReturnViolation`
283
-
- `collectAdoption`
284
-
- `collectException`
290
+
- The `path` property in the errors/violations collected
291
+
- The `path` property passed to `evaluateAndCollectAdoptionStatus`
285
292
286
293
> 💡 This path should either be the `path` parameter from the rule function or a derived value from it.
287
294
@@ -294,39 +301,39 @@ You can include **multiple error messages** for a violation. To do so:
0 commit comments