schema

package
v0.0.0-...-a3fcc2a Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package schema provides utilities for loading and parsing XDB schemas from various file formats including JSON and YAML.

The package defines schema structures and supports loading schema definitions:

  • Def: Schema definition with metadata, fields, and validation rules
  • FieldDef: Individual field definition with type and constraints
  • Mode: Validation mode (flexible or strict)

Supported field types:

  • Scalar types (STRING, INTEGER, FLOAT, BOOLEAN, etc.)
  • Array types
  • Map types
  • Nested field paths

Example JSON schema:

{
  "name": "User",
  "description": "User record schema",
  "version": "1.0.0",
  "fields": [
    {
      "name": "name",
      "description": "User's full name",
      "type": "STRING"
    },
    {
      "name": "tags",
      "type": "ARRAY",
      "array_of": "STRING"
    }
  ]
}

Example YAML schema:

name: User
description: User record schema
version: 1.0.0
fields:
  - name: name
    description: User's full name
    type: STRING
  - name: tags
    type: ARRAY
    array_of: STRING

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidSchema is returned when schema format is invalid.
	ErrInvalidSchema = errors.New("[xdb/schema] invalid schema format")
	// ErrInvalidType is returned when a type definition is invalid.
	ErrInvalidType = errors.New("[xdb/schema] invalid type definition")
)
View Source
var (
	// ErrUnknownField is returned when a tuple has an attribute not defined in the schema.
	ErrUnknownField = errors.New("[xdb/schema] unknown field")

	// ErrTypeMismatch is returned when a tuple value type does not match the field type.
	ErrTypeMismatch = errors.New("[xdb/schema] type mismatch")
)

Functions

func ValidateTuples

func ValidateTuples(def *Def, tuples []*core.Tuple) error

ValidateTuples validates tuples against the schema definition.

func WriteToJSON

func WriteToJSON(schema *Def) ([]byte, error)

WriteToJSON writes a schema to JSON format.

func WriteToYAML

func WriteToYAML(schema *Def) ([]byte, error)

WriteToYAML writes a schema to YAML format.

Types

type Def

type Def struct {
	// NS is the namespace this schema belongs to.
	NS *core.NS

	// Name is the schema name.
	Name string

	// Description provides human-readable documentation.
	Description string

	// Version tracks schema evolution (e.g., "1.0.0").
	Version string

	// Mode defines how records are validated against the schema.
	Mode Mode

	// Fields is a list of field schemas.
	// Use hierarchical paths for nested fields (e.g., "profile.email").
	Fields []*FieldDef
}

Def defines the structure and validation rules for records. It provides metadata, field definitions, and record-level constraints.

func LoadFromJSON

func LoadFromJSON(data []byte) (*Def, error)

LoadFromJSON loads a schema from JSON data.

func LoadFromYAML

func LoadFromYAML(data []byte) (*Def, error)

LoadFromYAML loads a schema from YAML data.

func (*Def) AddFields

func (s *Def) AddFields(fields ...*FieldDef)

AddFields adds field definitions to the schema, skipping duplicates.

func (*Def) Clone

func (s *Def) Clone() *Def

Clone returns a deep copy of the Def.

func (*Def) GetField

func (s *Def) GetField(path string) *FieldDef

GetField returns the field definition for the given path.

type FieldDef

type FieldDef struct {
	// Name is the field name.
	Name string

	// Description provides human-readable documentation.
	Description string

	// Type specifies the field's full type
	Type core.Type
}

FieldDef defines the definition for a single field.

func InferFields

func InferFields(def *Def, tuples []*core.Tuple) ([]*FieldDef, error)

InferFields returns new field definitions for attributes not yet in the schema.

func (*FieldDef) Clone

func (f *FieldDef) Clone() *FieldDef

Clone returns a deep copy of the FieldDef.

func (*FieldDef) Equals

func (f *FieldDef) Equals(other *FieldDef) bool

Equals returns true if this FieldDef is equal to the other FieldDef.

type Mode

type Mode string

Mode defines how records are validated against the schema.

const (
	// ModeFlexible allows records to have arbitrary attributes
	// without predefined structure.
	ModeFlexible Mode = "flexible"

	// ModeStrict requires records to have attributes
	// defined in the schema.
	ModeStrict Mode = "strict"

	// ModeDynamic automatically infers and adds new fields.
	ModeDynamic Mode = "dynamic"
)

Jump to

Keyboard shortcuts

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