Documentation
¶
Overview ¶
Package validate provides OpenAPI specification validation functionality. It validates OpenAPI specs against the official JSON Schema definitions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrPathEmpty = errors.New("path cannot be empty") ErrPathNoLeadingSlash = errors.New("path must start with '/'") ErrPathDuplicateParameter = errors.New("duplicate path parameter") ErrPathInvalidParameter = errors.New("invalid path parameter format") )
Path validation errors
Functions ¶
func ValidateComponentName ¶ added in v0.4.0
ValidateComponentName validates an OpenAPI component name. Valid names must match: ^[a-zA-Z0-9.\-_]+$ Returns an error if the name is invalid.
func ValidatePath ¶ added in v0.4.0
ValidatePath validates a path pattern for use in OpenAPI operations.
It accepts both router-style (:param) and OpenAPI-style ({param}) syntax for developer convenience. The internal builder will normalize these to OpenAPI format.
Validation checks:
- Non-empty path
- Path starts with '/'
- Valid path parameter syntax (:param or {param})
- No duplicate path parameters
- Parameter names match [a-zA-Z0-9._-]+
- Properly paired braces in {param} syntax
Returns an error if validation fails.
func ValidateResponseCode ¶ added in v0.4.0
ValidateResponseCode validates an OpenAPI response code. Valid codes: "default", "200", "404", "4XX", "5XX", etc. Returns an error if the code is invalid.
Types ¶
type Validator ¶ added in v0.4.0
type Validator struct {
// contains filtered or unexported fields
}
Validator validates OpenAPI specifications against their meta-schemas. Thread-safe. Compiles schemas once and caches them for reuse.
func New ¶
func New() *Validator
New creates a new Validator with embedded OpenAPI meta-schemas.
The validator uses santhosh-tekuri/jsonschema which supports both JSON Schema draft-04 (for OpenAPI 3.0) and draft-2020-12 (for OpenAPI 3.1).
func (*Validator) Validate ¶ added in v0.4.0
Validate validates an OpenAPI specification JSON against the meta-schema for the given version.
The schema is compiled on first use and cached for subsequent validations. This method is thread-safe.
Example:
validator := validate.New()
if err := validator.Validate(ctx, specJSON, validate.V31); err != nil {
log.Fatalf("Invalid OpenAPI spec: %v", err)
}