schema

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package schema is part of the GoFastr framework. See https://github.com/DonaldMurillo/gofastr for documentation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONSchema

func JSONSchema(fields []Field) map[string]any

JSONSchema generates a JSON Schema draft-07 object from a slice of Field definitions. The returned map can be marshaled directly to JSON.

func Validate

func Validate(field Field, value any) error

Validate checks a single value against a Field definition. Returns nil on success or a descriptive error on failure.

Types

type AutoGenerate

type AutoGenerate int

AutoGenerate controls how a field value is automatically produced server-side.

const (
	AutoNone      AutoGenerate = iota // not auto-generated
	AutoUUID                          // UUID v4
	AutoTimestamp                     // current RFC 3339 timestamp
	AutoIncrement                     // auto-incrementing integer
)

type Field

type Field struct {
	Name         string       // field name (must be unique within a Schema)
	Type         FieldType    // field type
	Required     bool         // value must be present and non-zero
	Unique       bool         // value must be unique across entities
	Default      any          // default value when omitted
	AutoGenerate AutoGenerate // server-side auto-generation strategy
	ReadOnly     bool         // excluded from create/update request bodies
	Hidden       bool         // excluded from API responses
	Max          *float64     // upper bound (numeric max / string max-length)
	Min          *float64     // lower bound (numeric min / string min-length)
	Pattern      string       // regex pattern for string validation
	Values       []string     // allowed values for Enum
	To           string       // target entity name for Relation
	Many         bool         // has-many relation flag
	RawType      string       // explicit SQL column type, overrides Type→SQL mapping (e.g. "NUMERIC(10,2)", "INET")
}

Field defines a single field in an entity schema.

type FieldType

type FieldType int

FieldType enumerates all supported field types in a GoFastr entity schema.

const (
	String    FieldType = iota // short string
	Text                       // long text / textarea
	Int                        // integer
	Float                      // floating point
	Decimal                    // fixed-precision decimal (stored as string)
	Bool                       // boolean
	Enum                       // one of a fixed set of strings
	UUID                       // UUID v4 identifier
	Timestamp                  // RFC 3339 timestamp
	Date                       // calendar date (2006-01-02)
	JSON                       // arbitrary JSON blob
	Relation                   // reference to another entity
	Image                      // image URL or path
	File                       // file URL or path
)

type Schema

type Schema struct {
	Fields []Field
}

Schema is an ordered collection of Fields with convenience helpers.

func (Schema) AutoGeneratedFields

func (s Schema) AutoGeneratedFields() []Field

AutoGeneratedFields returns fields that are auto-generated server-side.

func (Schema) FieldByName

func (s Schema) FieldByName(name string) (Field, bool)

FieldByName returns the field with the given name and true, or the zero Field and false if not found.

func (Schema) Names

func (s Schema) Names() []string

Names returns the ordered list of field names.

func (Schema) RequiredFields

func (s Schema) RequiredFields() []Field

RequiredFields returns only the required fields.

func (Schema) VisibleFields

func (s Schema) VisibleFields() []Field

VisibleFields returns fields that are included in API responses (not hidden).

func (Schema) WritableFields

func (s Schema) WritableFields() []Field

WritableFields returns fields that can be set by the client (not auto-generated, not read-only).

type ValidationResult

type ValidationResult struct {
	Valid  bool
	Errors map[string][]string // field name → list of error messages
}

ValidationResult holds the outcome of validating a full map of values.

func ValidateAll

func ValidateAll(s Schema, values map[string]any) ValidationResult

ValidateAll validates a map of values against every Field in the Schema. Unknown keys are ignored; missing required fields produce errors.

func ValidatePartial

func ValidatePartial(s Schema, values map[string]any) ValidationResult

ValidatePartial validates only the fields that are present in `values`. Missing fields — required or not — are not reported. Use this for partial-update flows (PUT/PATCH with only the fields the caller wants to change) so a sparse update doesn't fail because some other required field happens not to be in the body.

Jump to

Keyboard shortcuts

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