Skip to content

DocumentValidator

Header: swaggercpp/validator.hpp

Runs semantic checks on a Document that are independent of how the document was obtained. Parsing only catches syntactic issues — the validator catches the rest.

API

cpp
class DocumentValidator {
public:
    [[nodiscard]] ValidationReport validate(const Document&) const;
};

struct ValidationReport {
    std::vector<Issue> issues;
    [[nodiscard]] bool ok() const noexcept;   // true iff no errors
};

What it checks

RuleSeverity
info.title present and non-emptyerror
info.version present and non-emptyerror
openapi or swagger declarederror
Duplicate operation_id across operationserror
Response codes match \d{3} or equal defaulterror
Parameter name present and non-emptyerror
Parameter in + name uniqueness per operationerror
In-document $ref targets existerror
description missing on responseswarning
Operations without operation_idwarning
Unused component schemaswarning

INFO

This list is the current baseline. As the library grows, more checks will be added. See the Roadmap.

Usage

cpp
swaggercpp::DocumentValidator validator;
const auto report = validator.validate(doc);

if (!report.ok()) {
    for (const auto& issue : report.issues) {
        if (issue.severity == swaggercpp::IssueSeverity::error) {
            std::cerr << "error: " << issue.path << ": " << issue.message << '\n';
        }
    }
    return 2;
}

// Print warnings even on success
for (const auto& issue : report.issues) {
    if (issue.severity == swaggercpp::IssueSeverity::warning) {
        std::clog << "warn:  " << issue.path << ": " << issue.message << '\n';
    }
}

Combining with the reader

The typical pipeline is read → validate:

cpp
auto r = swaggercpp::DocumentReader::read_file("spec.yaml");
if (!r) return 1;

swaggercpp::DocumentValidator v;
auto report = v.validate(r.value());
if (!report.ok()) return 2;

TIP

The reader already reports syntax errors through Result::issues(). Validation is a second pass that assumes syntactic validity. You can skip it for trusted input, but it's cheap enough to run in most pipelines.

See also

Released under the MIT License.