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.
A parser is configured with two tag names: the rule tag (default "gozod") and the field-name tag that supplies field names (default "json"). Use NewWithTags to select both, or FieldName to resolve a single field's name from a chosen field-name tag such as "yaml" or "toml".
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 FieldNameResult
- type FieldPlan
- type OptionalPlacement
- type RuleOp
- type RulePlan
- type TagParser
- type TagRule
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyRuleName = errors.New("tagparser: empty rule name")
ErrEmptyRuleName indicates that a tag rule has no rule name.
var ErrRuleRequiresParam = errors.New("tagparser: rule requires a parameter")
ErrRuleRequiresParam indicates that a tag rule used "=" without a value.
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
FieldKey string // from the field-name 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. IP tags generate root GoZod constructors and do not need net.
func (FieldInfo) NeedsNetURLImport ¶ added in v0.10.0
NeedsNetURLImport reports whether codegen should import net/url. URL tags generate root GoZod constructors and do not need 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 FieldNameResult ¶ added in v0.11.6
FieldNameResult represents the resolved schema key for a struct field.
func FieldName ¶ added in v0.11.6
func FieldName(tagName string, f reflect.StructField) FieldNameResult
FieldName resolves the field name and skip marker for a struct field using the named struct tag (e.g. "json", "yaml", "toml"). An absent tag falls back to the Go field name; a tag value of "-" marks the field as skipped.
type FieldPlan ¶ added in v0.11.7
type FieldPlan struct {
Operations []RulePlan
RuntimePointerOptional OptionalPlacement
GeneratedOptional OptionalPlacement
}
FieldPlan is the shared semantic plan for a parsed struct field.
func CompileFieldPlan ¶ added in v0.11.7
CompileFieldPlan converts parsed field facts into semantic operations shared by runtime reflection and code generation.
func (FieldPlan) OperationsExcept ¶ added in v0.11.7
OperationsExcept returns planned operations except those for the named raw rules.
type OptionalPlacement ¶ added in v0.11.7
type OptionalPlacement string
OptionalPlacement describes where an Optional modifier belongs relative to field operations when a backend must express optionality as a fluent call.
const ( // OptionalPlacementNone means no generated Optional modifier is needed. OptionalPlacementNone OptionalPlacement = "none" // OptionalPlacementBeforeOperations places Optional before planned rule operations. OptionalPlacementBeforeOperations OptionalPlacement = "before_operations" // OptionalPlacementAfterOperations places Optional after planned rule operations. OptionalPlacementAfterOperations OptionalPlacement = "after_operations" )
type RuleOp ¶ added in v0.11.5
type RuleOp string
RuleOp identifies the semantic operation represented by a tag rule.
const ( RuleStructural RuleOp = "structural" RuleMethod RuleOp = "method" RuleStringCheck RuleOp = "string_check" RuleTime RuleOp = "time" RulePositive RuleOp = "positive" RuleNegative RuleOp = "negative" RuleFinite RuleOp = "finite" RuleNonEmpty RuleOp = "nonempty" RuleEnum RuleOp = "enum" RuleLiteral RuleOp = "literal" RuleDefault RuleOp = "default" RulePrefault RuleOp = "prefault" RuleUnsupported RuleOp = "unsupported" )
RuleOp values describe shared tag-rule operations.
type RulePlan ¶ added in v0.11.5
RulePlan is the shared semantic plan for a parsed tag rule.
func CompileRule ¶ added in v0.11.5
CompileRule converts a parsed tag rule into a shared semantic plan.
func (RulePlan) FirstParam ¶ added in v0.11.5
FirstParam returns the first parameter for rules that take one argument.
func (RulePlan) JoinedValue ¶ added in v0.11.5
JoinedValue returns a single value for default-like rules.
type TagParser ¶
type TagParser struct {
// contains filtered or unexported fields
}
TagParser handles gozod tag parsing with configurable tag names. rulesTagName selects the rule tag (default "gozod"); fieldNameTag selects the tag that supplies field names (default "json").
func New ¶
func New() *TagParser
New creates a TagParser with the default "gozod" rule tag and "json" field-name tag.
func NewWithTagName ¶
NewWithTagName creates a TagParser with a custom rule tag and the default "json" field-name tag.
func NewWithTags ¶ added in v0.11.6
NewWithTags creates a TagParser with custom rule and field-name tags. Empty values fall back to the defaults ("gozod" and "json").
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.