schema

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidType   = errors.New("field type invalid")
	ErrRequiredField = errors.New("required field not set")
	ErrWrongPattern  = errors.New("doesn't match pattern")
)

Functions

func OptionValues

func OptionValues(options ...Option) utils.Set[string]

Types

type Field

type Field struct {
	Name        string   // column name in database.
	Label       string   // short name of field which will be shown in UI, if not set - [Field.Name] is used.
	Description string   // (markdown) optional description for the field, also shown in UI as help text.
	Required    bool     // make field as required: empty values will not be accepted as well as at least one option should be selected.
	Disabled    bool     // user input will be ignored, by field will be visible in UI. Doesn't apply for options.
	Hidden      bool     // user input will be ignored, field not visible in UI
	Default     string   // golang template expression for the default value.  Doesn't apply for options with [Field.Multiple].
	Type        Type     // (default [TypeString]) field type used for user input validation.
	Pattern     string   // optional regexp to validate content, applicable only for string type
	Options     []Option // allowed values. If [Field.Multiple] set, it acts as "any of", otherwise "one of".
	Multiple    bool     // allow picking multiple options. Column type in database MUST be ARRAY of corresponding type.
	Multiline   bool     // multiline input (for [TypeString] only)
}

func (*Field) Parse

func (f *Field) Parse(value string, locale *time.Location, render interface{ Render(string) (string, error) }) (any, error)

type Form

type Form struct {
	Name        string // unique form name, if not set - file name without extension will be used.
	Table       string // database table name
	Title       string // optional title for the form
	Description string // (markdown) optional description of the form
	Fields      []Field
	Webhooks    []Webhook
	Success     string // markdown message for success (also go template with available .Result)
	Failed      string // markdown message for failed (also go template with .Error)
}

func Default

func Default() Form

func FormsFromFS

func FormsFromFS(src fs.FS) ([]Form, error)

func FormsFromFile

func FormsFromFile(fs fs.FS, file string) ([]Form, error)

func FormsFromStream

func FormsFromStream(reader io.Reader) ([]Form, error)

type Option

type Option struct {
	Label string // label for UI
	Value string // if not set - Label is used, allowed value should match textual representation of form value
}

type Template

type Template template.Template

func (*Template) Render

func (t *Template) Render(data any) ([]byte, error)

func (*Template) UnmarshalText

func (t *Template) UnmarshalText(text []byte) error

type Type

type Type string
const (
	TypeString   Type = "string" // default, also for enums
	TypeInteger  Type = "integer"
	TypeFloat    Type = "float"
	TypeBoolean  Type = "boolean"
	TypeDate     Type = "date"
	TypeDateTime Type = "date-time"
)

func (Type) Is

func (t Type) Is(value string) bool

func (Type) Parse

func (t Type) Parse(value string, locale *time.Location) (any, error)

func (*Type) UnmarshalText

func (t *Type) UnmarshalText(text []byte) error

type Webhook

type Webhook struct {
	URL      string            // URL for POST webhook, where payload is JSON with fields from database column.
	Method   string            // HTTP method to perform, default is POST
	Retry    int               // maximum number of retries (0 or negative means no retries)
	Timeout  time.Duration     // request timeout
	Interval time.Duration     // interval between attempts (for non 2xx code)
	Headers  map[string]string // arbitrary headers (ex: Authorization)
	Message  *Template         // payload content, if not set - JSON representation of storage result
}

Jump to

Keyboard shortcuts

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