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 ¶
- Variables
- type FieldInfo
- func (f FieldInfo) EffectiveTypeName() string
- func (f FieldInfo) EnumRule() *TagRule
- func (f FieldInfo) HasCoerceRule() bool
- func (f FieldInfo) HasRule(name string) bool
- func (f FieldInfo) HasRules() bool
- func (f FieldInfo) HasSchemaSpec() bool
- func (f FieldInfo) IsEnumStringField() bool
- func (f FieldInfo) IsPointerType() bool
- func (f FieldInfo) IsUUIDStringField() bool
- func (f FieldInfo) NeedsCoreImport() bool
- func (f FieldInfo) NeedsGeneratedOptional() bool
- func (f FieldInfo) NeedsNetImport() bool
- func (f FieldInfo) NeedsNetURLImport() bool
- func (f FieldInfo) NeedsOptionalModifier() bool
- func (f FieldInfo) NeedsPointerNilable() bool
- func (f FieldInfo) NeedsPointerOptional() bool
- func (f FieldInfo) NeedsRegexpImport() bool
- func (f FieldInfo) NeedsStringsImport() bool
- func (f FieldInfo) RequiredImports() []string
- func (f FieldInfo) RulesExcept(names ...string) []TagRule
- func (f FieldInfo) UsesTimeImport() bool
- func (f FieldInfo) ValidationRules() []TagRule
- func (f FieldInfo) ValidationRulesExcept(names ...string) []TagRule
- type TagParser
- type TagRule
Constants ¶
This section is empty.
Variables ¶
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
EffectiveTypeName returns the best available type name for codegen.
func (FieldInfo) HasCoerceRule ¶ added in v0.10.0
HasCoerceRule reports whether the field enables coercion.
func (FieldInfo) HasRule ¶ added in v0.10.0
HasRule reports whether a parsed rule with the given name exists.
func (FieldInfo) HasRules ¶ added in v0.10.0
HasRules reports whether the field has any parsed tag rules.
func (FieldInfo) HasSchemaSpec ¶ added in v0.10.0
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
IsEnumStringField reports whether this field should use the Enum constructor.
func (FieldInfo) IsPointerType ¶ added in v0.10.0
IsPointerType reports whether the field type is a pointer.
func (FieldInfo) IsUUIDStringField ¶ added in v0.10.0
IsUUIDStringField reports whether this field should use the UUID constructor.
func (FieldInfo) NeedsCoreImport ¶ added in v0.10.0
NeedsCoreImport reports whether codegen should import core.
func (FieldInfo) NeedsGeneratedOptional ¶ added in v0.10.0
NeedsGeneratedOptional reports whether generated schema code should append .Optional() after applying all rules.
func (FieldInfo) NeedsNetImport ¶ added in v0.10.0
NeedsNetImport reports whether codegen should import net.
func (FieldInfo) NeedsNetURLImport ¶ added in v0.10.0
NeedsNetURLImport reports whether codegen should import net/url.
func (FieldInfo) NeedsOptionalModifier ¶ added in v0.10.0
NeedsOptionalModifier reports whether generated schema code should append .Optional() for this field.
func (FieldInfo) NeedsPointerNilable ¶ added in v0.10.0
NeedsPointerNilable reports whether a pointer-backed schema should become nilable during struct-schema derivation.
func (FieldInfo) NeedsPointerOptional ¶ added in v0.10.0
NeedsPointerOptional reports whether pointer field rules should add Optional() at the end of parsed tag application.
func (FieldInfo) NeedsRegexpImport ¶ added in v0.10.0
NeedsRegexpImport reports whether codegen should import regexp.
func (FieldInfo) NeedsStringsImport ¶ added in v0.10.0
NeedsStringsImport reports whether codegen should import strings.
func (FieldInfo) RequiredImports ¶ added in v0.10.0
RequiredImports returns the non-gozod imports needed to generate this field.
func (FieldInfo) RulesExcept ¶ added in v0.10.0
RulesExcept returns all parsed rules except those whose names are excluded.
func (FieldInfo) UsesTimeImport ¶ added in v0.10.0
UsesTimeImport reports whether codegen should import time for this field.
func (FieldInfo) ValidationRules ¶ added in v0.10.0
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
ValidationRulesExcept returns validation rules except those explicitly excluded.
type TagParser ¶
type TagParser struct {
// contains filtered or unexported fields
}
TagParser handles gozod tag parsing with configurable tag name.
func NewWithTagName ¶
NewWithTagName creates a TagParser with a custom tag name.
func (*TagParser) ParseStructTags ¶
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
ParseStructTagsStrict parses all gozod tags in a struct type and returns ErrTypeMustBeStruct for non-struct input.