config

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 4 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*ValidationOptions)

Option Enables an 'Options pattern' approach

func DisablePathTree added in v0.13.0

func DisablePathTree() Option

DisablePathTree prevents automatic radix tree construction. Use this to fall back to regex-based path matching only.

func WithContentAssertions added in v0.4.0

func WithContentAssertions() Option

WithContentAssertions enables checks for contentType, contentEncoding, etc

func WithCustomFormat added in v0.5.0

func WithCustomFormat(name string, validator func(v any) error) Option

WithCustomFormat adds custom formats and their validators that checks for custom 'format' assertions When you add different validators with the same name, they will be overridden, and only the last registration will take effect.

func WithExistingOpts added in v0.4.4

func WithExistingOpts(options *ValidationOptions) Option

WithExistingOpts returns an Option that will copy the values from the supplied ValidationOptions instance

func WithFormatAssertions added in v0.4.0

func WithFormatAssertions() Option

WithFormatAssertions enables checks for 'format' assertions (such as date, date-time, uuid, etc)

func WithLogger added in v0.10.0

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for validation debug/error output. If not set, logging is silent (nil logger is handled gracefully).

func WithOpenAPIMode added in v0.6.0

func WithOpenAPIMode() Option

WithOpenAPIMode enables OpenAPI-specific keyword validation (default: true)

func WithPathTree added in v0.13.0

func WithPathTree(pathTree radix.PathLookup) Option

WithPathTree sets a custom radix tree for path matching. The default is built automatically from the OpenAPI specification.

func WithRegexCache added in v0.9.0

func WithRegexCache(regexCache RegexCache) Option

WithRegexCache assigns a cache for compiled regular expressions. A sync.Map should be sufficient for most use cases. It does not implement any cleanup

func WithRegexEngine

func WithRegexEngine(engine jsonschema.RegexpEngine) Option

WithRegexEngine Assigns a custom regular-expression engine to be used during validation.

func WithScalarCoercion added in v0.6.0

func WithScalarCoercion() Option

WithScalarCoercion enables string to boolean/number coercion (Jackson-style)

func WithSchemaCache added in v0.8.0

func WithSchemaCache(schemaCache cache.SchemaCache) Option

WithSchemaCache sets a custom cache implementation or disables caching if nil. Pass nil to disable schema caching and skip cache warming during validator initialization. The default cache is a thread-safe sync.Map wrapper.

func WithStrictIgnorePaths added in v0.10.0

func WithStrictIgnorePaths(paths ...string) Option

WithStrictIgnorePaths sets JSONPath patterns for paths to exclude from strict validation. Patterns use glob syntax:

  • * matches a single path segment
  • ** matches any depth (zero or more segments)
  • [*] matches any array index
  • \* escapes a literal asterisk

Examples:

  • "$.body.metadata.*" - any property under metadata
  • "$.body.**.x-*" - any x-* property at any depth
  • "$.headers.X-*" - any header starting with X-

func WithStrictIgnoredHeaders added in v0.10.0

func WithStrictIgnoredHeaders(headers ...string) Option

WithStrictIgnoredHeaders replaces the default ignored headers list entirely. Use this to fully control which headers are ignored in strict mode. For the default list, see the strict package's DefaultIgnoredHeaders.

func WithStrictIgnoredHeadersExtra added in v0.10.0

func WithStrictIgnoredHeadersExtra(headers ...string) Option

WithStrictIgnoredHeadersExtra adds headers to the default ignored list. Unlike WithStrictIgnoredHeaders, this merges with the defaults rather than replacing them.

func WithStrictMode added in v0.10.0

func WithStrictMode() Option

WithStrictMode enables strict property validation. In strict mode, undeclared properties are reported as errors even when additionalProperties: true would normally allow them.

This is useful for API governance scenarios where you want to ensure clients only send properties that are explicitly documented in the OpenAPI specification.

func WithURLEncodedBodyValidation added in v0.12.0

func WithURLEncodedBodyValidation() Option

WithURLEncodedBodyValidation enables converting an URL Encoded body to a JSON when validating the schema from a request and response body The default option is set to false

func WithXmlBodyValidation added in v0.12.0

func WithXmlBodyValidation() Option

WithXmlBodyValidation enables converting an XML body to a JSON when validating the schema from a request and response body The default option is set to false

func WithoutOpenAPIMode added in v0.6.0

func WithoutOpenAPIMode() Option

WithoutOpenAPIMode disables OpenAPI-specific keyword validation

func WithoutSecurityValidation added in v0.5.0

func WithoutSecurityValidation() Option

WithoutSecurityValidation disables security validation for request validation

type RegexCache added in v0.9.0

type RegexCache interface {
	Load(key any) (value any, ok bool) // Get a compiled regex from the cache
	Store(key, value any)              // Set a compiled regex to the cache
}

RegexCache can be set to enable compiled regex caching. It can be just a sync.Map, or a custom implementation with possible cleanup.

Be aware that the cache should be thread safe

type ValidationOptions

type ValidationOptions struct {
	RegexEngine         jsonschema.RegexpEngine
	RegexCache          RegexCache // Enable compiled regex caching
	FormatAssertions    bool
	ContentAssertions   bool
	SecurityValidation  bool
	OpenAPIMode         bool // Enable OpenAPI-specific vocabulary validation
	AllowScalarCoercion bool // Enable string->boolean/number coercion
	Formats             map[string]func(v any) error
	SchemaCache         cache.SchemaCache // Optional cache for compiled schemas
	PathTree            radix.PathLookup  // O(k) path lookup via radix tree (built automatically)

	Logger                        *slog.Logger // Logger for debug/error output (nil = silent)
	AllowXMLBodyValidation        bool         // Allows to convert XML to JSON for validating a request/response body.
	AllowURLEncodedBodyValidation bool         // Allows to convert URL Encoded to JSON for validating a request/response body.

	// strict mode options - detect undeclared properties even when additionalProperties: true
	StrictMode           bool     // Enable strict property validation
	StrictIgnorePaths    []string // Instance JSONPath patterns to exclude from strict checks
	StrictIgnoredHeaders []string // Headers to always ignore in strict mode (nil = use defaults)
	// contains filtered or unexported fields
}

ValidationOptions A container for validation configuration.

Generally fluent With... style functions are used to establish the desired behavior.

func NewValidationOptions

func NewValidationOptions(opts ...Option) *ValidationOptions

NewValidationOptions creates a new ValidationOptions instance with default values.

func (*ValidationOptions) GetEffectiveStrictIgnoredHeaders added in v0.10.0

func (o *ValidationOptions) GetEffectiveStrictIgnoredHeaders() []string

GetEffectiveStrictIgnoredHeaders returns the list of headers to ignore based on configuration. Returns defaults if not configured, merged list if extra headers were added, or replaced list if headers were fully replaced.

func (*ValidationOptions) IsPathTreeDisabled added in v0.13.0

func (o *ValidationOptions) IsPathTreeDisabled() bool

IsPathTreeDisabled returns true if radix tree auto-build was disabled via DisablePathTree.

Jump to

Keyboard shortcuts

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