add ParentPath to context, remove Parent from context and plumb changes in validators

This commit is contained in:
Aaron Prindle
2025-07-16 22:37:18 +00:00
parent c50da38aa1
commit 5d1c5ebd8c
5 changed files with 30 additions and 31 deletions

View File

@@ -350,10 +350,10 @@ func (td *typeDiscoverer) discoverType(t *types.Type, fldPath *field.Path) (*typ
panic(fmt.Sprintf("path for type != the type name: %s, %s", t.String(), fldPath.String()))
}
context := validators.Context{
Scope: validators.ScopeType,
Type: t,
Parent: nil,
Path: fldPath,
Scope: validators.ScopeType,
Type: t,
ParentPath: nil,
Path: fldPath,
}
extractedTags, err := td.validator.ExtractTags(context, t.CommentLines)
if err != nil {
@@ -577,11 +577,11 @@ func (td *typeDiscoverer) discoverStruct(thisNode *typeNode, fldPath *field.Path
// Extract any field-attached validation rules.
context := validators.Context{
Scope: validators.ScopeField,
Type: childType,
Parent: thisNode.valueType,
Member: &memb,
Path: childPath,
Scope: validators.ScopeField,
Type: childType,
ParentPath: fldPath,
Member: &memb,
Path: childPath,
}
tags, err := td.validator.ExtractTags(context, memb.CommentLines)

View File

@@ -400,9 +400,9 @@ func (evtv eachValTagValidator) GetValidations(context Context, tag codetags.Tag
}
elemContext := Context{
Type: nt.Elem,
Parent: t, // possibly an alias
Path: context.Path.Key("*"),
Type: nt.Elem,
ParentPath: context.Path,
Path: context.Path.Key("*"),
}
switch nt.Kind {
case types.Slice, types.Array:
@@ -568,10 +568,10 @@ func (ektv eachKeyTagValidator) GetValidations(context Context, tag codetags.Tag
}
elemContext := Context{
Scope: ScopeMapKey,
Type: t.Elem,
Parent: t,
Path: context.Path.Child("(keys)"),
Scope: ScopeMapKey,
Type: t.Elem,
ParentPath: context.Path,
Path: context.Path.Child("(keys)"),
}
if validations, err := ektv.validator.ExtractValidations(elemContext, *tag.ValueTag); err != nil {

View File

@@ -254,11 +254,11 @@ func (iv itemValidator) GetValidations(context Context) (Validations, error) {
// Extract validations from the stored tag
subContextPath := generateFieldPathForMap(item.criteria)
subContext := Context{
Scope: ScopeListVal,
Type: elemT,
Parent: context.Type,
Path: context.Path.Key(subContextPath),
Member: nil,
Scope: ScopeListVal,
Type: elemT,
Path: context.Path.Key(subContextPath),
ParentPath: context.Path,
Member: nil,
}
validations, err := iv.validator.ExtractValidations(subContext, item.valueTag)

View File

@@ -70,10 +70,11 @@ func (stv subfieldTagValidator) GetValidations(context Context, tag codetags.Tag
}
result := Validations{}
subContext := Context{
Scope: ScopeField,
Type: submemb.Type,
Parent: t,
Path: context.Path.Child(subname),
Scope: ScopeField,
Type: submemb.Type,
ParentPath: context.Path,
Member: submemb,
Path: context.Path.Child(subname),
}
if validations, err := stv.validator.ExtractValidations(subContext, *tag.ValueTag); err != nil {
return Validations{}, err

View File

@@ -198,12 +198,10 @@ type Context struct {
// both).
Type *types.Type
// Parent provides details about the logical parent type of the object
// being validated, when applicable. When Scope is ScopeField, this is the
// containing struct's type. When Scope indicates a list-value, map-key,
// or map-value, this is the type of the whole list or map. When Scope is
// ScopeType, this is nil.
Parent *types.Type
// ParentPath provides the field path to the parent type or field, enabling
// unique identification of validation contexts for the same type in
// different locations.
ParentPath *field.Path
// Member provides details about a field within a struct when Scope is
// ScopeField. For all other values of Scope, this will be nil.