Documentation
¶
Overview ¶
Package activemodel is a pure-Go (no cgo), MRI-faithful reimplementation of Ruby on Rails' ActiveModel, targeting the observable behaviour of the activemodel gem on MRI 4.0.5.
This v0.1 foundation ships the Validations + Errors + Naming core — the part of ActiveModel that a model object leans on to describe itself and to report why it is invalid:
- Naming (Name) — ActiveModel::Naming / ActiveModel::Name: derive the singular/plural/element/human/collection/param_key/route_key/i18n_key of a model class name via go-ruby-activesupport's Inflector, byte-for-byte with Rails.
- Errors, Error — ActiveModel::Errors / ActiveModel::Error: add, lookup, where/added?/include?, messages/details, and byte-faithful full_messages ("Name can't be blank") with %{...} interpolation and the complete default-message table.
- Validations — the ActiveModel::Validations engine: the standard validators (presence, absence, length, format, inclusion, exclusion, numericality, confirmation, acceptance), custom validate blocks, validates_each, conditional if/unless/on, and allow_nil/allow_blank, with the exact Rails error types and messages.
- Validator, EachValidator — the base contracts for writing custom validators, mirroring ActiveModel::Validator / EachValidator.
Because Go has no open classes, the model object is reached through two small seams — Attr (attribute get/set) and Dispatcher (call a method / test respond to) — combined in the Model interface. A host such as go-embedded-ruby plugs its own object behind them; the tests plug a trivial map-backed fake.
See the README for the roadmap covering the deferred subsystems (Attributes typecasting, Dirty tracking, Callbacks, Serialization, SecurePassword, and Translation/i18n integration).
Index ¶
- Constants
- type AcceptanceOptions
- type Attr
- type Condition
- type Conditions
- type ConfirmationOptions
- type Dispatcher
- type EachValidator
- type Error
- type Errors
- func (es *Errors) Add(attribute string, typ any, opts map[string]any) *Error
- func (es *Errors) Added(attribute string, typ any, opts map[string]any) bool
- func (es *Errors) Any() bool
- func (es *Errors) AttributeNames() []string
- func (es *Errors) Clear()
- func (es *Errors) Details() map[string][]map[string]any
- func (es *Errors) Each(fn func(*Error))
- func (es *Errors) Empty() bool
- func (es *Errors) Entries() []*Error
- func (es *Errors) FullMessages() []string
- func (es *Errors) FullMessagesFor(attribute string) []string
- func (es *Errors) Get(attribute string) []string
- func (es *Errors) Include(attribute string) bool
- func (es *Errors) Messages() map[string][]string
- func (es *Errors) MessagesFor(attribute string) []string
- func (es *Errors) OfKind(attribute string, typ any) bool
- func (es *Errors) Size() int
- func (es *Errors) Where(attribute string, typ any, opts map[string]any) []*Error
- type FormatOptions
- type LengthOptions
- type MembershipOptions
- type Model
- type Name
- type NumRange
- type NumericalityOptions
- type Options
- type Range
- type Symbol
- type Validations
- func (v *Validations) Invalid(m Model) (bool, *Errors)
- func (v *Validations) InvalidContext(m Model, context string) (bool, *Errors)
- func (v *Validations) ModelName() Name
- func (v *Validations) Valid(m Model) (bool, *Errors)
- func (v *Validations) ValidContext(m Model, context string) (bool, *Errors)
- func (v *Validations) Validate(c Conditions, block func(m Model, e *Errors))
- func (v *Validations) Validates(attrs []string, opts Options)
- func (v *Validations) ValidatesEach(attrs []string, c Conditions, ...)
- func (v *Validations) ValidatesEachWith(attrs []string, validator EachValidator, c Conditions)
- func (v *Validations) ValidatesWith(validator Validator, c Conditions)
- type Validator
Constants ¶
const Version = "0.1.0"
Version is the module version.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AcceptanceOptions ¶
AcceptanceOptions configures the acceptance validator. Accept defaults to [true, "1"]; acceptance skips nil values (allow_nil defaults true in Rails).
type Attr ¶
Attr is the attribute-access seam: read and write a model attribute by name. It mirrors the Ruby attr reader/writer pair a validated object exposes, and in particular ActiveModel's read_attribute_for_validation. A host such as go-embedded-ruby plugs its own object here; the tests plug a map-backed fake.
type Condition ¶
Condition is a single if:/unless: predicate. Method names a model method resolved through the Dispatcher seam (a Ruby symbol condition); otherwise Proc is called (a Ruby proc/lambda). Either way the result is judged by Ruby truthiness.
func MethodCond ¶
MethodCond builds a symbol condition naming a model method.
type Conditions ¶
type Conditions struct {
If []Condition
Unless []Condition
On []string
AllowNil bool
AllowBlank bool
}
Conditions is the shared control block for a validator: if:/unless:/on: plus allow_nil:/allow_blank:.
type ConfirmationOptions ¶
ConfirmationOptions configures the confirmation validator. CaseSensitive defaults to true.
type Dispatcher ¶
Dispatcher is the method-call seam for behaviour that is not plain attribute access: evaluating a symbol condition (an if:/unless: given as a method name), or any host method a validation refers to by name. Call invokes the method and returns its Ruby result; RespondTo reports whether the model responds to it (used exactly where ActiveModel guards on respond_to?).
type EachValidator ¶
EachValidator is ActiveModel::EachValidator: a custom validator invoked once per attribute with its value (after allow_nil/allow_blank filtering).
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is ActiveModel::Error: a single validation error bound to an attribute, with a (Symbol or literal-String) type and its options.
func (*Error) Details ¶
Details is ActiveModel::Error#details: {error: type, ...options} minus the callback and message options.
func (*Error) FullMessage ¶
FullMessage is ActiveModel::Error#full_message: "%{attribute} %{message}", except a :base error is its message alone.
func (*Error) Match ¶
Match is ActiveModel::Error#match?: same attribute, optionally the same type, and every supplied option equal.
func (*Error) Message ¶
Message is ActiveModel::Error#message: a literal String type verbatim, or the generated, interpolated message for a Symbol type.
type Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
Errors is ActiveModel::Errors: the ordered collection of an object's errors.
func NewErrors ¶
NewErrors builds an Errors bound to a model and its Name (used for the %{model} interpolation and full_message attribute humanization).
func (*Errors) Add ¶
Add is ActiveModel::Errors#add: append an error on attribute with the given type and options and return it. A nil type defaults to Symbol("invalid").
func (*Errors) Added ¶
Added is ActiveModel::Errors#added?: for a Symbol type, whether an error strictly matches attribute/type/options; for a literal String type, whether that message is present on the attribute.
func (*Errors) AttributeNames ¶
Attribute names present in the collection, in first-seen order (Errors#attribute_names).
func (*Errors) FullMessages ¶
FullMessages is Errors#full_messages: every error's full message, in order.
func (*Errors) FullMessagesFor ¶
FullMessagesFor is Errors#full_messages_for(attribute).
func (*Errors) Get ¶
Get is ActiveModel::Errors#[]: the message strings on the attribute (an alias for MessagesFor).
func (*Errors) Include ¶
Include reports whether any error is on attribute (Errors#include? / #has_key?).
func (*Errors) MessagesFor ¶
MessagesFor is Errors#messages_for(attribute): the message strings on the attribute, in order.
type FormatOptions ¶
FormatOptions configures the format validator (validates format:). With must match; Without must not.
type LengthOptions ¶
type LengthOptions struct {
Minimum *int
Maximum *int
Is *int
In *Range
Message any
TooLong any
TooShort any
WrongLength any
}
LengthOptions configures the length validator (validates length:). Minimum, Maximum and Is are optional; In is the range form. The per-key message overrides (TooLong/TooShort/WrongLength) and Message mirror Rails.
type MembershipOptions ¶
MembershipOptions configures inclusion/exclusion (validates inclusion:/ exclusion:): either a discrete In set or a numeric Range.
type Model ¶
type Model interface {
Attr
Dispatcher
}
Model is the object under validation: the two seams combined.
type Name ¶
type Name struct {
// Name is the class name as given, e.g. "Person" or "Admin::User".
Name string
// Singular is the underscored, "/"-flattened name: "person", "admin_user".
Singular string
// Plural pluralizes Singular: "people", "admin_users".
Plural string
// Element is the underscored, demodulized name: "person", "user".
Element string
// Human is the humanized Element: "Person", "User".
Human string
// Collection is the tableized name: "people", "admin/users".
Collection string
// ParamKey is the key for params: Singular (or the unnamespaced singular).
ParamKey string
// RouteKey is the plural route key ("people"), with "_index" appended for an
// uncountable name.
RouteKey string
// SingularRouteKey singularizes RouteKey.
SingularRouteKey string
// I18nKey is the underscored name used as an i18n scope key.
I18nKey string
}
Name is ActiveModel::Name: the bundle of conventional names Rails derives from a model class name (and an optional namespace module name). Every field is computed through go-ruby-activesupport's Inflector, byte-for-byte with Rails' ActiveModel::Name#initialize.
func NewName ¶
NewName builds the Name for a class name with no namespace, mirroring ActiveModel::Name.new(klass) where klass.name == name.
func NewNamespacedName ¶
NewNamespacedName builds the Name for a class name nested in a namespace module, mirroring ActiveModel::Name.new(klass, namespace). namespace is the module's own name (e.g. "Admin"); an empty namespace means none.
type NumRange ¶
NumRange is a numeric range used by inclusion/exclusion in: a Ruby Range. When ExcludeEnd is set it models the "..." (end-exclusive) form.
type NumericalityOptions ¶
type NumericalityOptions struct {
OnlyInteger bool
GreaterThan any
GreaterThanOrEqualTo any
EqualTo any
LessThan any
LessThanOrEqualTo any
OtherThan any
Odd bool
Even bool
Message any
}
NumericalityOptions configures the numericality validator. Each comparison bound may be a literal number, a func(Model) any (a Ruby proc), or a Symbol (a method name resolved through the Dispatcher seam).
type Options ¶
type Options struct {
Presence bool
Absence bool
Length *LengthOptions
Format *FormatOptions
Inclusion *MembershipOptions
Exclusion *MembershipOptions
Numericality *NumericalityOptions
Confirmation *ConfirmationOptions
Acceptance *AcceptanceOptions
Message any
AllowNil bool
AllowBlank bool
If []Condition
Unless []Condition
On []string
}
Options is the bundle passed to Validates: which standard validators to apply to the attribute(s), plus the shared control keys. It mirrors the Ruby hash `validates :attr, presence: true, length: {...}, if: ..., allow_blank: true`.
type Range ¶
type Range struct{ Min, Max int }
Range is an inclusive integer range used by length in:/within:.
type Symbol ¶
type Symbol string
Symbol is a Ruby Symbol surfaced to Go. ActiveModel distinguishes a Symbol error type (a key looked up in the message table and interpolated) from a String error type (a literal message returned verbatim). Passing a Go string to Errors.Add / a :message option therefore means a *literal* Ruby String, while passing a Symbol("blank") means the Ruby Symbol :blank — exactly the MRI distinction in ActiveModel::Error#message.
type Validations ¶
type Validations struct {
// contains filtered or unexported fields
}
Validations is the ActiveModel::Validations engine for one model class: it holds the registered validators and the model's Name.
func New ¶
func New(name Name) *Validations
New builds a Validations bound to a model Name (used for %{model} and the Errors it produces).
func (*Validations) Invalid ¶
func (v *Validations) Invalid(m Model) (bool, *Errors)
Invalid runs invalidation in the default context.
func (*Validations) InvalidContext ¶
func (v *Validations) InvalidContext(m Model, context string) (bool, *Errors)
InvalidContext is ActiveModel::Validations#invalid? in the given context.
func (*Validations) ModelName ¶
func (v *Validations) ModelName() Name
ModelName returns the model's Name (ActiveModel::Naming#model_name).
func (*Validations) Valid ¶
func (v *Validations) Valid(m Model) (bool, *Errors)
Valid runs validation in the default (empty) context.
func (*Validations) ValidContext ¶
func (v *Validations) ValidContext(m Model, context string) (bool, *Errors)
ValidContext is ActiveModel::Validations#valid? in the given context: run every validator against the model, collecting errors, and report whether none were added.
func (*Validations) Validate ¶
func (v *Validations) Validate(c Conditions, block func(m Model, e *Errors))
Validate is ActiveModel::Validations#validate with a block: register a whole-record custom validation.
func (*Validations) Validates ¶
func (v *Validations) Validates(attrs []string, opts Options)
Validates is ActiveModel::Validations#validates: register the configured standard validators for the given attribute(s).
func (*Validations) ValidatesEach ¶
func (v *Validations) ValidatesEach(attrs []string, c Conditions, block func(m Model, e *Errors, attribute string, value any))
ValidatesEach is ActiveModel::Validations#validates_each: run block once per attribute with its value, after allow_nil/allow_blank filtering.
func (*Validations) ValidatesEachWith ¶
func (v *Validations) ValidatesEachWith(attrs []string, validator EachValidator, c Conditions)
ValidatesEachWith registers a custom EachValidator over the given attributes.
func (*Validations) ValidatesWith ¶
func (v *Validations) ValidatesWith(validator Validator, c Conditions)
ValidatesWith registers a whole-record custom Validator (ActiveModel::Validations#validates_with).
