yq: validate yaml syntax

Mike Farah’s yq yaml processor has a a full-featured validation command that is very detailed in its reporting, but the yaml specification itself is very lenient, which means yq may accept scenarios you did not expect (e.g. an empty file).

yq -v file.yaml >/dev/null ; echo "final result = $?"

Luckily, the yq tips-and-tricks section provides an example of testing whether the yaml file is of valid syntax AND either a map or an array at the top level, which aligns better with what most would consider a valid yaml file.

yq --exit-status 'tag == "!!map" or tag== "!!seq"' file.yaml > /dev/null ; echo "final result = $?"

REFERENCES

Mike Farah yq on Github

stackoverflow, how to validate yaml using yq

YAML 1.2 specification