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 ¶
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") )
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 ¶
ValidateTuples validates tuples against the schema definition.
func WriteToJSON ¶
WriteToJSON writes a schema to JSON format.
func WriteToYAML ¶
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 ¶
LoadFromJSON loads a schema from JSON data.
func LoadFromYAML ¶
LoadFromYAML loads a schema from YAML data.
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 ¶
InferFields returns new field definitions for attributes not yet in the schema.
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" )