Search code examples
validationyamlschema

CLI tool to validate schema of YAML file?


Say I have a YAML file https://raw.githubusercontent.com/kaihendry/days/main/template.yml with an accompanying schema: https://raw.githubusercontent.com/awslabs/goformation/master/schema/sam.schema.json

What tool do I use to validate the template.yml to ensure it's correct from the Linux command line?


Solution

  • Please consider using yajsv as it is covering both the YAML and JSON use cases.

    The simpler way to install it is by downloading the proper release

    Linux amd64 example:

    To install it:
    wget https://github.com/neilpa/yajsv/releases/download/v1.4.1/yajsv.linux.amd64
    chmod +x ./yajsv.linux.amd64 # you may need to run it with root rights (aka sudo chm...)
    

    Keep in mind that the URL may change soon so just copy the proper URL using your browser.

    To validate ONE Yaml using a specific JSON schema you can use:
    ./yajsv.linux.amd64 -s /path/to/json/schema/direcotry/schema.json /path/to/yaml/data/direcotry/file.yml
    

    -OR-

    To validate MULTIPLE Yamls using a specific JSON schema you can use:
    ./yajsv.linux.amd64 -s /path/to/json/schema/direcotry/schema.json -r /path/to/yaml/data/direcotry/file1.yml /path/to/yaml/data/direcotry/file2.yml /path/to/yaml/data/direcotry/file3.yml
    

    You can use yajsv GitHub for more details.

    Bonus:

    If you are using Nix package manager or NixOs you can test the yajsv first by using:

    nix-shell -p yajsv
    # test your yamls
    exit # now yamls is not an existing command to your machine 
    

    If you want to install it:

    # If you are running NixOS:
    nix-env -iA nixos.yajsv
    
    # If you are running other distributions:
    nix-env -iA nixpkgs.yajsv