converter

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CasingOptions

func CasingOptions(rc RuleCheck) string

CasingOptions extracts the case type from a RuleCheck's FunctionOptions.

func EnumerationOptions

func EnumerationOptions(rc RuleCheck) []string

EnumerationOptions extracts allowed values from a RuleCheck's FunctionOptions.

func LengthOptions

func LengthOptions(rc RuleCheck) (minVal, maxVal *int)

LengthOptions extracts min/max from a RuleCheck's FunctionOptions.

func LookupNativeRule

func LookupNativeRule(spectralName string) (string, bool)

LookupNativeRule maps a Spectral/Vacuum rule name to a native rule ID. Returns the native ID and true if found, or empty string and false if not mapped.

func PatternOptions

func PatternOptions(rc RuleCheck) (match, notMatch string)

PatternOptions extracts match/notMatch from a RuleCheck's FunctionOptions.

func PropertyOptions

func PropertyOptions(rc RuleCheck) []string

PropertyOptions extracts properties from xor/or RuleCheck's FunctionOptions.

Types

type ExtendsEntry

type ExtendsEntry struct {
	Name     string // e.g., "spectral:oas", "speakeasy-recommended"
	Modifier string // e.g., "all", "recommended", "off" (empty if not specified)
}

ExtendsEntry stores the raw extends value from the source config. Known Name values by format:

Spectral: "spectral:oas", "spectral:asyncapi"
Legacy:   "speakeasy-recommended", "speakeasy-generation"

Known Modifier values (Spectral only, empty for Legacy):

"all"         - enable all rules from the extended ruleset
"recommended" - enable only recommended rules
"off"         - disable the extended ruleset entirely

type GenerateOption

type GenerateOption func(*GenerateOptions)

GenerateOption is a functional option for Generate.

func WithRulePrefix

func WithRulePrefix(prefix string) GenerateOption

WithRulePrefix sets the rule ID prefix.

func WithRulesDir

func WithRulesDir(dir string) GenerateOption

WithRulesDir sets the rules directory.

type GenerateOptions

type GenerateOptions struct {
	// RulesDir is the relative path for .ts files in config (default "./rules").
	RulesDir string

	// RulePrefix is the prefix for generated rule IDs (default "custom-").
	RulePrefix string
}

GenerateOptions configures the output generation.

type GenerateResult

type GenerateResult struct {
	// Config is the native lint config, serializable to YAML.
	Config *linter.Config

	// GeneratedRules maps ruleID -> TypeScript source code.
	GeneratedRules map[string]string

	// Warnings from the generation phase.
	Warnings []Warning
	// contains filtered or unexported fields
}

GenerateResult holds all generated output.

func Generate

func Generate(ir *IntermediateConfig, opts ...GenerateOption) (*GenerateResult, error)

Generate converts an IntermediateConfig into native linter output. This is stage 2 of the pipeline — all native-format-specific interpretation happens here.

func (*GenerateResult) WriteFiles

func (r *GenerateResult) WriteFiles(outputDir string) error

WriteFiles writes lint.yaml and rules/*.ts to the output directory.

type IntermediateConfig

type IntermediateConfig struct {
	// Extends stores the raw extends values from the source config.
	// Examples: ["spectral:oas"], ["speakeasy-recommended"]
	// Interpretation of these values is left to the consumer (Generate maps them).
	Extends []ExtendsEntry

	// Rules is the unified list of all rules from the source config.
	// Each rule is either a severity-only override or a full rule definition.
	// Parse does NOT classify rules as "builtin" vs "custom" — that's a
	// consumer concern. Rules with Given/Then populated are custom rules;
	// rules with only Severity set are overrides.
	Rules []Rule

	// Warnings collected during parsing (malformed entries, etc.)
	Warnings []Warning
}

IntermediateConfig is the format-agnostic intermediate representation. Parse Spectral/Vacuum/Legacy configs into this, then pass to Generate() or consume directly in other projects.

func Parse

func Parse(r io.Reader) (*IntermediateConfig, error)

Parse reads a Spectral, Vacuum, or Legacy Speakeasy config and returns the intermediate representation. Parse is lenient: it collects as many rules as possible and adds warnings for malformed entries rather than failing entirely.

func ParseFile

func ParseFile(path string) (*IntermediateConfig, error)

ParseFile reads a config file and returns the intermediate representation.

type JSONPathMapping

type JSONPathMapping struct {
	// Collection is the index collection name (e.g., "operations", "inlinePathItems").
	// Empty for direct document access patterns.
	Collection string

	// IsKeyAccess is true when the ~ operator is used (extract map key).
	IsKeyAccess bool

	// FieldAccess is a field to extract from each node (e.g., "url" for $.servers[*].url).
	FieldAccess string

	// Filter is a filter expression stripped during structural matching,
	// to be re-applied as generated TypeScript.
	Filter string

	// HTTPMethod is set when matching $.paths[*].{method} patterns.
	HTTPMethod string

	// IsDirect is true when the path targets a single document location
	// (e.g., $.info, $.components) rather than an indexed collection.
	IsDirect bool

	// DirectAccess is the TypeScript expression for direct access.
	DirectAccess string

	// Unsupported is true when the JSONPath could not be mapped.
	Unsupported bool

	// OriginalPath is the original JSONPath expression.
	OriginalPath string
}

JSONPathMapping represents the result of mapping a JSONPath to an index collection and generated TypeScript code.

func MapJSONPath

func MapJSONPath(path string) JSONPathMapping

MapJSONPath maps a JSONPath expression to an index collection and access pattern.

The mapper uses structural matching: 1. Strip filter expressions [?(...)] from the path 2. Strip the ~ (key name) operator 3. Match the structural path against known patterns 4. Return mapping info for code generation

Note on ~ operator: The ~ suffix in JSONPath (e.g., $.paths[*]~) is a Spectral extension, not standard JSONPath. It extracts the map key rather than the value.

type Rule

type Rule struct {
	ID          string      // rule name/ID from source config
	Description string      // rule description
	Message     string      // message template with {{placeholders}}
	Severity    string      // normalized severity (see doc comment above)
	Resolved    *bool       // nil = default (resolved), false = unresolved
	Formats     []string    // "oas2", "oas3", "oas3.0", "oas3.1"
	Source      string      // ruleset name this rule came from (for dedup tracing)
	Given       []string    // JSONPath expressions (empty for overrides)
	Then        []RuleCheck // checks to apply (empty for overrides)
}

Rule is a unified representation of any rule from a source config. Severity-only overrides have Given/Then empty. Full custom rules have Given/Then populated.

Severity is normalized during parsing to canonical values:

"error", "warn", "info", "hint", "off"

Numeric severities from Spectral (0=error, 1=warn, 2=info, 3=hint) are normalized to their string equivalents.

func (*Rule) IsDisabled

func (r *Rule) IsDisabled() bool

IsDisabled returns true if severity is "off".

func (*Rule) IsOverride

func (r *Rule) IsOverride() bool

IsOverride returns true if this rule is a severity-only override (no given/then logic).

type RuleCheck

type RuleCheck struct {
	Field           string         // field to check (empty = check the node itself)
	Function        string         // "truthy", "pattern", "enumeration", etc.
	FunctionOptions map[string]any // function-specific options (documented above)
	Message         string         // per-check message override
}

RuleCheck is a single function application from a "then" clause. FunctionOptions shape depends on Function:

  • "pattern": { "match": string, "notMatch": string }
  • "enumeration": { "values": []string }
  • "length": { "min": number, "max": number }
  • "casing": { "type": string, "disallowDigits": bool, "separator": { "char": string } }
  • "alphabetical":{ "keyedBy": string }
  • "schema": { "schema": object, "dialect": string, "allErrors": bool }
  • "xor"/"or": { "properties": []string }
  • "truthy"/"falsy"/"defined"/"undefined"/"typedEnum": (no options)

type Warning

type Warning struct {
	RuleID  string // the rule this warning relates to (empty for global warnings)
	Phase   string // "parse" or "generate"
	Message string
}

Warning represents a warning generated during parsing or generation.

func GenerateRuleTypeScript

func GenerateRuleTypeScript(rule Rule, rulePrefix string) (string, []Warning)

GenerateRuleTypeScript generates a self-contained TypeScript rule file from an IR Rule. Returns the TypeScript source code.

Jump to

Keyboard shortcuts

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