Search code examples
pythonpython-3.xpyyaml

Unable to validate a YAML file using PyYAML library


I am trying to validate YAML files using Python and I am using the PyYAML library for that.

But unfortunately, it reports any files it checks as valid YAML files, not only YAML files. I am not sure why.

Below is the Python script. Suppose you pass a sample bash script file_path = './testbashscript.sh' to validate, it reports the bash script as a valid YAML file which does not make sense to me.

#!/usr/bin/env python3

import yaml

def is_yaml_file(file_path):
    try:
        with open(file_path, 'r') as file:
            yaml.safe_load(file)
            return True           
    except:
        return False        
    
def main():
    file_path = './testbashscript.sh'
    if is_yaml_file(file_path):
        print(f"{file_path} is a valid YAML file")
    else:
        print(f"{file_path} is not a valid YAML file")

if __name__ == "__main__":
    main()

Content of the sample bash script:

→ cat testbashscript.sh     
#!/usr/bin/env bash
echo "Hello World"

The output I get is:

→ python3 yamlvalidate.py                                                     
./testbashscript.sh is a valid YAML file

The expected result is, it should say it is not a valid YAML file. Am I doing something wrong here?


Solution

  • The issue is that testbashscript.sh is a valid YAML file. It's the equivalent of the following JSON:

    "echo \"Hello World\""
    

    That is, it's a single string.

    You can confirm this by pasting the content into an online YAML validator like https://www.yamllint.com/; you'll find they make the same determination about its validity.