tagparser

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package tagparser parses struct-tag validation rules into a reusable representation shared by GoZod runtime code and codegen.

Callers should prefer FieldInfo helper methods such as FieldInfo.ValidationRules, FieldInfo.NeedsGeneratedOptional, and FieldInfo.RequiredImports instead of re-deriving schema-generation semantics from raw fields like Rules, Required, or Optional.

Use TagParser.ParseStructTags for compatibility-oriented callers that prefer empty results on non-struct input, or TagParser.ParseStructTagsStrict when non-struct input should be treated as a contract error.

Package tagparser provides shared tag parsing functionality for GoZod. Extracted from types/struct.go to enable reuse by cmd/gozodgen and other components.

Index

Constants

This section is empty.

Variables

View Source
var ErrTypeMustBeStruct = errors.New("tagparser: type must be a struct or pointer to struct")

ErrTypeMustBeStruct indicates that the input type is neither a struct nor a pointer to struct.

Functions

This section is empty.

Types

type FieldInfo

type FieldInfo struct {
	Name     string       // Go field name
	Type     reflect.Type // field type
	TypeName string       // AST type name for circular reference detection
	JSONName string       // from json tag, or Go field name
	GoZodTag string       // raw gozod tag value
	Rules    []TagRule    // parsed tag rules before helper-level filtering
	Required bool         // whether the parsed rules include "required"
	Optional bool         // derived optionality for parsed-tag callers
	Nilable  bool         // whether the parsed rules include "nilable"
}

FieldInfo represents parsed information about a struct field.

func (FieldInfo) EffectiveTypeName added in v0.10.0

func (f FieldInfo) EffectiveTypeName() string

EffectiveTypeName returns the best available type name for codegen.

func (FieldInfo) EnumRule added in v0.10.0

func (f FieldInfo) EnumRule() *TagRule

EnumRule returns the enum rule if present.

func (FieldInfo) HasCoerceRule added in v0.10.0

func (f FieldInfo) HasCoerceRule() bool

HasCoerceRule reports whether the field enables coercion.

func (FieldInfo) HasRule added in v0.10.0

func (f FieldInfo) HasRule(name string) bool

HasRule reports whether a parsed rule with the given name exists.

func (FieldInfo) HasRules added in v0.10.0

func (f FieldInfo) HasRules() bool

HasRules reports whether the field has any parsed tag rules.

func (FieldInfo) HasSchemaSpec added in v0.10.0

func (f FieldInfo) HasSchemaSpec() bool

HasSchemaSpec reports whether the field has tag-derived schema semantics. Runtime/codegen consumers should prefer helper methods on FieldInfo instead of open-coding logic from the raw fields.

func (FieldInfo) IsEnumStringField added in v0.10.0

func (f FieldInfo) IsEnumStringField() bool

IsEnumStringField reports whether this field should use the Enum constructor.

func (FieldInfo) IsPointerType added in v0.10.0

func (f FieldInfo) IsPointerType() bool

IsPointerType reports whether the field type is a pointer.

func (FieldInfo) IsUUIDStringField added in v0.10.0

func (f FieldInfo) IsUUIDStringField() bool

IsUUIDStringField reports whether this field should use the UUID constructor.

func (FieldInfo) NeedsCoreImport added in v0.10.0

func (f FieldInfo) NeedsCoreImport() bool

NeedsCoreImport reports whether codegen should import core.

func (FieldInfo) NeedsGeneratedOptional added in v0.10.0

func (f FieldInfo) NeedsGeneratedOptional() bool

NeedsGeneratedOptional reports whether generated schema code should append .Optional() after applying all rules.

func (FieldInfo) NeedsNetImport added in v0.10.0

func (f FieldInfo) NeedsNetImport() bool

NeedsNetImport reports whether codegen should import net.

func (FieldInfo) NeedsNetURLImport added in v0.10.0

func (f FieldInfo) NeedsNetURLImport() bool

NeedsNetURLImport reports whether codegen should import net/url.

func (FieldInfo) NeedsOptionalModifier added in v0.10.0

func (f FieldInfo) NeedsOptionalModifier() bool

NeedsOptionalModifier reports whether generated schema code should append .Optional() for this field.

func (FieldInfo) NeedsPointerNilable added in v0.10.0

func (f FieldInfo) NeedsPointerNilable() bool

NeedsPointerNilable reports whether a pointer-backed schema should become nilable during struct-schema derivation.

func (FieldInfo) NeedsPointerOptional added in v0.10.0

func (f FieldInfo) NeedsPointerOptional() bool

NeedsPointerOptional reports whether pointer field rules should add Optional() at the end of parsed tag application.

func (FieldInfo) NeedsRegexpImport added in v0.10.0

func (f FieldInfo) NeedsRegexpImport() bool

NeedsRegexpImport reports whether codegen should import regexp.

func (FieldInfo) NeedsStringsImport added in v0.10.0

func (f FieldInfo) NeedsStringsImport() bool

NeedsStringsImport reports whether codegen should import strings.

func (FieldInfo) RequiredImports added in v0.10.0

func (f FieldInfo) RequiredImports() []string

RequiredImports returns the non-gozod imports needed to generate this field.

func (FieldInfo) RulesExcept added in v0.10.0

func (f FieldInfo) RulesExcept(names ...string) []TagRule

RulesExcept returns all parsed rules except those whose names are excluded.

func (FieldInfo) UsesTimeImport added in v0.10.0

func (f FieldInfo) UsesTimeImport() bool

UsesTimeImport reports whether codegen should import time for this field.

func (FieldInfo) ValidationRules added in v0.10.0

func (f FieldInfo) ValidationRules() []TagRule

ValidationRules returns tag rules that should become schema modifiers or checks at runtime/codegen. Structural rules are handled elsewhere.

func (FieldInfo) ValidationRulesExcept added in v0.10.0

func (f FieldInfo) ValidationRulesExcept(names ...string) []TagRule

ValidationRulesExcept returns validation rules except those explicitly excluded.

type JSONField added in v0.10.5

type JSONField struct {
	Name string
	Skip bool
}

JSONField represents the resolved JSON contract for a struct field.

func JSONFieldName added in v0.10.5

func JSONFieldName(f reflect.StructField) JSONField

JSONFieldName resolves the JSON field name and skip marker for a struct field.

type TagParser

type TagParser struct {
	// contains filtered or unexported fields
}

TagParser handles gozod tag parsing with configurable tag name.

func New

func New() *TagParser

New creates a TagParser with the default "gozod" tag name.

func NewWithTagName

func NewWithTagName(name string) *TagParser

NewWithTagName creates a TagParser with a custom tag name.

func (*TagParser) ParseStructTags

func (p *TagParser) ParseStructTags(typ reflect.Type) ([]FieldInfo, error)

ParseStructTags parses all gozod tags in a struct type and returns FieldInfo for each exported field.

Non-struct input returns an empty slice and no error. Use [ParseStructTagsStrict] when non-struct input should fail.

func (*TagParser) ParseStructTagsStrict added in v0.10.0

func (p *TagParser) ParseStructTagsStrict(typ reflect.Type) ([]FieldInfo, error)

ParseStructTagsStrict parses all gozod tags in a struct type and returns ErrTypeMustBeStruct for non-struct input.

func (*TagParser) ParseTagString

func (p *TagParser) ParseTagString(tag string) ([]TagRule, error)

ParseTagString parses a single tag string into TagRule values.

type TagRule

type TagRule struct {
	Name   string   // e.g., "min", "max", "email"
	Params []string // e.g., ["2"] for "min=2"
}

TagRule represents a single validation rule parsed from a tag.

Jump to

Keyboard shortcuts

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