Skip to content

DisallowUnknownFields error message says "missing field" instead of "unknown field" #1049

Description

@phinze

Hello! We've been happily using go-toml at Miren for our config file parsing. Thanks for your work on this library :)

Describe the bug

When using Decoder.DisallowUnknownFields(), the error annotation in the diagnostic output says "missing field":

1| [services.web]
2| typo_field = "value"
   | ~~~~~~~~~~ missing field

This reads as though the TOML file is missing something it should have, when actually the user wrote a field that the decoder doesn't recognize. The public API calls it DisallowUnknownFields, so "unknown field" would be more consistent and less confusing.

To Reproduce

type Config struct {
    Name string
}

var cfg Config
err := toml.NewDecoder(strings.NewReader(`naem = "test"`)).DisallowUnknownFields().Decode(&cfg)
fmt.Println(err)
// output includes "missing field"

Expected behavior

The annotation should say "unknown field" instead of "missing field" to match the DisallowUnknownFields API name and describe the problem from the user's perspective.

Versions

  • go-toml: v2.2.3
  • go: 1.25
  • operating system: Linux

Additional context

The fix looks straightforward: change the Message in strict.go's MissingField method from "missing field" to "unknown field". Went ahead and sent a PR (#1050) since the change is small, but happy to discuss further!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions