Help us shape the future of Go! Take the 2026 Go Developer Survey

spec

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(value any, paramType string) error

Types

type Body

type Body struct {
	// MapSchema is the JSON schema for the request body in map format.
	MapSchema map[string]any
	// JsonSchema is the JSON schema for the request body in as a parsed jsonschema validator.
	JsonSchema *jsonschema.Schema
	// Yaml is the YAML representation of the request body schema.
	Yaml string
	// Json is the JSON representation of the request body schema.
	Json string
}

Body represents the request body of an API endpoint.

func (*Body) Validate

func (b *Body) Validate(value any) error

type Endpoint

type Endpoint struct {
	// Name is the name of the endpoint.
	Name string
	// Summary is a brief summary of the endpoint.
	Summary string
	// Description is a detailed description of the endpoint.
	Description string
	// Method is the HTTP method of the endpoint.
	Method string
	// Path is the URL path of the endpoint.
	Path string

	// Params is a map of parameters that the endpoint accepts. The key is the name
	// of the parameter, and the value is the parameter itself.
	Params map[string]*Param
	// Body is the request body of the endpoint, if any.
	Body *Body
	// contains filtered or unexported fields
}

Endpoint represents an API endpoint.

func MakeEndpoint

func MakeEndpoint(method, path string, op *v3.Operation, item *v3.PathItem) *Endpoint

MakeEndpoint creates an endpoint from an OpenAPI operation.

func (*Endpoint) BodySchema

func (e *Endpoint) BodySchema(prefixes ...string) string

BodySchema returns the JSON schema for the endpoint's request body.

func (*Endpoint) HasRequestBody

func (e *Endpoint) HasRequestBody() bool

HasRequestBody returns true if the endpoint has a request body.

func (*Endpoint) MethodName

func (e *Endpoint) MethodName() string

MethodName returns the CamelCase method name for the endpoint.

This converts _, -, and camelCase/PascalCase boundaries into words that are joined as CamelCase. For example, "accounts_verify-email" and "accountsVerifyEmail" both become "AccountsVerifyEmail".

func (*Endpoint) MustMakeUrl

func (e *Endpoint) MustMakeUrl(params map[string][]any) string

func (*Endpoint) ParamNames

func (e *Endpoint) ParamNames() []string

func (*Endpoint) String

func (e *Endpoint) String() string

func (*Endpoint) Validate

func (e *Endpoint) Validate(params map[string][]any, headers map[string][]string, body any) error

Validate checks the supplied params, headers, and body against the endpoint's declared schema.

Spec-declared header parameters MUST be supplied via headers (i.e. request.Header(...)), not via params (request.Param(...)). Passing a spec-declared header parameter through params returns an error pointing the caller at the right helper. Headers that are not declared in the spec are passed through untouched (e.g. ad-hoc Authorization, Content-Type).

type OpenApiSpec

type OpenApiSpec struct {
	// Source is the OpenAPI specification schema. It can be raw JSON or YAML, a
	// file:// URL, or an HTTP URL
	Source string
	// RawSchema is the raw bytes of the OpenAPI schema
	RawSchema []byte

	Endpoints map[string]*Endpoint
	// contains filtered or unexported fields
}

func (*OpenApiSpec) GetOperationName

func (o *OpenApiSpec) GetOperationName(methodName string) string

func (*OpenApiSpec) MethodNames

func (o *OpenApiSpec) MethodNames() []string

MethodNames the CamelCase method names for each endpoint.

func (*OpenApiSpec) New

func (o *OpenApiSpec) New() (err error)

New builds a new OpenApiSpec from the provided source. OpenApiSpec.Source must be set on the object before calling this method.

func (*OpenApiSpec) NewFromBytes

func (o *OpenApiSpec) NewFromBytes(source []byte) (*OpenApiSpec, error)

NewFromBytes builds a new OpenApiSpec from the provided schema encoded as bytes. This method does not provide caching.

func (*OpenApiSpec) NewFromSource

func (o *OpenApiSpec) NewFromSource(source string) (*OpenApiSpec, error)

NewFromSource builds a new OpenApiSpec from the provided source. The source can be a file:// URL or an HTTP URL or raw bytes. This method will cache the resulting OpenApiSpec, and return the same instance for the exact same source.

func (*OpenApiSpec) OperationNames

func (o *OpenApiSpec) OperationNames() []string

OperationNames snake_case operation names for each endpoint.

func (*OpenApiSpec) ReadSource

func (o *OpenApiSpec) ReadSource() ([]byte, error)

ReadSource reads the source file specified by the OpenAPI struct's Source field. It handles file:// URLs and will eventually support other protocols.

If the Source field does not contain a protocol (e.g., "file://"), it is assumed to be a local file path.

If the Source field contains a protocol but it's not "file://", an error is returned.

The function returns the contents of the file as a byte slice and any encountered errors.

Example:

openapi := &OpenAPI{Source: "file:///path/to/source.yaml"}
data, err := openapi.ReadSource()
if err != nil {
    log.Fatal(err)
}

type Param

type Param struct {
	// Name is the name of the parameter.
	Name string
	// In is the location of the parameter. It can be "query", "path", "header",
	// or "cookie".
	In string
	// Type is the data type of the parameter.
	Type string
	// Description is a brief description of the parameter.
	Description string
	// Required is a boolean indicating whether the parameter is required.
	Required bool
}

Param represents a parameter of an API endpoint.

func (*Param) Coerce

func (p *Param) Coerce(value any) any

func (*Param) Validate

func (p *Param) Validate(value any) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL