validator

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSchema = errors.New("no schema declared")

ErrNoSchema is returned by SchemaValidator.ValidateSchema when the document supports schema validation but does not declare a schema.

Functions

func JSONSchemaValidate

func JSONSchemaValidate(schemaURL string, docJSON []byte) (bool, error)

func JSONSchemaValidateWithPositions added in v2.2.0

func JSONSchemaValidateWithPositions(schemaURL string, docJSON []byte, posMap map[string]SourcePosition) (bool, error)

JSONSchemaValidateWithPositions validates docJSON against schemaURL and annotates errors with source positions from posMap. The map keys are gojsonschema context strings like "(root).name".

func ValidateXSD

func ValidateXSD(b []byte, schemaPath string) (bool, error)

ValidateXSD validates XML bytes against an XSD file at the given path. Exported for use by the CLI when applying external schemas.

Types

type CsvValidator

type CsvValidator struct {
	Delimiter  rune
	Comment    rune
	LazyQuotes bool
}

CsvValidator validates CSV files. Zero-value fields use Go's csv defaults (comma delimiter, no comment character, strict quotes).

func (CsvValidator) ValidateSyntax

func (v CsvValidator) ValidateSyntax(b []byte) (bool, error)

ValidateSyntax checks if the provided byte slice represents a valid .csv file. https://pkg.go.dev/encoding/csv

type EditorConfigValidator

type EditorConfigValidator struct{}

func (EditorConfigValidator) ValidateSyntax

func (EditorConfigValidator) ValidateSyntax(b []byte) (bool, error)

Validate implements the Validator interface by attempting to parse a byte array of an editorconfig file using editorconfig-core-go package

type EnvValidator

type EnvValidator struct{}

func (EnvValidator) ValidateSyntax

func (EnvValidator) ValidateSyntax(b []byte) (bool, error)

Validate implements the Validator interface by attempting to parse a byte array of a env file using envparse package

type HclValidator

type HclValidator struct{}

HclValidator is used to validate a byte slice that is intended to represent a HashiCorp Configuration Language (HCL) file.

func (HclValidator) ValidateSyntax

func (HclValidator) ValidateSyntax(b []byte) (bool, error)

Validate checks if the provided byte slice represents a valid .hcl file.

The hcl parser uses FIFO to determine which error to display to the user. For more information, see the documentation at:

https://pkg.go.dev/github.com/hashicorp/hcl/v2#Diagnostics.Error

If the hcl.Diagnostics slice contains more than one error, the wrapped error returned by this function will include them as "and {count} other diagnostic(s)" in the error message.

type HoconValidator

type HoconValidator struct{}

HoconValidator is used to validate a byte slice that is intended to represent a HOCON file.

func (HoconValidator) ValidateSyntax

func (HoconValidator) ValidateSyntax(b []byte) (bool, error)

Validate checks if the provided byte slice represents a valid .hocon file.

type IniValidator

type IniValidator struct {
	ForbidDuplicateKeys bool
}

IniValidator validates INI files. When ForbidDuplicateKeys is true, duplicate keys within the same section are reported as errors.

func (IniValidator) ValidateSyntax

func (v IniValidator) ValidateSyntax(b []byte) (bool, error)

type JSONCValidator added in v2.2.0

type JSONCValidator struct{}

func (JSONCValidator) MarshalToJSON added in v2.2.0

func (JSONCValidator) MarshalToJSON(b []byte) ([]byte, error)

func (JSONCValidator) ValidateSchema added in v2.2.0

func (JSONCValidator) ValidateSchema(b []byte, filePath string) (bool, error)

func (JSONCValidator) ValidateSyntax added in v2.2.0

func (JSONCValidator) ValidateSyntax(b []byte) (bool, error)

type JSONMarshaler

type JSONMarshaler interface {
	MarshalToJSON(b []byte) ([]byte, error)
}

JSONMarshaler is an optional interface for validators whose content can be converted to JSON for schema validation. This is used when an external schema (e.g. from SchemaStore or --schema-map) is applied to a file that does not declare its own $schema.

type JSONValidator

type JSONValidator struct {
	ForbidDuplicateKeys bool
}

JSONValidator validates JSON files. When ForbidDuplicateKeys is true, duplicate keys in objects are reported as errors.

func (JSONValidator) MarshalToJSON

func (JSONValidator) MarshalToJSON(b []byte) ([]byte, error)

func (JSONValidator) ValidateSchema

func (JSONValidator) ValidateSchema(b []byte, filePath string) (bool, error)

func (JSONValidator) ValidateSyntax

func (v JSONValidator) ValidateSyntax(b []byte) (bool, error)

type JustfileValidator added in v2.2.0

type JustfileValidator struct{}

func (JustfileValidator) ValidateSyntax added in v2.2.0

func (JustfileValidator) ValidateSyntax(b []byte) (bool, error)

type PlistValidator

type PlistValidator struct{}

PlistValidator is used to validate a byte slice that is intended to represent a Apple Property List file (plist).

func (PlistValidator) ValidateSyntax

func (PlistValidator) ValidateSyntax(b []byte) (bool, error)

Validate checks if the provided byte slice represents a valid .plist file.

type PropValidator

type PropValidator struct{}

func (PropValidator) ValidateSyntax

func (PropValidator) ValidateSyntax(b []byte) (bool, error)

Validate implements the Validator interface by attempting to parse a byte array of properties

type SarifValidator

type SarifValidator struct{}

func (SarifValidator) ValidateSchema

func (SarifValidator) ValidateSchema(b []byte, _ string) (bool, error)

func (SarifValidator) ValidateSyntax

func (SarifValidator) ValidateSyntax(b []byte) (bool, error)

type SchemaErrorPosition added in v2.2.0

type SchemaErrorPosition struct {
	Line   int
	Column int
}

SchemaErrorPosition holds the source position for a single schema error.

type SchemaErrors

type SchemaErrors struct {
	Prefix    string
	Items     []string
	Positions []SchemaErrorPosition
}

SchemaErrors holds multiple schema validation errors. The joined Error() string is used for backward compatibility, while Errors() returns individual error messages. Positions is parallel to Items — Positions[i] is the source position for Items[i]. A zero-value position means the location is unknown.

func (*SchemaErrors) Error

func (e *SchemaErrors) Error() string

func (*SchemaErrors) Errors

func (e *SchemaErrors) Errors() []string

type SchemaValidator

type SchemaValidator interface {
	ValidateSchema(b []byte, filePath string) (bool, error)
}

SchemaValidator is an optional interface for validators that support schema validation. The filePath parameter is the absolute path to the file being validated, used to resolve relative schema references. ValidateSchema should return ErrNoSchema when the document does not declare a schema (e.g. no $schema property in JSON).

type SourcePosition added in v2.2.0

type SourcePosition struct {
	Line   int
	Column int
}

SourcePosition holds a 1-based line and column in the original source file.

type TomlValidator

type TomlValidator struct{}

func (TomlValidator) MarshalToJSON

func (TomlValidator) MarshalToJSON(b []byte) ([]byte, error)

func (TomlValidator) ValidateSchema

func (TomlValidator) ValidateSchema(b []byte, filePath string) (bool, error)

func (TomlValidator) ValidateSyntax

func (TomlValidator) ValidateSyntax(b []byte) (bool, error)

type ToonValidator

type ToonValidator struct{}

func (ToonValidator) MarshalToJSON

func (ToonValidator) MarshalToJSON(b []byte) ([]byte, error)

func (ToonValidator) ValidateSchema

func (ToonValidator) ValidateSchema(b []byte, filePath string) (bool, error)

func (ToonValidator) ValidateSyntax

func (ToonValidator) ValidateSyntax(b []byte) (bool, error)

type ValidationError

type ValidationError struct {
	Err    error
	Line   int
	Column int
}

ValidationError wraps a validation error with optional source position. Line and Column are 1-based. A zero value means the position is unknown.

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

type Validator

type Validator interface {
	ValidateSyntax(b []byte) (bool, error)
}

Validator is the base interface that all validators must implement. For optional capabilities, use type assertions against SchemaValidator.

type XMLSchemaValidator

type XMLSchemaValidator interface {
	ValidateXSD(b []byte, schemaPath string) (bool, error)
}

XMLSchemaValidator is a marker interface for validators that use XSD schema validation instead of JSON Schema. When an external schema is applied via --schema-map or --schemastore, the CLI uses ValidateXSD instead of JSONSchemaValidate.

type XMLValidator

type XMLValidator struct{}

func (XMLValidator) ValidateSchema

func (XMLValidator) ValidateSchema(b []byte, filePath string) (bool, error)

func (XMLValidator) ValidateSyntax

func (XMLValidator) ValidateSyntax(b []byte) (bool, error)

func (XMLValidator) ValidateXSD

func (XMLValidator) ValidateXSD(b []byte, schemaPath string) (bool, error)

ValidateXSD satisfies the XMLSchemaValidator marker interface.

type YAMLValidator

type YAMLValidator struct{}

YAMLValidator validates YAML files. Note: yaml.v3 already rejects duplicate keys by default.

func (YAMLValidator) MarshalToJSON

func (YAMLValidator) MarshalToJSON(b []byte) ([]byte, error)

func (YAMLValidator) ValidateSchema

func (YAMLValidator) ValidateSchema(b []byte, filePath string) (bool, error)

func (YAMLValidator) ValidateSyntax

func (YAMLValidator) ValidateSyntax(b []byte) (bool, error)

Jump to

Keyboard shortcuts

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