validators

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CustomProvidersDir string
)

Functions

func ExtractOpenAPIMetadata added in v1.2.1

func ExtractOpenAPIMetadata(body []byte, result *models.ValidationResult)

ExtractOpenAPIMetadata walks an OpenAPI/Swagger document and stamps the results on the ValidationResult. Caller should check LooksOpenAPI first.

Adds (when present):

extra.openapi_paths     -> number of path entries
extra.openapi_schemas  -> number of schemas under components / definitions
extra.openapi_version  -> "3.x.x" or "2.x"
extra.openapi_title    -> info.title

func GetCategories added in v1.0.3

func GetCategories() ([]string, error)

func GetCommonDomains added in v1.0.8

func GetCommonDomains() []string

func LooksOpenAPI added in v1.2.1

func LooksOpenAPI(body []byte) bool

LooksOpenAPI returns true when the body has the basic shape of an OpenAPI or Swagger document. We only sniff well-known top-level keys to avoid false positives on regular API responses.

func NewValidatorFactory added in v1.0.8

func NewValidatorFactory(proxy string, timeout int) (*ValidatorFactory, []ProviderConfig, *client.ProxyRotator, error)

func NewValidatorFactoryWithOptions added in v1.2.0

func NewValidatorFactoryWithOptions(proxy string, timeout int, opts FactoryOptions) (*ValidatorFactory, []ProviderConfig, *client.ProxyRotator, error)

NewValidatorFactoryWithOptions is the configurable constructor. opts.PersistentCachePath enables a cross-run positive cache; opts.DisableCache forces every request through the network regardless of either cache layer.

Types

type DetectionConfig added in v1.2.1

type DetectionConfig struct {
	// MinScore is the minimum total detection score for this provider to be
	// considered a match. The default detector scoring produces scores well
	// over 100 for a confident match; values in the 50-150 range are typical
	// tuning points.
	MinScore int `yaml:"min_score,omitempty"`
}

DetectionConfig carries per-provider detection tuning.

type DetectionResult added in v1.0.4

type DetectionResult struct {
	Provider    string
	Suggestions []string
	Message     string
	Entropy     float64
	// Score is the detection confidence score for the chosen provider. Higher
	// is more confident; providers without an entry get a default of 0.
	Score int
}

func DetectProviderWithSuggestion added in v1.0.4

func DetectProviderWithSuggestion(key string, idx *DetectorIndex, manualCategory string) DetectionResult

type Detector

type Detector struct {
	// contains filtered or unexported fields
}

func NewDetector

func NewDetector() *Detector

func NewDetectorFromConfigs

func NewDetectorFromConfigs(configs []ProviderConfig) *Detector

func (*Detector) DetectProvider

func (d *Detector) DetectProvider(apiKey string, manualCategory string) string

func (*Detector) DetectProviderWithSuggestion added in v1.0.4

func (d *Detector) DetectProviderWithSuggestion(apiKey string, manualCategory string) DetectionResult

type DetectorIndex added in v1.0.7

type DetectorIndex struct {
	// contains filtered or unexported fields
}

func BuildDetectionIndex

func BuildDetectionIndex(configs []ProviderConfig) *DetectorIndex

type EndpointConfig added in v1.0.9

type EndpointConfig struct {
	URL     string            `yaml:"url"`
	Region  string            `yaml:"region,omitempty"`
	Headers map[string]string `yaml:"headers,omitempty"`
}

type ErrorCheck added in v1.0.9

type ErrorCheck struct {
	JSONPath string `yaml:"json_path,omitempty"`
}

type FactoryOptions added in v1.2.0

type FactoryOptions struct {
	// PersistentCachePath enables the cross-run positive cache when non-empty.
	// When empty, the factory uses only the in-memory ValidationCache.
	PersistentCachePath string
	// PersistentCacheTTL is the freshness window for persistent entries.
	// Zero means use the default (5 minutes).
	PersistentCacheTTL time.Duration
	// DisableCache disables both layers; validators will always hit the
	// network. Used when the user passes --no-cache.
	DisableCache bool
}

FactoryOptions configures the optional layers attached to a ValidatorFactory.

type FailureWhen added in v1.2.0

type FailureWhen struct {
	BodyRegex     string `yaml:"body_regex,omitempty"`
	BodyJSONPath  string `yaml:"body_jsonpath,omitempty"`
	JSONPathValue string `yaml:"jsonpath_value,omitempty"`
}

type GenericValidator

type GenericValidator struct {
	// contains filtered or unexported fields
}

func NewGenericValidatorWithClient

func NewGenericValidatorWithClient(cfg ProviderConfig, httpClient *http.Client, limiter *client.RateLimiterManager) *GenericValidator

func (*GenericValidator) Config added in v1.2.1

func (v *GenericValidator) Config() ProviderConfig

Config returns a copy of the provider configuration so callers (e.g. the runner's retry budget) can read per-provider tuning fields without being able to mutate the validator's internal state.

func (*GenericValidator) FetchMetadata added in v1.0.9

func (v *GenericValidator) FetchMetadata(ctx context.Context, apiKey string, result *models.ValidationResult)

func (*GenericValidator) KeyPatterns

func (v *GenericValidator) KeyPatterns() []string

func (*GenericValidator) Name

func (v *GenericValidator) Name() string

func (*GenericValidator) SetCache added in v1.0.9

func (v *GenericValidator) SetCache(cache client.ResultCache)

func (*GenericValidator) SetCanaryCheck added in v1.0.8

func (v *GenericValidator) SetCanaryCheck(check bool)

func (*GenericValidator) SetSkipMetadata added in v1.0.8

func (v *GenericValidator) SetSkipMetadata(skip bool)

func (*GenericValidator) Validate

func (v *GenericValidator) Validate(ctx context.Context, apiKey string) (*models.ValidationResult, error)

type LintIssue added in v1.2.1

type LintIssue struct {
	Severity LintSeverity `json:"severity"`
	Provider string       `json:"provider,omitempty"`
	Field    string       `json:"field,omitempty"`
	Message  string       `json:"message"`
}

LintIssue is a single finding produced by LintProvider / LintAll.

func LintAll added in v1.2.1

func LintAll(configs []ProviderConfig) ([]LintIssue, bool)

LintAll validates every provider in the given configs and returns the combined issue list. The bool return is true when no errors were found.

func LintFile added in v1.2.1

func LintFile(path string) ([]LintIssue, bool, error)

LintFile reads and lints a YAML file (single file, multiple providers).

func LintProvider added in v1.2.1

func LintProvider(cfg ProviderConfig) []LintIssue

LintProvider runs all schema checks against one provider config.

func (LintIssue) String added in v1.2.1

func (i LintIssue) String() string

type LintSeverity added in v1.2.1

type LintSeverity string

LintSeverity ranks issues. Errors block a run; warnings surface problems the user should know about but are not necessarily fatal.

const (
	SeverityError   LintSeverity = "error"
	SeverityWarning LintSeverity = "warning"
)

type MetadataConfig

type MetadataConfig struct {
	URL               string            `yaml:"url"`
	Method            string            `yaml:"method"`
	Auth              string            `yaml:"auth"`
	Headers           map[string]string `yaml:"headers"`
	BalancePath       string            `yaml:"balance_path"`
	Extract           string            `yaml:"extract"`
	StoreAs           string            `yaml:"store_as"`
	RegexExtract      string            `yaml:"regex_extract"`
	RegexExtractMatch int               `yaml:"regex_extract_match"`
}

type MetadataFromValidation

type MetadataFromValidation struct {
	BalancePath         string `yaml:"balance_path"`
	BalanceSubtractPath string `yaml:"balance_subtract_path"`
	NamePath            string `yaml:"name_path"`
	NameFallbackPath    string `yaml:"name_fallback_path"`
	EmailPath           string `yaml:"email_path"`
	QuotaPath           string `yaml:"quota_path"`
	CreditsPath         string `yaml:"credits_path"`
	VIPLevelPath        string `yaml:"vip_level_path"`
	TeamNamePath        string `yaml:"team_name_path"`
	UsernamePath        string `yaml:"username_path"`
	RegexExtract        string `yaml:"regex_extract"`
	RegexExtractMatch   int    `yaml:"regex_extract_match"`
}

type PatternEntry

type PatternEntry struct {
	Regex    *regexp.Regexp
	Provider string
	Category string
}

type PrefixEntry

type PrefixEntry struct {
	Prefix   string
	Provider string
	Category string
}

type PrefixIndex added in v1.0.5

type PrefixIndex map[string][]PrefixEntry

type PrefixTrie added in v1.1.0

type PrefixTrie struct {
	// contains filtered or unexported fields
}

func NewPrefixTrie added in v1.1.0

func NewPrefixTrie() *PrefixTrie

func (*PrefixTrie) Add added in v1.1.0

func (t *PrefixTrie) Add(p *PrefixEntry)

func (*PrefixTrie) Build added in v1.1.0

func (t *PrefixTrie) Build()

func (*PrefixTrie) Search added in v1.1.0

func (t *PrefixTrie) Search(key string) []*PrefixEntry

type ProviderConfig

type ProviderConfig struct {
	Name                   string                  `yaml:"name"`
	Category               string                  `yaml:"category"`
	KeyPrefixes            []string                `yaml:"key_prefixes"`
	KeyPatterns            []string                `yaml:"key_patterns"`
	SyntaxCheck            string                  `yaml:"syntax_check,omitempty"`
	CanaryPatterns         []string                `yaml:"canary_patterns,omitempty"`
	Validation             ValidationConfig        `yaml:"validation"`
	Metadata               []MetadataConfig        `yaml:"metadata,omitempty"`
	MetadataFromValidation *MetadataFromValidation `yaml:"metadata_from_validation,omitempty"`
	// Detection tunes how aggressively this provider matches unknown keys.
	// MinScore, when > 0, requires the detector to score at least this high
	// before reporting this provider as a match.
	Detection *DetectionConfig `yaml:"detection,omitempty"`
	// CacheTTLSeconds overrides the global positive-cache TTL for entries
	// produced by this provider. Zero means "use the global default".
	CacheTTLSeconds int `yaml:"cache_ttl_seconds,omitempty"`
	// RetryPolicy overrides the global retry budget. MaxAttempts is total
	// tries (1 = no retries). Backoff is exponential with cap.
	RetryPolicy *RetryPolicy `yaml:"retry_policy,omitempty"`
	// RegionalEndpoints lists alternative validation URLs to race against
	// the primary validation.url. The first 200 wins and the endpoint that
	// succeeded is recorded on the result.
	RegionalEndpoints []EndpointConfig `yaml:"regional_endpoints,omitempty"`
	IsCustom          bool             `yaml:"-"`
}

func InitValidatorsWithConfigs

func InitValidatorsWithConfigs(proxy string, timeout int) (map[string]Validator, []ProviderConfig, *client.ProxyRotator, error)

func LoadProviderConfigs

func LoadProviderConfigs() ([]ProviderConfig, error)

func LoadProviderFile added in v1.2.1

func LoadProviderFile(path string) ([]ProviderConfig, error)

LoadProviderFile reads and parses one provider YAML file. The file may contain a single provider or a YAML list of providers.

type ProviderInfo

type ProviderInfo struct {
	Name        string
	Category    string
	KeyPrefixes []string
}

func FindProviderByName added in v1.0.3

func FindProviderByName(name string) ([]ProviderInfo, error)

func GetAllProviders

func GetAllProviders() ([]ProviderInfo, error)

func GetProvidersByCategory added in v1.0.3

func GetProvidersByCategory(category string) ([]ProviderInfo, error)

type ResponseMatch added in v1.2.0

type ResponseMatch struct {
	BodyRegex      string `yaml:"body_regex,omitempty"`
	BodyJSONPath   string `yaml:"body_jsonpath,omitempty"`
	JSONPathValue  string `yaml:"jsonpath_value,omitempty"`
	HeaderContains string `yaml:"header_contains,omitempty"`
}

type RetryPolicy added in v1.2.1

type RetryPolicy struct {
	MaxAttempts      int   `yaml:"max_attempts"`
	OnStatus         []int `yaml:"on_status,omitempty"`
	InitialBackoffMs int   `yaml:"initial_backoff_ms,omitempty"`
	MaxBackoffMs     int   `yaml:"max_backoff_ms,omitempty"`
}

RetryPolicy is the per-provider retry behavior.

type TrieNode added in v1.1.0

type TrieNode struct {
	// contains filtered or unexported fields
}

type ValidationConfig

type ValidationConfig struct {
	Method         string            `yaml:"method"`
	URL            string            `yaml:"url"`
	Auth           string            `yaml:"auth"`
	Headers        map[string]string `yaml:"headers"`
	Body           string            `yaml:"body"`
	Endpoints      []EndpointConfig  `yaml:"endpoints,omitempty"`
	ErrorCheck     *ErrorCheck       `yaml:"error_check,omitempty"`
	ExpectedStatus []int             `yaml:"expected_status,omitempty"`
	ResponseMatch  *ResponseMatch    `yaml:"response_match,omitempty"`
	RetryOnStatus  []int             `yaml:"retry_on_status,omitempty"`
	FailureWhen    *FailureWhen      `yaml:"failure_when,omitempty"`
}

type Validator

type Validator interface {
	Name() string
	KeyPatterns() []string
	Validate(ctx context.Context, apiKey string) (*models.ValidationResult, error)
	SetSkipMetadata(bool)
	SetCanaryCheck(bool)
	FetchMetadata(ctx context.Context, apiKey string, result *models.ValidationResult)
}

type ValidatorFactory added in v1.0.8

type ValidatorFactory struct {
	// contains filtered or unexported fields
}

func (*ValidatorFactory) Cache added in v1.0.9

func (f *ValidatorFactory) Cache() client.ResultCache

func (*ValidatorFactory) GetValidator added in v1.0.8

func (f *ValidatorFactory) GetValidator(name string) (Validator, bool)

func (*ValidatorFactory) RegisterConfig added in v1.2.1

func (f *ValidatorFactory) RegisterConfig(cfg ProviderConfig)

RegisterConfig adds (or replaces) a provider config in the factory. Used by tests to inject stub providers without mutating the global cached configs. Production code should rely on LoadProviderConfigs and the --templates flag instead.

func (*ValidatorFactory) SharedClient added in v1.1.0

func (f *ValidatorFactory) SharedClient() *http.Client

func (*ValidatorFactory) SharedLimiter added in v1.2.0

func (f *ValidatorFactory) SharedLimiter() *client.RateLimiterManager

SharedLimiter exposes the underlying RateLimiterManager so callers can install a global rate ceiling via SetGlobalLimit.

Jump to

Keyboard shortcuts

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