json

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 License: Apache-2.0 Imports: 13 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrJSONPathInvalid returns an ErrParse when a JSONPath expression could
	// not be parsed.
	ErrJSONPathInvalid = fmt.Errorf(
		"%w: JSONPath invalid", gdterrors.ErrParse,
	)
	// ErrJSONPathInvalidNoRoot returns an ErrParse when a JSONPath expression
	// does not start with '$'
	ErrJSONPathInvalidNoRoot = fmt.Errorf(
		"%w: expression must start with '$'", ErrJSONPathInvalid,
	)
	// ErrJSONUnmarshalError is returned when JSON content cannot be decoded
	ErrJSONUnmarshalError = fmt.Errorf(
		"%w: failed to unmarshal JSON", gdterrors.ErrFailure,
	)
	// ErrJSONPathNotFound returns an ErrFailure when a JSONPath expression
	// could not evaluate to a found element.
	ErrJSONPathNotFound = fmt.Errorf(
		"%w: failed to find element at JSONPath", gdterrors.ErrFailure,
	)
	// ErrJSONPathConversionError returns an ErrFailure when a JSONPath
	// expression evaluated to a found element but could not be converted to a
	// string.
	ErrJSONPathConversionError = fmt.Errorf(
		"%w: JSONPath value could not be compared", gdterrors.ErrFailure,
	)
	// ErrJSONPathNotEqual returns an ErrFailure when a JSONPath
	// expression evaluated to a found element but the value did not match an
	// expected string.
	ErrJSONPathNotEqual = fmt.Errorf(
		"%w: JSONPath values not equal", gdterrors.ErrFailure,
	)
	// ErrJSONSchemaValidateError returns an ErrFailure when a JSONSchema could
	// not be parsed.
	ErrJSONSchemaValidateError = fmt.Errorf(
		"%w: failed to parse JSONSchema", gdterrors.ErrFailure,
	)
	// ErrJSONSchemaInvalid returns an ErrFailure when some content could not
	// be validated with a JSONSchema.
	ErrJSONSchemaInvalid = fmt.Errorf(
		"%w: JSON content did not adhere to JSONSchema", gdterrors.ErrFailure,
	)
	// ErrJSONFormatError returns an ErrFailure when a JSONFormat expression
	// could not evaluate to a found element.
	ErrJSONFormatError = fmt.Errorf(
		"%w: failed to determine JSON format", gdterrors.ErrFailure,
	)
	// ErrJSONFormatNotEqual returns an ErrFailure when a an element at a
	// JSONPath was not in the expected format.
	ErrJSONFormatNotEqual = fmt.Errorf(
		"%w: JSON format not equal", gdterrors.ErrFailure,
	)
	// ErrJSONSchemaFileNotFound indicates a specified JSONSchema file could
	// not be found.
	ErrJSONSchemaFileNotFound = fmt.Errorf(
		"%w: unable to find JSONSchema file",
		gdterrors.ErrParse,
	)
	// ErrUnsupportedJSONSchemaReference indicates that a specified JSONSchema
	// file is referenced as an HTTP(S) URL instead of a file URI.
	ErrUnsupportedJSONSchemaReference = fmt.Errorf(
		"%w: unsupported JSONSchema reference",
		gdterrors.ErrParse,
	)
)

Functions

func JSONFormatError

func JSONFormatError(format string, err error) error

JSONFormatError returns an ErrFailure when a JSONFormat expression could not evaluate to a found element.

func JSONFormatNotEqual

func JSONFormatNotEqual(path string, exp string) error

JSONFormatNotEqual returns an ErrFailure when a an element at a JSONPath was not in the expected format.

func JSONPathConversionError

func JSONPathConversionError(path string, exp interface{}, got interface{}) error

JSONPathConversionError returns an ErrFailure when a JSONPath expression evaluated to a found element but the expected and found value types were incomparable.

func JSONPathInvalid

func JSONPathInvalid(path string, err error, node *yaml.Node) error

JSONPathInvalid returns an ErrParse when a JSONPath expression could not be parsed.

func JSONPathInvalidNoRoot

func JSONPathInvalidNoRoot(path string, node *yaml.Node) error

JSONPathInvalidNoRoot returns an ErrJSONPathInvalidNoRoot when a JSONPath expression does not start with '$'.

func JSONPathNotEqual

func JSONPathNotEqual(path string, exp interface{}, got interface{}) error

JSONPathValueNotEqual returns an ErrFailure when a JSONPath expression evaluated to a found element but the value did not match an expected string.

func JSONPathNotFound

func JSONPathNotFound(path string, err error) error

JSONPathNotFound returns an ErrFailure when a JSONPath expression could not evaluate to a found element.

func JSONSchemaFileNotFound

func JSONSchemaFileNotFound(path string, node *yaml.Node) error

JSONSchemaFileNotFound returns ErrJSONSchemaFileNotFound for a supplied path.

func JSONSchemaInvalid

func JSONSchemaInvalid(path string, err error) error

JSONSchemaInvalid returns an ErrFailure when some content could not be validated with a JSONSchema.

func JSONSchemaValidateError

func JSONSchemaValidateError(path string, err error) error

JSONSchemaValidateError returns an ErrFailure when a JSONSchema could not be parsed.

func JSONUnmarshalError

func JSONUnmarshalError(err error, node *yaml.Node) error

JSONUnmarshalError returns an ErrFailure when JSON content cannot be decoded.

func New

func New(
	exp *Expect,
	content []byte,
) gdttypes.Assertions

New returns a `gdttypes.Assertions` that asserts various conditions about JSON content

func UnsupportedJSONSchemaReference

func UnsupportedJSONSchemaReference(url string, node *yaml.Node) error

UnsupportedJSONSchemaReference returns ErrUnsupportedJSONSchemaReference for a supplied URL.

Types

type Expect

type Expect struct {
	// Length of the expected JSON string.
	Len *int `yaml:"len,omitempty"`
	// Paths is a map, keyed by JSONPath expression, of expected values to find
	// at that expression.
	Paths map[string]string `yaml:"paths,omitempty"`
	// PathFormats is a map, keyed by JSONPath expression, of expected formats
	// that values found at the expression should have.
	PathFormats map[string]string `yaml:"path-formats,omitempty"`
	// Schema is a file path to the JSONSchema that the JSON should validate
	// against.
	Schema string `yaml:"schema,omitempty"`
}

Expect represents one or more assertions about JSON data responses

func (*Expect) UnmarshalYAML

func (e *Expect) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML is a custom unmarshaler that ensures that JSONPath expressions contained in the Expect are valid.

Jump to

Keyboard shortcuts

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