-
I wanted to know if it's possible to error when a required field was not found during the unmarshall. type MyConfig struct {
DatabaseUrl string `toml:"database_url,required"`
}
var MyConfig cfg
if err := toml.Unmarshall(configFileContent, &cfg), err != nil {
// Handle missing required field error, or any other error
} I don't know if this is possible, but it is easier to check a single error than checking if each individual field doesn't contain an invalid value. If it isn't possible, I say it would be a nice addition. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Really sorry I missed this question! There is no notion of required field in go-toml. In the spirit of keeping go-toml minimal, I think this feature is better implemented outside the library, for example: https://github.com/go-playground/validator. If you want to answer the question "is this value present in the toml document?" you have three options that come to mind:
|
Beta Was this translation helpful? Give feedback.
-
For reference, I ended up doing my own validator by unmarshalling the fields as pointers, then checking if the pointers are |
Beta Was this translation helpful? Give feedback.
Really sorry I missed this question!
There is no notion of required field in go-toml. In the spirit of keeping go-toml minimal, I think this feature is better implemented outside the library, for example: https://github.com/go-playground/validator. If you want to answer the question "is this value present in the toml document?" you have three options that come to mind:
validator
to check its result.interface{}
and walk its value to see what was in the file.