4

I have a YAML file in my directory and I want to validate the YAML file was in correct structure using java programming. In online it having lot of YAML checker but I want to validate that on java programming.

Anthon
  • 59,987
  • 25
  • 170
  • 228

3 Answers3

4

Just try to load it with your YAML parser/library. If that throws an error catch it and the structure is invalid. That is the way the online parsers work as well.

Anthon
  • 59,987
  • 25
  • 170
  • 228
  • 1
    While this is correct, this approach doesn't cover all use cases that e.g. are covered by XSD for XML files. E.g. when a YAML file lacks a **mandatory** property, it is set to `null` in the created POJO/bean (without creating an error). What I said at least applies to snakeyaml. There might be more feature-rich parsers. – Marteng Mar 18 '21 at 13:43
2

You can use YAML parser/librairies like Jackson or SnakeYAML:

https://www.baeldung.com/jackson-yaml

https://www.baeldung.com/java-snake-yaml

Philippe GRANET
  • 308
  • 2
  • 5
0

There's also https://github.com/EsotericSoftware/yamlbeans

One issue I've found is that JSON objects are acceptable YAML 1.2 and, arguably, 1.1 formats.

What valid JSON files are not valid YAML 1.1 files?

MBudnick
  • 23
  • 5