Documentation
¶
Overview ¶
Package schema provides a simple way to register and lookup schemas.
Index ¶
- Constants
- func Insert(id ID, data []byte) ([]byte, error)
- func Register(base ID, objs ...interface{})
- func RegisterIn(base ID, objs ...interface{})
- func ToSnakeCase(str string) string
- func Type(id ID) reflect.Type
- func Types() map[reflect.Type]ID
- type Calculable
- type Correctable
- type Error
- type ID
- type Identifiable
- type Object
- func (d *Object) Calculate() error
- func (d *Object) Clone() (*Object, error)
- func (d *Object) Correct(opts ...Option) error
- func (d *Object) CorrectionOptionsSchema() (any, error)
- func (d *Object) Instance() interface{}
- func (d *Object) IsEmpty() bool
- func (Object) JSONSchema() *jsonschema.Schema
- func (d *Object) MarshalJSON() ([]byte, error)
- func (d *Object) Replicate() error
- func (d *Object) UUID() uuid.UUID
- func (d *Object) UnmarshalJSON(data []byte) error
- func (d *Object) Validate() error
- func (d *Object) ValidateWithContext(ctx context.Context) error
- type Option
- type Replicable
Constants ¶
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 )
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 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 ¶
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.
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
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" )
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
Extract attempts to Unmarshal the provided JSON document in order to extract the payload's Schema ID.
func (ID) Add ¶
Add appends the provided path to the id, and removes any anchor data that might be there.
type Identifiable ¶ added in v0.73.0
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
NewObject instantiates an Object wrapper around the provided payload.
func (*Object) Calculate ¶ added in v0.56.0
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
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
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
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) 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
MarshalJSON satisfies the json.Marshaler interface.
func (*Object) Replicate ¶ added in v0.75.0
Replicate will attempt to clone and run the Replicate method of the object if it has one.
func (*Object) UUID ¶ added in v0.73.0
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
UnmarshalJSON satisfies the json.Unmarshaler interface.
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.
type Replicable ¶ added in v0.75.0
type Replicable interface {
Replicate() error
}
Replicable defines the methods expected of a document payload that can be replicated.