How could I validate a YAML structure in java programming
Asked Answered
C

3

6

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.

Clive answered 7/10, 2015 at 10:56 Comment(0)
E
5

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.

Effieeffigy answered 7/10, 2015 at 11:15 Comment(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.Assonance
P
4

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

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

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

Palestrina answered 16/9, 2019 at 13:57 Comment(0)
M
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?

Matlick answered 24/6, 2020 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.