Skip to main content

Validation

gswag can validate the generated spec for structural correctness and JSON Schema compliance against the OpenAPI 3.0 meta-schema.

Validate the in-memory spec

issues := gswag.ValidateSpec()
for _, issue := range issues {
fmt.Println(issue.String()) // includes severity and message
}

ValidationIssue carries a severity — "error" or "warning". Errors indicate spec invalidity; warnings indicate style or completeness issues.

Validate a spec file on disk

issues, err := gswag.ValidateSpecFile("./docs/openapi.yaml")
if err != nil {
log.Fatal(err)
}
for _, issue := range issues {
fmt.Println(issue.String())
}

Write and validate in one step

if err := gswag.WriteAndValidateSpec(); err != nil {
// err wraps ErrSpecInvalid when any error-level issue exists
log.Fatal(err)
}

Use this in AfterSuite instead of WriteSpec when you want the test suite to fail on an invalid spec:

var _ = AfterSuite(func() {
testServer.Close()
Expect(gswag.WriteAndValidateSpec()).To(Succeed())
})

Validation checks

Errors (block spec output)

CheckDescription
info.title presentRequired by the OpenAPI spec
info.version presentRequired by the OpenAPI spec
Security scheme references resolveEvery BearerAuth() / Security("name") call must match a scheme declared in Config.SecuritySchemes
JSON Schema validityThe serialised spec is validated against the OpenAPI 3.0 JSON meta-schema

Warnings (reported, not blocking)

CheckDescription
Empty pathsA paths object with no operations
Operations missing summaryOperations should have a human-readable summary
Operations missing tagsUntagged operations may not group correctly in UI tools

Runtime response validation

Enable response validation during test execution to assert that real responses conform to the declared schema:

Init(&Config{
Title: "My API",
Version: "1.0.0",
EnforceResponseValidation: true,
ValidationMode: "warn", // "fail" (default) or "warn"
})

ValidationMode: "fail" causes a test failure when a response does not match its declared schema. "warn" logs the mismatch without failing.