customfield

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 7 Imported by: 6 modules, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	DefaultMaxItemsList    = 100
	DefaultMaxItemsGallery = 50
)

Default caps for the array types when the schema sets no max_items. They bound the stored JSON; an explicit max_items overrides them (and must be positive — see ValidateMaxItems).

View Source
const PostScriptSlug = "post_script"

PostScriptSlug is the reserved custom-field slug rendering verbatim at post end.

Variables

View Source
var (
	// ErrItemNotAllowed is returned when a multiselect value carries an item
	// outside the configured options.
	ErrItemNotAllowed = errors.New("item must be one of the configured options")
	// ErrItemEmpty is returned when a list value carries an empty item.
	ErrItemEmpty = errors.New("items must not be empty")
	// ErrItemNotString is returned when a list or multiselect item is not a
	// JSON string.
	ErrItemNotString = errors.New("items must be strings")
	// ErrItemLimitExceeded is returned when an array value carries more items
	// than the field allows.
	ErrItemLimitExceeded = errors.New("too many items")
	// ErrGalleryItemInvalid is returned when a gallery item is not an object
	// with a url (and optional alt) string pair.
	ErrGalleryItemInvalid = errors.New("gallery items must be objects with a url and an optional alt string")
	// ErrGalleryURLInvalid is returned when a gallery item URL is neither an
	// http(s) URL nor a root-relative media path.
	ErrGalleryURLInvalid = errors.New("gallery item url must be an http(s) URL or a root-relative path")
)
View Source
var (
	ErrFieldNameRequired  = errors.New("field name is required and must be between 1 and 200 characters")
	ErrFieldSlugInvalid   = errors.New("field slug must be between 1 and 200 characters and contain only lowercase letters, numbers, and underscores")
	ErrFieldTypeInvalid   = errors.New("field type must be one of: text, textarea, number, date, datetime, email, url, select, checkbox, multiselect, list, gallery")
	ErrSelectRequiresOpts = errors.New("select fields require an options list")
	ErrSelectEmptyOption  = errors.New("select field options must not contain empty or blank strings")
	ErrNumberMinGTMax     = errors.New("min cannot be greater than max")
	ErrMaxLengthInvalid   = errors.New("max_length must be greater than 0 and is only valid for text, textarea, and list fields")
	ErrMaxItemsInvalid    = errors.New("max_items must be greater than 0 and is only valid for list and gallery fields")
	ErrDuplicateFieldSlug = errors.New("duplicate field slug")
)

Functions

func NormalizeValue added in v1.1.0

func NormalizeValue(f FieldSchema, value any) (any, error)

NormalizeValue canonicalizes and validates a custom-field value for the array types, accepting the legacy single-string forms so switching a field's type in config stays a data-compatible migration:

  • multiselect: comma-separated string → []any{string…}; array values are checked against the configured options.
  • list: newline-separated string → []any{string…} (blank lines dropped); per-item max_length and max_items enforced.
  • gallery: one-URL-per-line string → []any{{url, alt}}; items are {url, alt} objects with an http(s) or root-relative url.

Canonical (already-array) input is validated and returned unchanged in shape; scalar field types pass through untouched (their validation lives in the content service). The returned value is always JSON-marshalable in the canonical storage shape: []any of strings, or []any of map[string]any{"url","alt"} for gallery.

func ValidateField

func ValidateField(f FieldSchema) error

ValidateField checks one field schema: name, slug, type, options, range, length, and item-count constraints.

func ValidateFieldName

func ValidateFieldName(name string) error

ValidateFieldName requires a non-blank name within length limits.

func ValidateFieldSlug

func ValidateFieldSlug(slug string) error

ValidateFieldSlug requires a lowercase snake_case slug within limits.

func ValidateFieldType

func ValidateFieldType(t FieldType) error

ValidateFieldType rejects unknown field types.

func ValidateFields

func ValidateFields(fields []FieldSchema) error

ValidateFields checks every schema and rejects duplicate slugs.

func ValidateGalleryURL added in v1.1.0

func ValidateGalleryURL(rawURL string) error

ValidateGalleryURL accepts http(s) URLs and root-relative media paths — the same shapes the media pipeline and sanitizer allowlists use. Empty is rejected: callers that allow absent URLs check for empty first.

func ValidateMaxItems added in v1.1.0

func ValidateMaxItems(t FieldType, maxItems *int) error

ValidateMaxItems allows max_items on list and gallery fields only, and requires a positive value when present.

func ValidateMaxLength

func ValidateMaxLength(t FieldType, maxLength *int) error

ValidateMaxLength allows max_length on text and textarea fields (the whole value) and on list fields (per item); other types reject it.

func ValidateNumberRange

func ValidateNumberRange(t FieldType, min, max *float64) error

ValidateNumberRange rejects min > max on number fields; other types and absent bounds pass.

func ValidateSelectOptions

func ValidateSelectOptions(t FieldType, options []string) error

ValidateSelectOptions requires non-blank options for select and multiselect fields; other types ignore options.

Types

type FieldSchema

type FieldSchema struct {
	Name      string    `json:"name" toml:"name"`
	Slug      string    `json:"slug" toml:"slug"`
	Type      FieldType `json:"type" toml:"type"`
	Required  bool      `json:"required,omitempty" toml:"required,omitempty"`
	Options   []string  `json:"options,omitempty" toml:"options,omitempty"`
	Min       *float64  `json:"min,omitempty" toml:"min,omitempty"`
	Max       *float64  `json:"max,omitempty" toml:"max,omitempty"`
	MaxLength *int      `json:"maxLength,omitempty" toml:"max_length,omitempty"`
	MaxItems  *int      `json:"maxItems,omitempty" toml:"max_items,omitempty"`
}

FieldSchema describes one custom field: identity, type, and constraints.

type FieldType

type FieldType string

FieldType names a custom-field value kind.

const (
	FieldTypeText     FieldType = "text"
	FieldTypeTextarea FieldType = "textarea"
	FieldTypeNumber   FieldType = "number"
	FieldTypeDate     FieldType = "date"
	FieldTypeDatetime FieldType = "datetime"
	FieldTypeEmail    FieldType = "email"
	FieldTypeUrl      FieldType = "url"
	FieldTypeSelect   FieldType = "select"
	FieldTypeCheckbox FieldType = "checkbox"
	// FieldTypeMultiselect is an ordered array of strings drawn from the
	// configured options — the many-values sibling of select.
	FieldTypeMultiselect FieldType = "multiselect"
	// FieldTypeList is an ordered array of free-form strings (specs,
	// ingredients, step lists) — the general-purpose string array.
	FieldTypeList FieldType = "list"
	// FieldTypeGallery is an ordered array of media items ({url, alt}) —
	// references into the media library rendered as a captioned image
	// collection.
	FieldTypeGallery FieldType = "gallery"
)

Jump to

Keyboard shortcuts

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