@@ -1453,7 +1453,10 @@ func (d *Decoder) decodeStruct(ctx context.Context, dst reflect.Value, src ast.N
14531453 node , exists := keyToNodeMap [structField .RenderName ]
14541454 if exists {
14551455 // TODO: to make FieldError message cutomizable
1456- return errors .ErrSyntax (fmt .Sprintf ("%s" , err ), node .GetToken ())
1456+ return errors .ErrSyntax (
1457+ fmt .Sprintf ("%s" , err ),
1458+ d .getParentMapTokenIfExistsForValidationError (node .Type (), node .GetToken ()),
1459+ )
14571460 } else if t := src .GetToken (); t != nil && t .Prev != nil && t .Prev .Prev != nil {
14581461 // A missing required field will not be in the keyToNodeMap
14591462 // the error needs to be associated with the parent of the source node
@@ -1467,6 +1470,37 @@ func (d *Decoder) decodeStruct(ctx context.Context, dst reflect.Value, src ast.N
14671470 return nil
14681471}
14691472
1473+ // getParentMapTokenIfExists if the NodeType is a container type such as MappingType or SequenceType,
1474+ // it is necessary to return the parent MapNode's colon token to represent the entire container.
1475+ func (d * Decoder ) getParentMapTokenIfExistsForValidationError (typ ast.NodeType , tk * token.Token ) * token.Token {
1476+ if tk == nil {
1477+ return nil
1478+ }
1479+ if typ == ast .MappingType {
1480+ // map:
1481+ // key: value
1482+ // ^ current token ( colon )
1483+ if tk .Prev == nil {
1484+ return tk
1485+ }
1486+ key := tk .Prev
1487+ if key .Prev == nil {
1488+ return tk
1489+ }
1490+ return key .Prev
1491+ }
1492+ if typ == ast .SequenceType {
1493+ // map:
1494+ // - value
1495+ // ^ current token ( sequence entry )
1496+ if tk .Prev == nil {
1497+ return tk
1498+ }
1499+ return tk .Prev
1500+ }
1501+ return tk
1502+ }
1503+
14701504func (d * Decoder ) decodeArray (ctx context.Context , dst reflect.Value , src ast.Node ) error {
14711505 d .stepIn ()
14721506 defer d .stepOut ()
0 commit comments