Manager

New, the configuration options, and the Set and Validate methods.

The manager is the main entry point. New binds a pointer to your struct and returns a reusable *Struct you build once and call many times.

New

func New(structure any, opts ...Option) *Struct

Binds a pointer to a struct into a reusable *Struct. structure must be a pointer to a struct. Options override the defaults, which are DefaultTags for tag priority, DefaultRules for validation, DefaultEncodingTags for comma stripping, and "rules" for the validation message tag.

manager := structs.New(cfg, structs.WithTags("arg", "short"))

Options

Each option is an Option, a function that configures the *Struct. Pass any number to New.

func WithTags(tags ...string) Option

Sets the tag priority order used for input lookup and validation. The first tag a field carries wins. Defaults to DefaultTags (json, yaml).

func WithEncodingTags(tags ...string) Option

Sets the tags whose values follow the stdlib name,option convention and so have their comma options stripped when parsed. Defaults to DefaultEncodingTags. Pass none to disable comma stripping entirely.

func WithRules(rules map[string]RuleFunc) Option

Sets the rule set used by Validate, keyed by the name used in a rules: tag. Defaults to DefaultRules. Pass your own map to extend or replace it.

func WithValidationTag(tag string) Option

Sets the struct tag whose value is used as the field key in the map returned by Validate. When a field carries this tag, its value replaces the tag-priority-resolved name in the validation errors, letting you report under a stable, caller-facing name. Defaults to "rules".

Set

func (m *Struct) Set(inputs map[string]any) error

Populates the bound struct from inputs, resolving keys by tag priority and applying default: tag values to fields left zero. Returns an error only when binding fails internally, not for a missing key.

Validate

func (m *Struct) Validate(inputs map[string]any) (map[string][]string, error)

Runs the configured rules over inputs and returns the validation errors as a map of field name (resolved by tag priority, or the validation tag when present) to messages. An empty map means everything passed. Validate never mutates the struct. The returned error is non-nil only when validation could not run, never for a field that failed a rule. See Validation for the usual check-then-set flow.