Skip to content

DocumentValidator ​

DocumentValidator::builtin_rule_ids() returns the supported built-in rule identifiers. The CLI rejects unknown IDs so configuration typos do not silently disable intended checks.

Header: swaggercpp/validator.hpp

Validates the typed document after parsing. It checks structural consistency and selected schema keywords; it is not a complete OpenAPI metaschema validator or a request/response payload validator. Empty paths and unconstrained schemas are valid. OpenAPI 3.1 documents may contain only webhooks or reusable components.

API ​

cpp
struct ValidationRuleOptions {
    bool enabled {true};
    std::optional<IssueSeverity> severity;
};

struct ValidatorOptions {
    bool require_operation_id {false};
    bool require_operation_description {false};
    bool require_error_response {false};
    std::map<std::string, ValidationRuleOptions> rules;
};

using ValidationRule = std::function<std::vector<Issue>(const Document&)>;

class DocumentValidator {
public:
    explicit DocumentValidator(ValidatorOptions options = {});
    void add_rule(std::string rule_id, ValidationRule rule);
    [[nodiscard]] ValidationReport validate(const Document&) const;
};

Existing DocumentValidator validator; validator.validate(document); usage remains supported. ValidationReport::ok() is true when no issue has error severity. Validation does not mutate or resolve the document.

Built-in rules ​

Structural rules are enabled as errors by default:

Rule IDCheck
document.versionExactly one Swagger 2.0, OpenAPI 3.0.x, or OpenAPI 3.1.x declaration, consistent with Document::version
document.webhooks.versionWebhooks require OpenAPI 3.1
document.depthSchema and preserved JSON traversal stays within a nesting limit of 256
document.limitValidation stops after 100,000 schema visits, including repeated visits to shared nodes
info.title, info.versionNonempty API title and version
path.leading_slashPath keys start with /; webhook names are exempt
path.templatePath template expressions are well formed
path.parameterDeclared path parameters match template expressions for each operation
parameter.nameParameter names are nonempty
parameter.path.requiredPath parameters are required
parameter.schema_or_contentParameters define exactly one of schema or content; Swagger 2.0 parameters may define a top-level type
parameter.contentParameter content contains only one media type
parameter.uniqueName/location pairs are unique within each parameter list
operation.responsesEvery operation declares at least one response
operation.id.uniqueDeclared operation IDs are unique
response.statusResponse keys are 100–599 or default; OpenAPI additionally permits 1XX–5XX
response.descriptionNon-reference responses declare a description; explicitly empty strings are valid
security.schemeDocument and operation requirements refer to defined security schemes
reference.unresolvedFragment references resolve, including escaped/percent-encoded JSON pointers and schema anchors
schema.versionBoolean schemas, type arrays, and exclusive bounds match the declared specification version
schema.typeSchema types are supported and type-array values are unique
schema.numericNumeric bounds and multipleOf contain numeric values
schema.multiple_ofmultipleOf is positive
schema.required.uniqueRequired property names are unique

The validator traverses reusable schemas, parameters, responses, and request bodies as well as operations and webhooks. Recursive schema graphs terminate safely. Repeated expansion of a shared acyclic schema graph is bounded by a 100,000-visit limit; exceeding it emits one document.limit issue and stops further schema traversal and reference checking. The report is incomplete in this case. Path-level parameters may be overridden by operation-level parameters. Internal component parameter references participate in duplicate and template checks. External references are left to the reference resolver; unresolved external parameter definitions do not produce speculative missing-template-parameter errors.

Optional lint rules default to warnings and are disabled unless enabled through their convenience option or the rule map:

Rule IDConvenience optionCheck
operation.id.requiredrequire_operation_idEvery operation has a nonempty operation ID
operation.description.requiredrequire_operation_descriptionEvery operation has a description
operation.error_response.requiredrequire_error_responseEvery operation documents a 4xx, 5xx, or default response

Configure rules ​

cpp
swaggercpp::ValidatorOptions options;
options.require_operation_id = true;
options.require_error_response = true;
options.rules["operation.id.required"].severity = swaggercpp::IssueSeverity::error;
options.rules["response.description"].enabled = false;

swaggercpp::DocumentValidator validator(options);
const auto report = validator.validate(document);

A rule-map entry overrides the convenience option. Creating an entry with enabled = true also enables an optional lint rule. Unknown rule IDs have no effect unless registered as custom rules.

Custom rules ​

cpp
validator.add_rule("team.contact", [](const swaggercpp::Document& document) {
    std::vector<swaggercpp::Issue> issues;
    if (!document.info.contact) {
        issues.push_back({
            .severity = swaggercpp::IssueSeverity::warning,
            .path = "/info/contact",
            .message = "Provide a support contact"
        });
    }
    return issues;
});

The registered rule ID is attached to each returned issue. Its configuration can disable the callback entirely or override the severity of all its issues. Empty IDs, empty callbacks, and duplicate custom rule IDs throw std::invalid_argument. Callback exceptions propagate to the caller. Use a project-specific prefix for custom IDs to avoid collisions with built-in IDs.

Diagnostics and source locations ​

Every built-in issue includes a stable rule_id and an escaped JSON Pointer path, such as /paths/~1items/get/responses/200/description. Issues are sorted by path, rule ID, message, and severity so command-line and CI output is deterministic.

Read with ReaderOptions::track_source_locations = true to retain source coordinates. The validator adds Document::source_file and the recorded location of the issue's node, or its nearest existing ancestor for a missing field. It leaves line and column unset when the reader did not record them. Coordinates provided by custom rules are preserved; validation never guesses missing coordinates.

cpp
swaggercpp::ReaderOptions reader_options;
reader_options.track_source_locations = true;
const auto result = swaggercpp::DocumentReader::read_file("spec.yaml", reader_options);
if (!result) return 1;

const auto report = swaggercpp::DocumentValidator().validate(result.value());
for (const auto& issue : report.issues) {
    std::cerr << issue.rule_id << ": " << issue.path << ": " << issue.message << '\n';
}
if (!report.ok()) return 2;

Reference scope and limits ​

Fragment references are checked against the current document or the enclosing schema resource created by $id. Schema $anchor names are recognized. Unknown fields and bundled reference targets remain available for JSON Pointer lookup. Examples, defaults, and enum/constant payloads are treated as data, not reference declarations. This pass does not fetch external documents, validate OAuth scope catalogs, or exhaustively validate every keyword retained in raw JSON fields.

See also ​

Released under the MIT License.