schema

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package schema handles schema loading and validation.

Index

Constants

View Source
const CurrentSchemaVersion = 1

CurrentSchemaVersion is the latest schema format version.

Variables

View Source
var BuiltinTypes = map[string]bool{
	"page":    true,
	"section": true,
	"date":    true,
}

BuiltinTypes are types that are always present and cannot be modified by users. These types have fixed definitions managed by Raven itself.

Functions

func BuiltinTypeNames

func BuiltinTypeNames() []string

BuiltinTypeNames returns a slice of all built-in type names.

func CreateDefault

func CreateDefault(vaultPath string) (bool, error)

CreateDefault creates a default schema.yaml file in the vault. Returns true if a new file was created, false if one already existed.

func IsBuiltinType

func IsBuiltinType(typeName string) bool

IsBuiltinType returns true if the type name is a built-in type that cannot be modified.

func IsValidFieldType

func IsValidFieldType(fieldType FieldType) bool

func ResolveTypeTemplateFile

func ResolveTypeTemplateFile(sch *Schema, typeName, templateID string) (string, error)

ResolveTypeTemplateFile resolves a template file path for a type.

Selection order: 1) If templateID is provided, resolve that template ID from type.templates. 2) Else, if default_template is set, resolve that template ID. 3) Else, fallback to legacy type.template (if present). 4) Else, no template ("").

func ValidFieldTypes

func ValidFieldTypes() string

func ValidateNameField

func ValidateNameField(typeDef *TypeDefinition) error

ValidateNameField checks that a type's name_field is valid. Returns an error if the name_field references a non-existent field or a non-string field.

func ValidateSchema

func ValidateSchema(sch *Schema) []string

ValidateSchema performs comprehensive validation of a schema. Returns a list of issues found.

func ValidateTraitValue

func ValidateTraitValue(def *TraitDefinition, value FieldValue) error

ValidateTraitValue validates a single trait value against the trait definition. It assumes a value is present (non-bare trait usage).

Types

type CoreTypeDefinition

type CoreTypeDefinition struct {
	// Templates lists template IDs from the schema-level templates map that this core type can use.
	Templates []string `yaml:"templates,omitempty"`
	// DefaultTemplate selects the template ID from Templates that is applied by default.
	DefaultTemplate string `yaml:"default_template,omitempty"`
}

CoreTypeDefinition defines supported configuration for a core type.

Core types are Raven-managed with fixed field definitions. Only explicitly-supported knobs are configurable via schema.yaml "core:".

type FieldDefinition

type FieldDefinition struct {
	Type     FieldType   `yaml:"type"`
	Required bool        `yaml:"required,omitempty"`
	Default  interface{} `yaml:"default,omitempty"`
	Values   []string    `yaml:"values,omitempty"` // For enum types
	Target   string      `yaml:"target,omitempty"` // For ref types
	// Description provides optional context for humans/agents about this field.
	Description string   `yaml:"description,omitempty"`
	Min         *float64 `yaml:"min,omitempty"`        // For number types
	Max         *float64 `yaml:"max,omitempty"`        // For number types
	Derived     string   `yaml:"derived,omitempty"`    // How to compute value
	Positional  bool     `yaml:"positional,omitempty"` // For traits: positional argument
}

FieldDefinition defines a field within a type or trait.

type FieldType

type FieldType string

FieldType represents the type of a field.

const (
	FieldTypeString        FieldType = "string"
	FieldTypeStringArray   FieldType = "string[]"
	FieldTypeNumber        FieldType = "number"
	FieldTypeNumberArray   FieldType = "number[]"
	FieldTypeURL           FieldType = "url"
	FieldTypeURLArray      FieldType = "url[]"
	FieldTypeDate          FieldType = "date"
	FieldTypeDateArray     FieldType = "date[]"
	FieldTypeDatetime      FieldType = "datetime"
	FieldTypeDatetimeArray FieldType = "datetime[]"
	FieldTypeEnum          FieldType = "enum"
	FieldTypeEnumArray     FieldType = "enum[]"
	FieldTypeBool          FieldType = "bool"
	FieldTypeBoolArray     FieldType = "bool[]"
	FieldTypeRef           FieldType = "ref"
	FieldTypeRefArray      FieldType = "ref[]"
)

type FieldValue

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

FieldValue represents a parsed field value.

func Array

func Array(items []FieldValue) FieldValue

Array creates an array FieldValue.

func Bool

func Bool(b bool) FieldValue

Bool creates a boolean FieldValue.

func Date

func Date(s string) FieldValue

Date creates a date FieldValue.

func Datetime

func Datetime(s string) FieldValue

Datetime creates a datetime FieldValue.

func NewFieldValue

func NewFieldValue(v interface{}) FieldValue

NewFieldValue creates a new FieldValue.

func Null

func Null() FieldValue

Null creates a null FieldValue.

func Number

func Number(n float64) FieldValue

Number creates a number FieldValue.

func Ref

func Ref(s string) FieldValue

Ref creates a reference FieldValue.

func String

func String(s string) FieldValue

String creates a string FieldValue.

func (FieldValue) AsArray

func (fv FieldValue) AsArray() ([]FieldValue, bool)

AsArray returns the value as an array, if possible.

func (FieldValue) AsBool

func (fv FieldValue) AsBool() (bool, bool)

AsBool returns the value as a boolean, if possible.

func (FieldValue) AsNumber

func (fv FieldValue) AsNumber() (float64, bool)

AsNumber returns the value as a number, if possible.

func (FieldValue) AsRef

func (fv FieldValue) AsRef() (string, bool)

AsRef returns the value as a reference path, if possible.

func (FieldValue) AsString

func (fv FieldValue) AsString() (string, bool)

AsString returns the value as a string, if possible.

func (FieldValue) IsDate

func (fv FieldValue) IsDate() bool

IsDate returns true if this is a date value (YYYY-MM-DD).

func (FieldValue) IsDatetime

func (fv FieldValue) IsDatetime() bool

IsDatetime returns true if this is a datetime value.

func (FieldValue) IsNull

func (fv FieldValue) IsNull() bool

IsNull returns true if the value is null.

func (FieldValue) IsRef

func (fv FieldValue) IsRef() bool

IsRef returns true if this is a reference value.

func (FieldValue) MarshalJSON

func (fv FieldValue) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (FieldValue) Raw

func (fv FieldValue) Raw() interface{}

Raw returns the underlying raw value.

type LoadResult

type LoadResult struct {
	Schema   *Schema
	Warnings []SchemaWarning
}

LoadResult contains the loaded schema and any warnings.

func LoadWithWarnings

func LoadWithWarnings(vaultPath string) (*LoadResult, error)

LoadWithWarnings loads the schema and returns any migration warnings.

type Schema

type Schema struct {
	Version   int                            `yaml:"version,omitempty"` // Schema format version
	Types     map[string]*TypeDefinition     `yaml:"types"`
	Core      map[string]*CoreTypeDefinition `yaml:"core,omitempty"`
	Traits    map[string]*TraitDefinition    `yaml:"traits"`
	Templates map[string]*TemplateDefinition `yaml:"templates,omitempty"`
}

Schema represents the complete schema definition loaded from schema.yaml.

func Load

func Load(vaultPath string) (*Schema, error)

Load loads the schema from a vault's schema.yaml file. Returns a default schema if the file doesn't exist.

func NewSchema

func NewSchema() *Schema

NewSchema creates a new schema with built-in types.

type SchemaWarning

type SchemaWarning struct {
	Message string
}

SchemaWarning represents a non-fatal schema issue.

type TemplateDefinition

type TemplateDefinition struct {
	// File is the template file path under directories.template (e.g., "templates/interview/technical.md").
	File string `yaml:"file"`
	// Description provides optional context for humans/agents.
	Description string `yaml:"description,omitempty"`
}

TemplateDefinition defines a schema-level template that can be bound to one or more types.

type TraitDefinition

type TraitDefinition struct {
	// Type is the value type (date, enum, string, boolean, etc.)
	// If empty or "boolean", the trait is a marker with no value.
	Type FieldType `yaml:"type,omitempty"`

	// Values lists valid values for enum types.
	Values []string `yaml:"values,omitempty"`

	// Default is the default value if none provided.
	Default interface{} `yaml:"default,omitempty"`
}

TraitDefinition defines a trait (@due, @priority, @highlight, etc.). Traits are single-valued annotations applied to content.

func (*TraitDefinition) IsBoolean

func (td *TraitDefinition) IsBoolean() bool

IsBoolean returns true if this trait is a boolean/marker trait.

type TypeDefinition

type TypeDefinition struct {
	Fields      map[string]*FieldDefinition `yaml:"fields"`
	DefaultPath string                      `yaml:"default_path,omitempty"`
	// Description provides optional context for humans/agents about this type's purpose.
	Description string `yaml:"description,omitempty"`
	// NameField specifies which field serves as the display name for this type.
	// When set, the title argument to `rvn new` will auto-populate this field.
	// The field must be a string type. If the field doesn't exist, it will be
	// auto-created as a required string field.
	NameField string `yaml:"name_field,omitempty"`
	// Template is a path to a template file (e.g., "templates/meeting.md").
	// Deprecated: use Schema.Templates + TypeDefinition.Templates + DefaultTemplate.
	Template string `yaml:"template,omitempty"`
	// Templates lists template IDs from the schema-level templates map that this type can use.
	Templates []string `yaml:"templates,omitempty"`
	// DefaultTemplate selects the template ID from Templates that is applied by default.
	// If empty, object creation proceeds without a template unless explicitly selected.
	DefaultTemplate string `yaml:"default_template,omitempty"`
}

TypeDefinition defines a type (person, meeting, project, etc.).

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError represents a field validation error.

func ValidateFields

func ValidateFields(fields map[string]FieldValue, fieldDefs map[string]*FieldDefinition, schema *Schema) []ValidationError

ValidateFields validates a set of fields against a type's field definitions.

func (ValidationError) Error

func (e ValidationError) Error() string

Jump to

Keyboard shortcuts

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