validator

package
v0.0.0-...-e54aff3 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package validator contains types for validating Eiffel events in different ways, e.g. by checking that they adhere to their JSON schemas.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrSchemaMissing = errors.New("no schema found by any of the configured schema locators")

Functions

This section is empty.

Types

type BundledSchemaLocator

type BundledSchemaLocator struct{}

BundledSchemaLocator locates schemas from those built into the binary via this package.

func NewBundledSchemaLocator

func NewBundledSchemaLocator() *BundledSchemaLocator

func (*BundledSchemaLocator) GetSchema

func (bsl *BundledSchemaLocator) GetSchema(ctx context.Context, eventType string, version string, schemaURI string) (io.ReadCloser, error)

GetSchema returns the event's schema from the built-in official set of schemas. Returns (nil, nil) if no schema was found for the provided event type and version.

type HTTPGetter

type HTTPGetter interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPGetter is capable of downloading an HTTP resource via a GET request.

type MetaSchemaLocator

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

MetaSchemaLocator is a SchemaLocator implementation that downloads an event's schema from its non-empty meta.schemaURI member via HTTP(S).

func NewMetaSchemaLocator

func NewMetaSchemaLocator(getter HTTPGetter) *MetaSchemaLocator

func (*MetaSchemaLocator) GetSchema

func (msl *MetaSchemaLocator) GetSchema(ctx context.Context, eventType string, version string, schemaURI string) (io.ReadCloser, error)

GetSchema downloads the event's schema via HTTP(S) if schemaURI is non-empty. Returns (nil, nil) if schemaURI is empty or if the URI scheme isn't "http" or "https".

type SchemaLocator

type SchemaLocator interface {
	// GetSchema attempts to find and return a JSON schema that matches the given
	// input attributes. Returns (nil, nil) if no schema could be found and no
	// error occurred while reaching that conclusion.
	GetSchema(ctx context.Context, eventType string, version string, schemaURI string) (io.ReadCloser, error)
}

SchemaLocator defines a method that attempts to find the schema for a particular event based on the event's type, its version, and (if available) the schema URI from the meta.schemaURI member.

type SchemaValidationError

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

SchemaValidationError indicates that the event failed validation against the JSON schema.

func (*SchemaValidationError) Error

func (ve *SchemaValidationError) Error() string

func (*SchemaValidationError) Is

func (ve *SchemaValidationError) Is(target error) bool

type SchemaValidator

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

SchemaValidator is a Validator instance that locates a suitable JSON schema for an event and validates the event. Loaded schemas are cached indefinitely with the event type, event version, and schema URI (from the meta.schemaURI member) as the cache key.

Example
event, _ := eiffelevents.NewCompositionDefined()
event.Data.Name = "name-of-composition"

v := DefaultSet()
if err := v.Validate(context.Background(), []byte(event.String())); err != nil {
	fmt.Printf("Validation failed: %s", err)
	return
}
fmt.Println("Validation passed!")
Output:

Validation passed!

func NewSchemaValidator

func NewSchemaValidator(schemaLocators ...SchemaLocator) *SchemaValidator

func (*SchemaValidator) Validate

func (sv *SchemaValidator) Validate(ctx context.Context, event []byte) error

Validate runs through the configured schema locators to find one that has a schema for the provided event, and proceeds to validate the event against the schema. Returns an ErrSchemaMissing error if no schema could be located and SchemaValidationError if the validation fails.

type Validator

type Validator interface {
	Validate(ctx context.Context, event []byte) error
}

Validator is capable of applying some set of rules to validate the correctness of an event.

type ValidatorSet

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

ValidatorSet contains an ordered set of one or more Validator instances. The set can validate events against all validators in the set and require all validators to give a passing grade.

func DefaultSet

func DefaultSet() *ValidatorSet

DefaultSet returns the currently recommended set of validators, each with a default configuration.

func NewSet

func NewSet(validators ...Validator) *ValidatorSet

NewSet returns a ValidatorSet containing the specified Validator pointers.

func (*ValidatorSet) Add

func (vs *ValidatorSet) Add(validators ...Validator)

Add appends one or more validators to the current set.

func (*ValidatorSet) Validate

func (vs *ValidatorSet) Validate(ctx context.Context, event []byte) error

Validate loops over the Validator instances in the set and asks them to validate the given event. The loop will terminate upon the first validation error, i.e. all validators aren't guaranteed to be called.

Jump to

Keyboard shortcuts

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