validator

package
v0.1.0-beta.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractSchemaDeclarations

func ExtractSchemaDeclarations(rootElement xmldom.Element) (map[string]string, error)

ExtractSchemaDeclarations extracts schema:* attribute declarations from the root element. These attributes must be in the http://agentflare.ai/agentml/schema namespace. Returns a map of namespace prefix -> URI.

func GitHubSchemaLoader

func GitHubSchemaLoader(httpClient *http.Client) xsd.SchemaLoaderFunc

GitHubSchemaLoader creates a schema loader function that fetches schemas from GitHub For a namespace like "github.com/foo/bar", it will try to fetch: - https://raw.githubusercontent.com/foo/bar/main/bar.xsd - https://raw.githubusercontent.com/foo/bar/master/bar.xsd

func LoadDeclaredSchemas

func LoadDeclaredSchemas(declarations map[string]string, baseDir string) (map[string]*jsonschema.Schema, error)

LoadDeclaredSchemas loads all schemas declared via schema:* attributes Returns a map of namespace prefix -> loaded schema

func LoadFileSchema

func LoadFileSchema(fileURI string, baseDir string) (*jsonschema.Schema, error)

LoadFileSchema loads a JSON schema from a file:// URL Paths are resolved relative to baseDir

func LoadGitHubSchema

func LoadGitHubSchema(githubURL string) (*jsonschema.Schema, error)

LoadGitHubSchema loads a JSON schema from a GitHub URL Converts github.com/user/repo/path/to/schema.json to raw.githubusercontent.com URL

func LoadSchemaFromURI

func LoadSchemaFromURI(uri string, baseDir string) (*jsonschema.Schema, error)

LoadSchemaFromURI loads a JSON schema from a URI Supports file:// (relative to baseDir) and github.com/ URLs

func ResolveSchemaPointer

func ResolveSchemaPointer(schema *jsonschema.Schema, pointer string) (*jsonschema.Schema, error)

ResolveSchemaPointer resolves a JSON pointer within a schema Returns the sub-schema at the specified path

func ResolveSchemaReference

func ResolveSchemaReference(ref *SchemaReference, loadedSchemas map[string]*jsonschema.Schema) (*jsonschema.Schema, error)

ResolveSchemaReference resolves a schema reference to an actual schema Handles both inline JSON and namespace pointers

Types

type CancelExactlyOneRule

type CancelExactlyOneRule struct{}

CancelExactlyOneRule validates <cancel> requires exactly one of sendid/sendidexpr

func (*CancelExactlyOneRule) Name

func (r *CancelExactlyOneRule) Name() string

func (*CancelExactlyOneRule) Validate

func (r *CancelExactlyOneRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type Config

type Config struct {
	Strict     bool   // Treat selected warnings as errors; apply stricter SCXML rules
	DataModel  string // Optional datamodel context (ecmascript, xpath, null, starlark)
	SourceName string // Optional source name for reporting

	// RecursiveInvoke enables recursive validation of invoked SCXML files.
	// When true, the validator will attempt to load and validate any SCXML files
	// referenced in <invoke type="scxml" src="..."> elements.
	RecursiveInvoke bool

	// InvokeBasePath is the base directory to resolve relative paths in invoke src attributes.
	// If empty, paths are resolved relative to the current working directory.
	InvokeBasePath string

	// SchemaBasePath is the base directory to resolve relative schema paths in xmlns declarations.
	// If empty, defaults to InvokeBasePath or current working directory.
	SchemaBasePath string

	// SemanticRules allows injection of custom semantic validators.
	// If nil, DefaultSemanticRules() is used.
	// Set to empty slice to disable semantic validation.
	SemanticRules []SemanticRule

	// SchemaLoaders allows injection of custom XSD schema loaders.
	// Loaders are tried in order - more specific patterns should come first.
	// If nil, default loaders are used.
	SchemaLoaders []SchemaLoaderSpec

	// JSONSchemas stores loaded JSON schemas keyed by namespace prefix.
	// These are loaded from schema:* attributes on the root element.
	// Example: schema:user="file://schema.json" -> JSONSchemas["user"] = loaded schema
	JSONSchemas map[string]*jsonschema.Schema
	// contains filtered or unexported fields
}

Config controls validator behavior

type Diagnostic

type Diagnostic struct {
	Severity  Severity  `json:"severity"`
	Code      string    `json:"code"`
	Message   string    `json:"message"`
	Position  Position  `json:"position"`
	Tag       string    `json:"tag"`
	Attribute string    `json:"attribute,omitempty"`
	SpecRef   string    `json:"spec_ref,omitempty"`
	Hints     []string  `json:"hints,omitempty"`
	Related   []Related `json:"related,omitempty"`
}

Diagnostic describes a validation issue found in the document It is designed to be useful to both humans and LLMs.

func SortedDiagnostics

func SortedDiagnostics(diags []Diagnostic) []Diagnostic

SortedDiagnostics returns diagnostics sorted by file/line/column

type DonedataContentParamExclusionRule

type DonedataContentParamExclusionRule struct{}

DonedataContentParamExclusionRule validates <donedata> cannot have both content and param

func (*DonedataContentParamExclusionRule) Name

func (*DonedataContentParamExclusionRule) Validate

type EventDescriptorRule

type EventDescriptorRule struct{}

EventDescriptorRule validates event descriptor tokens

func (*EventDescriptorRule) Name

func (r *EventDescriptorRule) Name() string

func (*EventDescriptorRule) Validate

func (r *EventDescriptorRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type HistoryShallowTargetRule

type HistoryShallowTargetRule struct{}

HistoryShallowTargetRule validates shallow history target must be immediate child

func (*HistoryShallowTargetRule) Name

func (r *HistoryShallowTargetRule) Name() string

func (*HistoryShallowTargetRule) Validate

func (r *HistoryShallowTargetRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type IDTokenRule

type IDTokenRule struct{}

IDTokenRule validates that ID attributes are valid NCName tokens

func (*IDTokenRule) Name

func (r *IDTokenRule) Name() string

func (*IDTokenRule) Validate

func (r *IDTokenRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type InitialOneTransitionRule

type InitialOneTransitionRule struct{}

InitialOneTransitionRule validates <initial> must have exactly one <transition>

func (*InitialOneTransitionRule) Name

func (r *InitialOneTransitionRule) Name() string

func (*InitialOneTransitionRule) Validate

func (r *InitialOneTransitionRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type InitialTargetDescendantRule

type InitialTargetDescendantRule struct{}

InitialTargetDescendantRule validates <initial>'s target must be a descendant

func (*InitialTargetDescendantRule) Name

func (*InitialTargetDescendantRule) Validate

func (r *InitialTargetDescendantRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type InitialTransitionConstraintsRule

type InitialTransitionConstraintsRule struct{}

InitialTransitionConstraintsRule validates <initial>'s <transition> cannot have event/cond

func (*InitialTransitionConstraintsRule) Name

func (*InitialTransitionConstraintsRule) Validate

type InvokeSrcExclusivityRule

type InvokeSrcExclusivityRule struct{}

InvokeSrcExclusivityRule validates <invoke> cannot have both src and srcexpr

func (*InvokeSrcExclusivityRule) Name

func (r *InvokeSrcExclusivityRule) Name() string

func (*InvokeSrcExclusivityRule) Validate

func (r *InvokeSrcExclusivityRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type JSONConfig

type JSONConfig struct{}

type JSONReporter

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

func NewJSONReporter

func NewJSONReporter(w io.Writer, _ ...JSONConfig) *JSONReporter

func (*JSONReporter) Print

func (r *JSONReporter) Print(result Result) error

type ParamNameAndXorRule

type ParamNameAndXorRule struct{}

ParamNameAndXorRule validates <param> requires name and exactly one of expr/location

func (*ParamNameAndXorRule) Name

func (r *ParamNameAndXorRule) Name() string

func (*ParamNameAndXorRule) Validate

func (r *ParamNameAndXorRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type Position

type Position struct {
	File   string `json:"file"`
	Line   int    `json:"line"`
	Column int    `json:"column"`
	Offset int64  `json:"offset"`
}

Position contains source position information for a diagnostic

type PrettyConfig

type PrettyConfig struct {
	Color           bool
	ContextBefore   int
	ContextAfter    int
	ShowFullElement bool
	MaxElementLines int // 0 = unlimited
}

PrettyConfig configures PrettyReporter construction Zero values are treated as sensible defaults (e.g., 1 line of context)

type PrettyReporter

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

func NewPrettyReporter

func NewPrettyReporter(w io.Writer, maybeCfg ...PrettyConfig) *PrettyReporter

func (*PrettyReporter) Print

func (r *PrettyReporter) Print(sourceName string, source string, diags []Diagnostic) error
type Related struct {
	Label    string   `json:"label"`
	Position Position `json:"position"`
}

Related points to a related location in the source (e.g., reference target) that can help explain or remedy an error.

type Result

type Result struct {
	Diagnostics []Diagnostic `json:"diagnostics"`
}

Result is the aggregate validation result

func ValidateDocument

func ValidateDocument(ctx context.Context, doc xmldom.Document, source string) Result

func (*Result) Add

func (r *Result) Add(diags ...Diagnostic)

Add appends diagnostics to the result

func (*Result) HasErrors

func (r *Result) HasErrors() bool

HasErrors returns true if there is at least one error severity diagnostic

type SchemaLoaderSpec

type SchemaLoaderSpec struct {
	Pattern string               // Regex pattern to match namespace URIs
	Loader  xsd.SchemaLoaderFunc // Function to load the schema
}

SchemaLoaderSpec defines a schema loader with its matching pattern

type SchemaReference

type SchemaReference struct {
	IsInline  bool   // true if inline JSON schema, false if namespace pointer
	Namespace string // namespace prefix (e.g., "user" from "user:/definitions/User")
	Pointer   string // JSON pointer path (e.g., "/definitions/User")
	Inline    string // inline JSON schema string if IsInline is true
}

SchemaReference represents a parsed schema reference

func ParseSchemaReference

func ParseSchemaReference(schemaAttr string) (*SchemaReference, error)

ParseSchemaReference parses a schema attribute value Returns SchemaReference with either inline JSON or namespace+pointer

type SemanticRule

type SemanticRule interface {
	// Name returns the diagnostic code for this rule (e.g., "E301")
	Name() string

	// Validate checks the rule against a document and returns diagnostics
	Validate(doc xmldom.Document, config Config) []Diagnostic
}

SemanticRule validates SCXML-specific semantic constraints that cannot be expressed in XSD schema (e.g., mutual exclusion, context-dependent rules).

func DefaultSemanticRules

func DefaultSemanticRules() []SemanticRule

DefaultSemanticRules returns the standard SCXML 1.0 semantic validators. These rules enforce constraints from the SCXML specification that cannot be expressed in XSD schemas alone.

type SendContentEventExclusionRule

type SendContentEventExclusionRule struct{}

SendContentEventExclusionRule validates <send> with content cannot have event/eventexpr

func (*SendContentEventExclusionRule) Name

func (*SendContentEventExclusionRule) Validate

func (r *SendContentEventExclusionRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type SendNamelistContentExclusionRule

type SendNamelistContentExclusionRule struct{}

SendNamelistContentExclusionRule validates <send> with namelist cannot have content/param

func (*SendNamelistContentExclusionRule) Name

func (*SendNamelistContentExclusionRule) Validate

type Severity

type Severity string

Severity represents the severity level of a diagnostic

const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
	SeverityInfo    Severity = "info"
)

type StateDeadlockRule

type StateDeadlockRule struct{}

StateDeadlockRule validates non-final states have at least one unconditional exit path

func (*StateDeadlockRule) Name

func (r *StateDeadlockRule) Name() string

func (*StateDeadlockRule) Validate

func (r *StateDeadlockRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type StateInitialAtomicRule

type StateInitialAtomicRule struct{}

StateInitialAtomicRule validates atomic state cannot have initial attribute

func (*StateInitialAtomicRule) Name

func (r *StateInitialAtomicRule) Name() string

func (*StateInitialAtomicRule) Validate

func (r *StateInitialAtomicRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type StateInitialConflictRule

type StateInitialConflictRule struct{}

StateInitialConflictRule validates state cannot have both initial attribute and element

func (*StateInitialConflictRule) Name

func (r *StateInitialConflictRule) Name() string

func (*StateInitialConflictRule) Validate

func (r *StateInitialConflictRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type TransitionAtLeastOneRule

type TransitionAtLeastOneRule struct{}

TransitionAtLeastOneRule validates transition must specify event/cond/target

func (*TransitionAtLeastOneRule) Name

func (r *TransitionAtLeastOneRule) Name() string

func (*TransitionAtLeastOneRule) Validate

func (r *TransitionAtLeastOneRule) Validate(doc xmldom.Document, config Config) []Diagnostic

type UnconditionalTransitionCycleRule

type UnconditionalTransitionCycleRule struct{}

UnconditionalTransitionCycleRule detects cycles in unconditional transitions

func (*UnconditionalTransitionCycleRule) Name

func (*UnconditionalTransitionCycleRule) Validate

type Validator

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

Validator validates SCXML documents

func New

func New(cfg ...Config) *Validator

New creates a new Validator

func (*Validator) ValidateDocument

func (v *Validator) ValidateDocument(ctx context.Context, doc xmldom.Document, source string) Result

ValidateDocument runs the rule set on the provided document source should be the raw XML string used to generate doc (for precise positions)

func (*Validator) ValidateReader

func (v *Validator) ValidateReader(ctx context.Context, r io.Reader) (Result, xmldom.Document, string, error)

ValidateReader reads all from r, validates, and returns result+doc

func (*Validator) ValidateString

func (v *Validator) ValidateString(ctx context.Context, xml string) (Result, xmldom.Document, error)

ValidateString validates an SCXML string and returns diagnostics and the parsed document

Directories

Path Synopsis
cmd
validate command

Jump to

Keyboard shortcuts

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