Skip to content

Incorrect source annotation for validator #684

@Madoxen

Description

@Madoxen

Describe the bug
I have a custom validator that checks if a field with a given validator tag is set. If it is set then the validator fails and error is printed. Below there is an example of emitted error. The suprising thing is that for structs and maps the error is positioned in the inner map instead of the colon at the problematic map.

error: [3:9] W0001: warn: field is deprecated --- "ImageManifests" use "images" instead
               1 | ---
               2 | image-manifests:
            >  3 |   docker: null
                           ^

To Reproduce
Though I have a custom validator written, we can reproduce this by adding a testcase, with eq=0, to testdata/validate_test.go:

		{
			TestName: "Test map",
			YAMLContent: `
map:
  hello: hello
  hello2: hello`,
			ExpectedErr: `[2:4] Key: 'Map' Error:Field validation for 'Map' failed on the 'eq' tag
>  2 | map:
          ^
   3 |   hello: hello
   4 |   hello2: hello`,
			Instance: &struct {
				// Make sure that this map is not set
				Map map[string]string `yaml:"map" validate:"eq=0"`
			}{},
		},

The result will be:

=== RUN   TestStructValidator/Test_map
    validate_test.go:205: unexpected error: [3:8] Key: 'Map' Error:Field validation for 'Map' failed on the 'eq' tag
           2 | map:
        >  3 |   hello: hello
                      ^
           4 |   hello2: hello

Expected behavior
The error should be positioned at the problematic map not at it's value. For the above test I think this would be better:

=== RUN   TestStructValidator/Test_Required_map
    validate_test.go:205: unexpected error: [1:4] Key: 'Map' Error:Field validation for 'Map' failed on the 'eq' tag
           > 2 | map:
                     ^
           3 |   hello: hello
           4 |   hello2: hello

Version Variables

  • Go version: 1.21
  • go-yaml's Version: @latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions