contracts

package
v0.0.0-...-ea9222f Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const ValidatorVersion = "contracts-validator/v0.1.1"

Variables

View Source
var (
	ErrInvalidRoot       = errors.New("contracts: invalid schema root")
	ErrNotFound          = errors.New("contracts: schema not found")
	ErrPathEscape        = errors.New("contracts: path escapes root")
	ErrTooManyFiles      = errors.New("contracts: too many schema files")
	ErrFileTooLarge      = errors.New("contracts: schema too large")
	ErrInvalidJSON       = errors.New("contracts: invalid json")
	ErrNotJSONObject     = errors.New("contracts: schema must be a JSON object")
	ErrRefNotAllowed     = errors.New("contracts: $ref not allowed")
	ErrRefTooDeep        = errors.New("contracts: $ref depth exceeded")
	ErrTooManyRefs       = errors.New("contracts: too many $ref resolutions")
	ErrRefPointerInvalid = errors.New("contracts: invalid json pointer")
	ErrCanonicalTooLarge = errors.New("contracts: canonical json exceeds max bytes")
	ErrCompiledTooLarge  = errors.New("contracts: compiled schema exceeds max nodes")
)

Functions

This section is empty.

Types

type CompiledSchema

type CompiledSchema struct {
	RootPath      string            `json:"root_path"`
	LoadedDocs    []LoadedDocDigest `json:"loaded_docs"`    // all schema files referenced (sorted)
	HashSHA256    string            `json:"hash_sha256"`    // hash of CanonicalJSON
	CanonicalJSON []byte            `json:"canonical_json"` // deterministic JSON bytes
	JSON          map[string]any    `json:"json"`           // compiled tree
}

CompiledSchema is a fully inlined schema with all local $ref resolved (bounded).

type LoadedDocDigest

type LoadedDocDigest struct {
	Path   string `json:"path"`
	SHA256 string `json:"sha256"`
}

type Report

type Report struct {
	GeneratedAt      time.Time `json:"generated_at"`
	ValidatorVersion string    `json:"validator_version"`

	SchemaRootPath string `json:"schema_root_path,omitempty"`
	SchemaHash     string `json:"schema_hash,omitempty"`

	CheckedNodes int `json:"checked_nodes"`
	Errors       int `json:"errors"`
	Warnings     int `json:"warnings"`
	Infos        int `json:"infos"`

	Violations []Violation `json:"violations"`
}

func (Report) HasErrors

func (r Report) HasErrors() bool

type SchemaDoc

type SchemaDoc struct {
	Path     string         `json:"path"`      // normalized rel path (slashes)
	LoadedAt time.Time      `json:"loaded_at"` // UTC
	SHA256   string         `json:"sha256"`    // hash of raw bytes
	JSON     map[string]any `json:"json"`      // parsed JSON object
}

SchemaDoc is a raw loaded schema document.

type Severity

type Severity string
const (
	SevInfo  Severity = "info"
	SevWarn  Severity = "warn"
	SevError Severity = "error"
)

type Store

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

func NewStore

func NewStore(root string, opts StoreOptions) (*Store, error)

func (*Store) Compile

func (s *Store) Compile(ctx context.Context, relPath string) (*CompiledSchema, error)

Compile loads a root schema and resolves all local $ref into a single inlined schema (bounded).

func (*Store) List

func (s *Store) List(ctx context.Context) ([]string, error)

List returns all .json files under root as rel paths (sorted). Note: this is discovery; it does not guarantee each file is a valid schema object.

func (*Store) Load

func (s *Store) Load(ctx context.Context, relPath string) (*SchemaDoc, error)

Load loads and parses a schema JSON file (must be object).

type StoreOptions

type StoreOptions struct {
	MaxFiles          int   // default 5000
	MaxFileBytes      int64 // default 2 MiB per schema
	MaxRefDepth       int   // default 64
	MaxRefs           int   // default 20000 (total resolved refs)
	MaxCanonicalBytes int64 // default 4 MiB compiled canonical JSON
	MaxCompiledNodes  int   // default 500000 nodes emitted during compilation
}

type VOptions

type VOptions struct {
	MaxDepth      int `json:"max_depth"`       // default 64
	MaxNodes      int `json:"max_nodes"`       // default 250000
	MaxIssues     int `json:"max_issues"`      // default 10000
	MaxRegexCache int `json:"max_regex_cache"` // default 128

	// big.Float precision for numeric comparisons (enum/const, min/max, etc.)
	NumberPrecision uint `json:"number_precision"` // default 512

	// If true, stop after first error.
	FailFast bool `json:"fail_fast"`

	// If true, warn when ignored keywords are present (format, $comment, title, description, etc.)
	WarnIgnoredKeywords bool `json:"warn_ignored_keywords"`
}

type Validator

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

func NewValidator

func NewValidator(opts VOptions) *Validator

func (*Validator) Validate

func (v *Validator) Validate(ctx context.Context, schema *CompiledSchema, instance any) Report

type Violation

type Violation struct {
	Severity Severity `json:"severity"`
	Code     string   `json:"code"`
	Path     string   `json:"path,omitempty"`      // instance path: $.a.b[0]
	SchemaAt string   `json:"schema_at,omitempty"` // schema path: #/properties/x
	Message  string   `json:"message"`
}

Jump to

Keyboard shortcuts

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