Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ValidationError ¶
type ValidationError struct {
// Path is the JSON path to the problematic field (e.g., "paths./pets.get.responses")
Path string
// Message is a human-readable error message
Message string
// SpecRef is the URL to the relevant section of the OAS specification
SpecRef string
// Severity indicates whether this is an error or warning
Severity Severity
// Field is the specific field name that has the issue
Field string
// Value is the problematic value (optional)
Value interface{}
}
ValidationError represents a single validation issue
func (ValidationError) String ¶
func (e ValidationError) String() string
String returns a formatted string representation of the validation error
Example ¶
ExampleValidationError_String demonstrates formatting of validation errors
package main
import (
"fmt"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
err := validator.ValidationError{
Path: "paths./pets.get.responses",
Message: "Missing required field 'responses'",
SpecRef: "https://spec.openapis.org/oas/v3.0.0.html#operation-object",
Severity: validator.SeverityError,
Field: "responses",
}
fmt.Println(err.String())
// Output will show formatted error with path, message, and spec reference
}
type ValidationResult ¶
type ValidationResult struct {
// Valid is true if no errors were found (warnings are allowed)
Valid bool
// Version is the detected OAS version string
Version string
// OASVersion is the enumerated OAS version
OASVersion parser.OASVersion
// Errors contains all validation errors
Errors []ValidationError
// Warnings contains all validation warnings
Warnings []ValidationError
// ErrorCount is the total number of errors
ErrorCount int
// WarningCount is the total number of warnings
WarningCount int
}
ValidationResult contains the results of validating an OpenAPI specification
Example ¶
ExampleValidationResult demonstrates working with validation results
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
v := validator.New()
testFile := filepath.Join("testdata", "invalid-oas3.yaml")
result, err := v.Validate(testFile)
if err != nil {
log.Fatalf("Validation failed: %v", err)
}
// Check if document is valid
if !result.Valid {
fmt.Printf("Validation failed with %d error(s):\n", result.ErrorCount)
for i, validationErr := range result.Errors {
fmt.Printf("%d. %s: %s\n", i+1, validationErr.Path, validationErr.Message)
if validationErr.SpecRef != "" {
fmt.Printf(" See: %s\n", validationErr.SpecRef)
}
}
}
// Show warnings if any
if result.WarningCount > 0 {
fmt.Printf("\nWarnings (%d):\n", result.WarningCount)
for i, warning := range result.Warnings {
fmt.Printf("%d. %s: %s\n", i+1, warning.Path, warning.Message)
}
}
}
type Validator ¶
type Validator struct {
// IncludeWarnings determines whether to include best practice warnings
IncludeWarnings bool
// StrictMode enables stricter validation beyond the spec requirements
StrictMode bool
// contains filtered or unexported fields
}
Validator handles OpenAPI specification validation
func New ¶
func New() *Validator
New creates a new Validator instance with default settings
Example ¶
ExampleNew demonstrates creating a new validator with default settings
package main
import (
"fmt"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
// Create a validator with default settings
v := validator.New()
// Default settings
fmt.Printf("Include warnings: %v\n", v.IncludeWarnings)
fmt.Printf("Strict mode: %v\n", v.StrictMode)
}
Output: Include warnings: true Strict mode: false
func (*Validator) Validate ¶
func (v *Validator) Validate(specPath string) (*ValidationResult, error)
Validate validates an OpenAPI specification file
Example ¶
ExampleValidator_Validate demonstrates basic validation of an OpenAPI specification
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
// Create a new validator
v := validator.New()
// Validate a specification file
testFile := filepath.Join("testdata", "petstore-3.0.yaml")
result, err := v.Validate(testFile)
if err != nil {
log.Fatalf("Validation failed: %v", err)
}
// Check the results
fmt.Printf("Valid: %v\n", result.Valid)
fmt.Printf("Version: %s\n", result.Version)
fmt.Printf("Errors: %d\n", result.ErrorCount)
fmt.Printf("Warnings: %d\n", result.WarningCount)
}
Example (MultipleVersions) ¶
ExampleValidator_Validate_multipleVersions demonstrates validating different OAS versions
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
v := validator.New()
files := []string{
"petstore-2.0.yaml",
"petstore-3.0.yaml",
"petstore-3.1.yaml",
"petstore-3.2.yaml",
}
for _, file := range files {
testFile := filepath.Join("testdata", file)
result, err := v.Validate(testFile)
if err != nil {
log.Printf("Error validating %s: %v", file, err)
continue
}
fmt.Printf("%s: Valid=%v, Version=%s, Errors=%d, Warnings=%d\n",
file, result.Valid, result.Version, result.ErrorCount, result.WarningCount)
}
}
Example (NoWarnings) ¶
ExampleValidator_Validate_noWarnings demonstrates validation with warnings suppressed
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
// Create a validator that suppresses warnings
v := validator.New()
v.IncludeWarnings = false
testFile := filepath.Join("testdata", "petstore-3.0.yaml")
result, err := v.Validate(testFile)
if err != nil {
log.Fatalf("Validation failed: %v", err)
}
// Warnings will not be included in the result
fmt.Printf("Valid: %v\n", result.Valid)
fmt.Printf("Warnings count: %d\n", result.WarningCount)
}
Example (Oas2) ¶
ExampleValidator_Validate_oas2 demonstrates validating an OAS 2.0 specification
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
v := validator.New()
testFile := filepath.Join("testdata", "petstore-2.0.yaml")
result, err := v.Validate(testFile)
if err != nil {
log.Fatalf("Validation failed: %v", err)
}
fmt.Printf("Valid OAS 2.0 document: %v\n", result.Valid)
fmt.Printf("Version: %s\n", result.Version)
}
Example (StrictMode) ¶
ExampleValidator_Validate_strictMode demonstrates validation with strict mode enabled
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/erraggy/oastools/internal/validator"
)
func main() {
// Create a validator with strict mode
v := validator.New()
v.StrictMode = true
testFile := filepath.Join("testdata", "petstore-3.0.yaml")
result, err := v.Validate(testFile)
if err != nil {
log.Fatalf("Validation failed: %v", err)
}
// Strict mode may produce additional warnings
fmt.Printf("Valid: %v\n", result.Valid)
fmt.Printf("Errors: %d\n", result.ErrorCount)
fmt.Printf("Warnings: %d\n", result.WarningCount)
}