schema

package
v0.74.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 13 Imported by: 2

Documentation

Overview

Package schema provides a simple way to register and lookup schemas.

Index

Constants

View Source
const (
	// Version of the current version of the schema
	Version = "draft-0"
	// BaseURL is the base URL for all GOBL schemas
	BaseURL = "https://gobl.org/"
	// GOBL stores the base schema ID for GOBL, including current schema version.
	GOBL ID = BaseURL + Version
)
View Source
const (
	// Recommended defines the constant used in JSON Schema extensions
	// to define a list of recommended but not required fields.
	// This is leveraged in UIs to determine fields that should be show
	// by default but not required if left empty.
	Recommended = "recommended"
)

Variables

This section is empty.

Functions

func Insert added in v0.26.1

func Insert(id ID, data []byte) ([]byte, error)

Insert adds the provided schema ID to the JSON data provided.

func Register

func Register(base ID, objs ...interface{})

Register adds a new link between a schema ID and object to the global schema registry. This should be called for all GOBL models that will be included inside schema documents or included in an envelope document payload. The name of the object will be determined from the type of the object provided.

func RegisterIn

func RegisterIn(base ID, objs ...interface{})

RegisterIn will determine the anchor and add it to the base schema before adding to the global registry.

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase converts the provided string into snake case using dashes. This is useful for Schema IDs and definitions to be coherent with common JSON Schema examples.

func Type

func Type(id ID) reflect.Type

Type provides the type from a matching registered schema.

func Types

func Types() map[reflect.Type]ID

Types provides a complete map of types to schema IDs that have been registered.

Types

type Calculable added in v0.56.0

type Calculable interface {
	Calculate() error
}

Calculable defines the methods expected of a document payload that contains a `Calculate` method to be used to perform any additional calculations.

type Correctable added in v0.56.0

type Correctable interface {
	Correct(...Option) error
	CorrectionOptionsSchema() (interface{}, error)
}

Correctable defines the expected interface of a document that can be corrected.

type Error added in v0.56.0

type Error string

Error is used to define schema errors

const (
	// ErrUnknownSchema is returned when the schema has not been registered.
	ErrUnknownSchema Error = "unknown-schema"
)

func (Error) Error added in v0.56.0

func (e Error) Error() string

Error provides the error code

type ID

type ID string

ID contains the official schema URL.

const (
	// UnknownID is provided when the schema has not been registered
	UnknownID ID = ""
)

func Extract added in v0.26.1

func Extract(data []byte) (ID, error)

Extract attempts to Unmarshal the provided JSON document in order to extract the payload's Schema ID.

func List

func List() []ID

List of known schema IDs. Mainly used for debugging.

func Lookup

func Lookup(obj interface{}) ID

Lookup finds the objects schema ID, if set

func (ID) Add

func (id ID) Add(path string) ID

Add appends the provided path to the id, and removes any anchor data that might be there.

func (ID) Anchor

func (id ID) Anchor(name string) ID

Anchor either adds or replaces the anchor part of the schema URI.

func (ID) Base

func (id ID) Base() ID

Base removes any anchor information from the schema

func (ID) Interface

func (id ID) Interface() any

Interface attempts to determine the type by looking up the ID in the registered list of schemas, and providing an empty instance.

func (ID) String

func (id ID) String() string

String provides string version of ID

func (ID) Validate

func (id ID) Validate() error

Validate ensures the schema ID looks good.

type Identifiable added in v0.73.0

type Identifiable interface {
	GetUUID() uuid.UUID
	SetUUID(uuid.UUID)
}

Identifiable defines the methods expected of a document payload that contains a UUID. The `uuid` packages `Identify` struct can be embedded to satisfy this.

type Object added in v0.56.0

type Object struct {
	Schema ID `json:"$schema"`
	// contains filtered or unexported fields
}

Object helps handle json objects that must contain a schema to correctly identify the contents and ensuring that a `$schema` property is added automatically when marshalling back into JSON.

func NewObject added in v0.56.0

func NewObject(payload interface{}) (*Object, error)

NewObject instantiates an Object wrapper around the provided payload.

func (*Object) Calculate added in v0.56.0

func (d *Object) Calculate() error

Calculate will attempt to run the calculation method on the document payload. If the object implements the Identifiable interface, it will also ensure the UUID is set.

func (*Object) Clone added in v0.56.0

func (d *Object) Clone() (*Object, error)

Clone makes a copy of the document by serializing and deserializing the contents into a new document instance.

func (*Object) Correct added in v0.56.0

func (d *Object) Correct(opts ...Option) error

Correct will attempt to run the correction method on the document using some of the provided options.

func (*Object) CorrectionOptionsSchema added in v0.58.1

func (d *Object) CorrectionOptionsSchema() (interface{}, error)

CorrectionOptionsSchema provides a schema with the correction options available for the schema, if available.

func (*Object) Instance added in v0.56.0

func (d *Object) Instance() interface{}

Instance returns a prepared version of the document's content.

func (*Object) IsEmpty added in v0.56.0

func (d *Object) IsEmpty() bool

IsEmpty returns true if no payload has been set yet.

func (Object) JSONSchema added in v0.56.0

func (Object) JSONSchema() *jsonschema.Schema

JSONSchema returns a jsonschema.Schema instance.

func (*Object) MarshalJSON added in v0.56.0

func (d *Object) MarshalJSON() ([]byte, error)

MarshalJSON satisfies the json.Marshaler interface.

func (*Object) UUID added in v0.73.0

func (d *Object) UUID() uuid.UUID

UUID extracts the UUID from the payload using reflection. An empty id is returned if the payload does not have a UUID field.

func (*Object) UnmarshalJSON added in v0.56.0

func (d *Object) UnmarshalJSON(data []byte) error

UnmarshalJSON satisfies the json.Unmarshaler interface.

func (*Object) Validate added in v0.56.0

func (d *Object) Validate() error

Validate checks to ensure the document has everything it needs and will pass on the validation call to the payload.

func (*Object) ValidateWithContext added in v0.56.0

func (d *Object) ValidateWithContext(ctx context.Context) error

ValidateWithContext checks to ensure the document has everything it needs and will pass on the validation call to the payload.

type Option added in v0.56.0

type Option func(o interface{})

Option is a generic single function intended to be used for handling options to method calls.

Jump to

Keyboard shortcuts

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