Skip to main content

Enforce Required Properties in Your OpenAPI Definition Using Custom Validation Rules

· One min read

Until now, our custom validation rules supported Pattern, Order, and Length verification methods. This allowed us to enforce specific patterns for property values, restrict property lengths, and validate alphabetical ordering within OpenAPI definitions.

We're excited to introduce a new verification method: "Required."

With this update, you can now mark a property as required - even if OpenAPI standards don't enforce it. This means greater flexibility in defining custom validation rules, including support for vendor extensions.

Example: Marking a Property as Required

Here's how you can enforce the presence of a vendor extension, x-my-extension, inside the info object:

{
"Id": "new-ruleset",
"Rules": [
{
"Id": "100-required-x-extensions",
"Severity": "Blocking",
"Targets": [
{
"JsonPath": "$.info"
}
],
"VerificationMethod": "Required",
"VerificationMethodArgs": {
"Properties": ["x-my-extension"]
},
"Message": "API info must contain required fields."
}
]
}

In this ruleset, the x-my-extension property under VerificationMethodArgs is required at the root info object level. Similarly, you can apply this rule to any property in any OpenAPI object.

alt text

note

Visit our documentation if you need help in writing your first custom validation rule.