Skip to main content

Adding Your Own Rules

note

This feature is currently only supported for OpenAPI v2.0 and v3.0 files and works only when the UseStrictValidation import setting is set to true.

If you are looking to add your own rules beyond the capabilities provided by our built-in rulesets, you can create a custom ruleset and apply that on your API specification document.

How Do Custom Rules Work?

In a custom ruleset, you can configure rules that use built-in methods to verify one or more of the selected target components. If the target component fails verification, a lint warning will be logged. The severity of these messages as well as other details can be easily configured to suit your needs when defining the rules.

Creating and Applying a Custom Ruleset File

Custom rules need to be defined inside a custom ruleset file placed at a path relative to the main API specification document. The file must use valid JSON/YAML syntax and contain the Ruleset Object at root level.

Example:

{
"Id": "my-custom-ruleset",
"Rules": [
{
"Id": "max-operation-id-length-30",
"VerificationMethod": "Length",
"Targets": [
{
"JsonPath": "$.paths.*.*.operationId"
}
],
"VerificationMethodArgs": {
"Maximum": 30
},
"Message": "Operation id is too long."
}
]
}

In order to apply a custom ruleset on your API specification document, it needs to be referenced from within a APIMatic Metadata file's validation configuration as shown below:

{
"ValidationConfiguration": {
"Rulesets": {
"my-custom-ruleset": {
"FilePath": "/path/to/ruleset/file"
}
}
}
}

Ruleset Object

This object represents the root object that is placed in a custom ruleset file. The available properties are listed below:

Property NameTypeDescription
IdStringRequired. Unique identifier of the ruleset. Use only alphanumeric characters or dashes.
RulesArray[Custom Lint Rule Object]Required. List of custom rule definitions that need to be applied on target components. The list must contain at least one rule definition.
NameStringA short user-friendly name of the ruleset.
DescriptionStringA detailed description of the ruleset.
SeveritySeverityDefault: Warning. Global declaration of severity of all rules. Can be overridden at rule level.
ExternalLinksArray[String]List of external links that may provide additional details about the ruleset.
TagsArray[String]Tags that will be shown along with rules of the ruleset for more context and to facilitate searching.

Custom Lint Rule Object

The available properties to describe a custom rule are listed below:

Property NameTypeDescription
IdStringRequired. Unique identifier of the rule across the ruleset. Use only alphanumeric characters or dashes.
TargetsArray[Rule Target Object]Required. Identifies the target components that need to be verified with the current rule. The list must contain at least one target.
VerificationMethodVerification MethodRequired. Name of the pre-defined method that needs to be applied on the selected object as part of the rule verification.
VerificationMethodArgsMap[String, Any]Key-value pairs containing arguments to pass to the verification method where the key represents the argument name. Depending upon the selected VerificationMethod, one or more arguments may be required.
MessageStringThe message that needs to be displayed during the validation process if a violation of the rule is found. If not specified, a default message will be used. It is recommended to keep this message short.
SeveritySeverityBy default, the ruleset level global severity will be applicable (Warning by default). The global severity can be overridden with the severity specified here.
NameStringShort user-friendly name for the rule.
DescriptionStringDescribes the rule in detail e.g. it can specify what kind of verification is expected and on what types of target components.
ExternalLinksArray[String]External links that can provide more details about the current rule.
HintsArray[String]Any tips/suggestions that can help someone easily resolve the violations of the current rule.
TagsArray[String]Tags that can be shown with the rule for more context and to help in searching as well.

Rule Target Object

The available properties to help select rule target component(s) are listed below:

Property NameTypeDescription
JsonPathStringRequired. A valid JSONPath expression that helps locate and select the component(s) on which the rule needs to be applied.
KeysOnlyBooleanThe target objects will be converted into a list of keys (extracted from the top level of an object). Only applicable if selected target(s) are objects.
SelectWithKeysArray[String]For each target object, only properties with specified keys will be evaluated. Mutually exclusive with IgnoreWithKeys. Only applicable if selected target(s) are objects.
IgnoreWithKeysArray[String]For each target object, properties with specified keys will not be evaluated. Mutually exclusive with SelectWithKeys. Only applicable if selected target(s) are objects.

Verification Methods

Available verification methods are listed below:

Method NameDescription
PatternMore details can be found here.
OrderMore details can be found here.
LengthMore details can be found here.