1

I was looking for a configuration parser for go and https://github.com/spf13/viper seems to come highly recommended.

I am very surprised to find that configuration files are not validated by default. Viper parses files and extracts requested values from them but I cannot find a way to detect bad configuration.

For instance I if create a (Java style) .properties file containing just "???" and nothing else. This is accepted without any error.

I can understand the philosophy that you should ignore irrelevant configuration items but I desire more rigor. I would also like to reject anything that does not match the X=Y format in a properties file.

To me this is a fatal flaw that suggests I should use a different package (or roll my own as usual).

Have I missed something? Does viper in fact support detecting and rejecting bad configuration keys?

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
  • What you're looking for is schema validation, and is not something Viper does (as far as I'm aware). [There's an open issue for it](https://github.com/spf13/viper/issues/702) though, if you want to voice your support and track it. –  Oct 30 '19 at 12:36
  • I saw that issue (which is poorly worded) but this is more fundamental. A file containing "???" is I believe incorrect syntactically as well as semantically. – Bruce Adams Oct 30 '19 at 12:41
  • That's pretty vague - depending on the file type and where in the file, "???" is not necessary invalid. With no details it's hard to tell what you're talking about. Is this a JSON file? YAML file? XML? TOML? How is the file structured and where is the "invalid" content in that structure? – Adrian Oct 30 '19 at 14:12
  • A .properties file as I specified. viper also seems prescriptive as to the file extension used. Altered to clarify slightly. – Bruce Adams Oct 30 '19 at 15:14

2 Answers2

1

I think the answer is no. viper does not validate java .properties files. I posted a bug report (or feature request depending on your point of view) as https://github.com/spf13/viper/issues/790

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
0

You can try https://github.com/num30/config library which is based on Viper. It has built-in validation.

Denis Palnitsky
  • 18,267
  • 14
  • 46
  • 55