transformer

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package transformer provides struct-field transformation for REST requests using go-playground/mold. It is the conform/sanitize counterpart to the validator package and exposes mold's two faculties:

  • modifiers (`mod:` tags) mutate fields in place — trim, lcase, default, snake, … — and are meant to run before validation so dirty input is cleaned first.
  • scrubbers (`scrub:` tags) redact sensitive fields — emails, name, … — so a request struct can be logged safely.

The canonical request flow is decode → Transform (conform) → validate. Scrubbing mutates in place, so use ScrubbedCopy when the original value must be preserved (e.g. for logging a request that the handler still needs).

Basic usage:

type User struct {
    Name  string `json:"name"  mod:"trim"        validate:"required"  scrub:"name"`
    Email string `json:"email" mod:"trim,lcase" validate:"required,email" scrub:"emails"`
}

t := transformer.DefaultRestTransformer()
_ = t.Transform(ctx, &user) // conform: "  A@B.com " -> "a@b.com"
// ... validate, handle ...
safe, _ := transformer.ScrubbedCopy(ctx, t, &user) // redacted copy for logging

Custom transformations can be registered via RegisterModifier / RegisterScrubber, or by reaching the underlying mold transformers in the Modifiers / Scrubbers fields.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotTransformable = errors.New("transformer: value is not transformable")

Functions

func IsTransformable

func IsTransformable(i any) bool

func ScrubbedCopy

func ScrubbedCopy[T any](ctx context.Context, tfm *Transformer, v *T) (*T, error)

func ScrubbedCopyValue

func ScrubbedCopyValue(ctx context.Context, tfm *Transformer, v any) (any, error)

Types

type Transformer

type Transformer struct {
	Modifiers *mold.Transformer // applies `mod:` tags (conform before validation)
	Scrubbers *mold.Transformer // applies `scrub:` tags (redact PII for logging)
}

func DefaultRestTransformer

func DefaultRestTransformer() *Transformer

func (*Transformer) RegisterModifier

func (t *Transformer) RegisterModifier(tag string, fn mold.Func)

func (*Transformer) RegisterScrubber

func (t *Transformer) RegisterScrubber(tag string, fn mold.Func)

func (*Transformer) Scrub

func (t *Transformer) Scrub(ctx context.Context, i any) error

func (*Transformer) Transform

func (t *Transformer) Transform(ctx context.Context, i any) error

Jump to

Keyboard shortcuts

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