schema

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: MPL-2.0 Imports: 6 Imported by: 0

README

schema

Module for JSON Schema validation

Setup
import (
    "fmt"
    "os"

    "github.com/rs/zerolog"

    "gitlab.com/msts-public/general/gomods/schema"
)

func main() {
    l := zerolog.New(os.Stdout).With().Timestamp().Logger()

    config := schema.Config{
        SchemaURI: "/full/path/test.schema.json",
        Logger:    l,
        Defs:      []string{"request","response"}
    }

    s, err := schema.NewSchema(config)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Validates the data against the schema
    s.SetSchema("request")
    err = s.Validate(`{"first_name": "John"}`)
    if err != nil {
        fmt.Println(err)
    }
}
Config
type Config struct {
	Logger    zerolog.Logger // Zero logger instance
	SchemaURI string         // URI of schema definition. If scheme is not specified, it will default to using 'file' scheme
	Defs      []string       // Definitions within the schema. If empty, will default to "root"
}
References:
  • github.com/rs/zerolog
  • github.com/xeipuuv/gojsonschema

Documentation

Index

Constants

View Source
const (
	ErrorCodeInvalidURI       = "invalid_schema_uri"
	ErrorCodeSchemaNotLoaded  = "schema_not_loaded"
	ErrorCodeInvalidSchemaDef = "invalid_schema_definition"
	ErrorCodeInvalidData      = "invalid_data"
)

package constants

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Logger    zerolog.Logger
	SchemaURI string
	Defs      []string
}

Config -

type Error added in v0.1.2

type Error struct {
	Type   string
	Detail string
}

Error - non-schema validation errors

func NewError added in v0.1.2

func NewError(typ string, err error) Error

NewError - general schema error

func (Error) Error added in v0.1.2

func (e Error) Error() string

Error - returns a formatted error string for the schema error

type Schema

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

Schema -

func NewSchema

func NewSchema(c Config) (*Schema, error)

NewSchema - returns a new schema validator which loads and compiles the schema file supplied in the config

func (*Schema) Init added in v0.1.1

func (s *Schema) Init(l zerolog.Logger) error

Init -

func (*Schema) SetSchema

func (s *Schema) SetSchema(def string) error

SetSchema - sets the active schema definition for validation if definitions are supplied in the config and is not root.

func (*Schema) Validate

func (s *Schema) Validate(data interface{}) error

Validate - validate data against the schema returning schema validation errors

type ValidationError added in v0.1.2

type ValidationError struct {
	Type     string
	Property string
	Value    string
	Detail   string
	Path     string
	// contains filtered or unexported fields
}

ValidationError - schema validation errors

func NewSchemaJSONError

func NewSchemaJSONError(err gojsonschema.ResultError) ValidationError

NewSchemaJSONError - JSON schema validation errors

func NewValidationError added in v0.1.2

func NewValidationError(typ, property, value, detail, path string) ValidationError

NewValidationError -

func (ValidationError) Error added in v0.1.2

func (ve ValidationError) Error() string

Error - returns a formatted error string for the schema validation error

type ValidationErrorCollection added in v0.1.2

type ValidationErrorCollection []ValidationError

ValidationErrorCollection - collection of ValidationError types

func NewValidationErrorCollection added in v0.1.2

func NewValidationErrorCollection() ValidationErrorCollection

NewValidationErrorCollection - returns a collection of ValidationError types

func (*ValidationErrorCollection) Add added in v0.1.2

Add - add error to schema validation error collection

func (ValidationErrorCollection) Error added in v0.1.2

func (vec ValidationErrorCollection) Error() string

Error - returns a formatted error string from the schema validation error collection

Jump to

Keyboard shortcuts

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