Documentation
¶
Index ¶
- Constants
- Variables
- func NormalizeValue(f FieldSchema, value any) (any, error)
- func ValidateField(f FieldSchema) error
- func ValidateFieldName(name string) error
- func ValidateFieldSlug(slug string) error
- func ValidateFieldType(t FieldType) error
- func ValidateFields(fields []FieldSchema) error
- func ValidateGalleryURL(rawURL string) error
- func ValidateMaxItems(t FieldType, maxItems *int) error
- func ValidateMaxLength(t FieldType, maxLength *int) error
- func ValidateNumberRange(t FieldType, min, max *float64) error
- func ValidateSelectOptions(t FieldType, options []string) error
- type FieldSchema
- type FieldType
Constants ¶
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).
const PostScriptSlug = "post_script"
PostScriptSlug is the reserved custom-field slug rendering verbatim at post end.
Variables ¶
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") )
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 ¶
ValidateFieldName requires a non-blank name within length limits.
func ValidateFieldSlug ¶
ValidateFieldSlug requires a lowercase snake_case slug within limits.
func ValidateFieldType ¶
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
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
ValidateMaxItems allows max_items on list and gallery fields only, and requires a positive value when present.
func ValidateMaxLength ¶
ValidateMaxLength allows max_length on text and textarea fields (the whole value) and on list fields (per item); other types reject it.
func ValidateNumberRange ¶
ValidateNumberRange rejects min > max on number fields; other types and absent bounds pass.
func ValidateSelectOptions ¶
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" )