config

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 3 Imported by: 1

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 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 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(cache 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 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
	Logger              *slog.Logger      // Logger for debug/error output (nil = silent)

	// 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.

Jump to

Keyboard shortcuts

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