gospectral

package module
v0.0.0-...-66fc070 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 37 Imported by: 0

README

Go-Spectral

This tool is under active development and must be considered alpha. It's API may be changed in a breaking way until a 1.0 version is released. Submit issues to the Github issue tracker if found.

go-spectral is a Go wrapper for stoplightio/spectral using the pure Go ECMAScript 5.1 implementation Goja with Node support using goja_nodejs. The additional Node system calls are translated (roughly) to either no-op's (if the function during preliminary testing was not used) or the respective Go SDK methods.

Import
$ go get github.com/Emptyless/go-spectral@latest
Quickstart
package main

import (
	"fmt"

	"github.com/Emptyless/go-spectral"
)

func main() {
	output, err := gospectral.Lint([]string{"./openapi.yaml"}, "./.spectral.yaml")
	if err != nil {
		panic(err)
	}

	fmt.Println(output)
}
Options

To customise the behavior of Lint, additional options can be supplied:

  • WithWorkingDirectory: sets the working directory used to load system files (e.g. .spectral.yaml)
  • WithFS: sets the Config.FS to load documents and rulesets from. This can be useful when using e.g. embed.FS as a means to bundle specs/rulesets.
  • WithDist: sets the Config.Dist to a custom supplied value. This can be useful for using a specific version of the source and/or bundling it on your own.
  • WithScript: sets the Config.Script to a custom value
  • WithBeforeModule: is evaluated before any module is enabled and can be used to change e.g. the Loader. If a non-nil Enable.Fn is returned, it is used instead of the provided Enable
  • WithAfterModule: is evaluated after any module is enabled and can be used to change the current runtime state
TODO's
  • get basic structure of the wrapper working
  • add tests
Mentions

Notice

See ./NOTICE for the NOTICE as produced by the webpack-license-plugin for dist/built.js

Documentation

Index

Constants

View Source
const DistName = "./dist/built.js"

DistName used to import built.js e.g. with `var spectral = require('./dist/built.js');`

Variables

View Source
var ErrAfterModule = errors.New("failed to run AfterModule hook")

ErrAfterModule if the AfterModule call failed

View Source
var ErrBeforeModule = errors.New("failed to run BeforeModule hook")

ErrBeforeModule if the BeforeModule call failed

View Source
var ErrPromiseRejected = errors.New("promise rejected")

ErrPromiseRejected when the JS promise is rejected

View Source
var ErrUnknownReturn = errors.New("unknown return")

ErrUnknownReturn when the result of the operation is not a string type

View Source
var Licenses embed.FS

Licenses includes the built.js.LICENSE.txt and oss-licenses.json file produced by Webpack for the dist

Functions

func DefaultDist

func DefaultDist() []byte

DefaultDist returning the transpiled source with the library

func DefaultScript

func DefaultScript() []byte

DefaultScript returns the default script that evaluates the DefaultDist

func EnableDist

func EnableDist(require *require.RequireModule) error

func LoadModules

func LoadModules(runtime *goja.Runtime, registry *noderequire.Registry, beforeModule BeforeModule, afterModule AfterModule) (*noderequire.RequireModule, error)

LoadModules replacing NodeJS functionality required by Dist

Types

type AfterModule

type AfterModule = func(name string, runtime *goja.Runtime, registry *noderequire.Registry, requireModule *noderequire.RequireModule) error

AfterModule is evaluated after any module is enabled and can be used to change the current runtime state

type BeforeModule

type BeforeModule = func(enable Enable, runtime *goja.Runtime, registry *noderequire.Registry, requireModule *noderequire.RequireModule) (func(runtime *goja.Runtime, registry *noderequire.Registry, requireModule *noderequire.RequireModule), error)

BeforeModule is evaluated before any module is enabled and can be used to change e.g. the Loader. If a non-nil Enable.Fn is returned, it is used instead of the provided Enable

func DefaultBeforeModule

func DefaultBeforeModule(currentWorkingDirectory string, fileSystem fs.FS, printer console.Printer) BeforeModule

DefaultBeforeModule implementation of BeforeModule is constructed using a curried function containing the working directory and a possibly virtual filesystem

type Config

type Config struct {
	// Dist is the source JS file exporting
	// 1) function: formatOutput
	// 2) promise: lint output using global variables lintDocuments and lintRuleset
	//
	// See the index.js for more details
	Dist []byte

	// Script that evaluates the Dist, i.e. by the default
	// by using the output of the lint promise and wrapping
	// it in a formatOutput promise
	Script []byte

	// FS to use when loading document(s) and ruleset. If set, when a file is loaded (e.g. an OpenAPI document or
	// a ruleset) it is first searched in the FS *without* the WorkingDirectory reference. If the file is not found
	// in the FS, continue the search relative to the WorkingDirectory. This mechanism allows to bundle static files
	// (e.g. rulesets) and also have a runtime component.
	FS fs.FS

	// Printer used for console output
	Printer console.Printer

	// WorkingDirectory, defaults to os.Getcwd() if ""
	WorkingDirectory string

	// BeforeModule hook to customize behavior before (or instead of) enabling a module
	BeforeModule BeforeModule

	// AfterModule hook to customize runtime or registry state
	AfterModule AfterModule
}

Config to instantiate the runner

type Enable

type Enable struct {
	// Name of the Module to Enable
	Name string

	// Fn to Enable Module
	Fn func(runtime *goja.Runtime, registry *noderequire.Registry, requireModule *noderequire.RequireModule)
}

Enable module with Name

func Enables

func Enables() []Enable

Enables slice to LoadModules

type EvaluateError

type EvaluateError struct {
	Err error
}

EvaluateError translates various failure cases in an easier to understand format

func (EvaluateError) Error

func (e EvaluateError) Error() string

Error implementation of EvaluateError

type LogrusPrinter

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

LogrusPrinter is an adapter from logrus.Logger to console.Printer

func (LogrusPrinter) Error

func (l LogrusPrinter) Error(s string)

Error to logrus.Error

func (LogrusPrinter) Log

func (l LogrusPrinter) Log(s string)

Log to logrus.Info

func (LogrusPrinter) Warn

func (l LogrusPrinter) Warn(s string)

Warn to logrus.Warn

type Option

type Option func(config *Config) error

Option that can be supplied to modify the Config

func WithAfterModule

func WithAfterModule(after AfterModule) Option

WithAfterModule sets the Config.AfterModule

func WithBeforeModule

func WithBeforeModule(before BeforeModule) Option

WithBeforeModule sets the Config.BeforeModule

func WithDist

func WithDist(dist []byte) Option

WithDist sets the Config.Dist to a custom supplied value. This can be useful for using a specific version of the source and/or bundling it on your own.

func WithFS

func WithFS(fs fs.FS) Option

WithFS sets the Config.FS to load documents and rulesets from. This can be useful when using e.g. embed.FS as a means to bundle specs/rulesets.

func WithLogrus

func WithLogrus(logger *logrus.Logger) Option

WithLogrus uses a logrus.Logger (or if nil, the global logrus.Logger) for console output produced by goja

func WithScript

func WithScript(script []byte) Option

WithScript sets the Config.Script to a custom value

func WithWorkingDirectory

func WithWorkingDirectory(workingDirectory string) Option

WithWorkingDirectory sets the working directory used to load system files (e.g. .spectral.yaml)

type Output

type Output []Rule

Output is a slice of Rule

func Lint

func Lint(documents []string, ruleset string, options ...Option) (Output, error)

Lint OpenAPI documents (e.g. openapi.yaml) with a Spectral ruleset, e.g. `extends: ["spectral:oas"]`

type Rule

type Rule struct {
	Source   string   `json:"source"`
	Code     string   `json:"code"`
	Path     []string `json:"path"`
	Message  string   `json:"message"`
	Severity int      `json:"severity"`
	Range    struct {
		Start struct {
			Line      int `json:"line"`
			Character int `json:"character"`
		} `json:"start"`
		End struct {
			Line      int `json:"line"`
			Character int `json:"character"`
		} `json:"end"`
	}
}

Rule is an instance of a failure during Lint

Directories

Path Synopsis
node
fs
os
process
Code generated by process/index.js; DO NOT EDIT.
Code generated by process/index.js; DO NOT EDIT.
tty
vm

Jump to

Keyboard shortcuts

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