Top 5 Tools to Validate YAML Files

David Singer
Hosting

What is YAML?

YAML Ain’t Markup Language (or YAML™) is a human-friendly, easily readable data language widely used with or alongside almost all programming mediums. It is designed around a standard formatted layout useful for creating configuration files that readily integrate across multiple development environments. It is also valuable for managing concurrent data as it includes the ability to employ Unicode printable characters.

YAML Format

The structure of YAML is indicated through indentations using one or more spaces. A single YAML file can contain multiple documents in a logical sequence. Item values are designated by whitespaces, hyphens, pipes, curly and square brackets, number signs, question marks, single or double quotes, backslashes, periods, ampersands, asterisks, and various other special characters. It can contain multiple commands but does not execute those commands itself. The related language called on accomplishes that task.

YAML has multiple formatting requirements to ensure it is compatible with the platform where it is employed. Any file designated as a YAML file should have the appended *.yaml or *.yml file extension. Additionally, YAML formatting prohibits tabs from being used. This restriction is primarily due to the differences in code editors and other development tools. 

Available Platforms

The YAML standard is used in over 20 different languages or other related projects. Some of these include:

  • C/C++
  • C#/.NET
  • Go
  • Java
  • Javascript
  • Perl
  • PHP
  • Python 
  • Ruby
  • Rust

Features

YAML regards markup language with a vital construct to distinguish the data-oriented language within the document markup. Noted below are the YAML design goals:

  • Data is portable between programming languages.
  • Provides a consistent and reproducible data model.
  • Easy to use and implement in various languages. 
  • Supports single, one-pass directional processing of data within.
  • Users can validate YAML from within the command line or using a software program.

YAML Example

---  # document start

# Comments in YAML look like this.

################
# SCALAR TYPES #
################

# Our root object (which continues for the entire document) will be a map,
# which is equivalent to a dictionary, hash or object in other languages.
key: value
another_key: Another value goes here.
a_number_value: 100
scientific_notation: 1e+12
# The number 1 will be interpreted as a number, not a boolean. if you want
# it to be interpreted as a boolean, use true
boolean: true
null_value: null
key with spaces: value
# Notice that strings don't need to be quoted. However, they can be.
however: 'A string, enclosed in quotes.'
'Keys can be quoted too.': "Useful if you want to put a ':' in your key."
single quotes: 'have ''one'' escape pattern'
double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and more."
# UTF-8/16/32 characters need to be encoded
Superscript two: \u00B2

# Multiple-line strings can be written either as a 'literal block' (using |),
# or a 'folded block' (using '>').
literal_block: |
    This entire block of text will be the value of the 'literal_block' key,
    with line breaks being preserved.

    The literal continues until de-dented, and the leading indentation is
    stripped.

        Any lines that are 'more-indented' keep the rest of their indentation -
        these lines will be indented by 4 spaces.
folded_style: >
    This entire block of text will be the value of 'folded_style', but this
    time, all newlines will be replaced with a single space.

    Blank lines, like above, are converted to a newline character.

        'More-indented' lines keep their newlines, too -
        this text will appear over two lines.

####################
# COLLECTION TYPES #
####################

# Nesting uses indentation. 2 space indent is preferred (but not required).
a_nested_map:
  key: value
  another_key: Another Value
  another_nested_map:
    hello: hello

# Maps don't have to have string keys.
0.25: a float key

# Keys can also be complex, like multi-line objects
# We use ? followed by a space to indicate the start of a complex key.
? |
  This is a key
  that has multiple lines
: and this is its value

# YAML also allows mapping between sequences with the complex key syntax
# Some language parsers might complain
# An example
? - Manchester United
  - Real Madrid
: [2001-01-01, 2002-02-02]

# Sequences (equivalent to lists or arrays) look like this
# (note that the '-' counts as indentation):
a_sequence:
  - Item 1
  - Item 2
  - 0.5  # sequences can contain disparate types.
  - Item 4
  - key: value
    another_key: another_value
  -
    - This is a sequence
    - inside another sequence
  - - - Nested sequence indicators
      - can be collapsed

# Since YAML is a superset of JSON, you can also write JSON-style maps and
# sequences:
json_map: {"key": "value"}
json_seq: [3, 2, 1, "takeoff"]
and quotes are optional: {key: [3, 2, 1, takeoff]}

#######################
# EXTRA YAML FEATURES #
#######################

# YAML also has a handy feature called 'anchors', which let you easily duplicate
# content across your document. Both of these keys will have the same value:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name

# Anchors can be used to duplicate/inherit properties
base: &base
  name: Everyone has same name

# The regexp << is called Merge Key Language-Independent Type. It is used to
# indicate that all the keys of one or more specified maps should be inserted
# into the current map.

foo:
  <<: *base
  age: 10

bar:
  <<: *base
  age: 20

# foo and bar would also have name: Everyone has same name

# YAML also has tags, which you can use to explicitly declare types.
explicit_string: !!str 0.5
# Some parsers implement language specific tags, like this one for Python's
# complex number type.
python_complex_number: !!python/complex 1+2j

# We can also use yaml complex keys with language specific tags
? !!python/tuple [5, 7]
: Fifty Seven
# Would be {(5, 7): 'Fifty Seven'} in Python

####################
# EXTRA YAML TYPES #
####################

# Strings and numbers aren't the only scalars that YAML can understand.
# ISO-formatted date and datetime literals are also parsed.
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14

# The !!binary tag indicates that a string is actually a base64-encoded
# representation of a binary blob.
gif_file: !!binary |
  R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
  OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
  +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
  AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML also has a set type, which looks like this:
set:
  ? item1
  ? item2
  ? item3
or: {item1, item2, item3}

# Sets are just maps with null values; the above is equivalent to:
set2:
  item1: null
  item2: null
  item3: null

...  # document end

Why Validate YAML?

It is essential to ensure a file is syntactically correct when working with YAML files. The strict nature of the YAML language requires formatting to be exact when laying out and defining a file’s data and commands. Typically, developers use a linter in a software development program to analyze the file during the creation process. The linter improves and corrects the code’s quality by verifying and identifying bugs, programming errors, style errors, or other odd formatting issues. It also checks YAML files to ensure they adhere to best formatting practices.

YAML Validation in IDEs

An IDE is an application that allows developers to write and design software. Most modern IDEs either have built-in support or plugin support that users can install to enable a YAML file’s linting. Here are the top five platforms:

  • Microsoft Visual Studio/VSCodium – This well-known IDE uses a RedHat-based linting tool that works well to validate YAML files.
  • Eclipse IDE – Two popular plugins for Eclipse use the YEdit and Yaml editor to manipulate and check YAML files. 
  • Android Studio – This IDE software by JetBrains is one of three IDEs on our list of the most popular development environments. JetBrains offers multiple plugins that are useful for working with YAML files.
  • PyCharm – Has many plugins as well as an internal method to work with YAML files. Users can find these settings below:
    • WindowsFile | Settings | Editor | Code Style | YAML for Windows and
    • LinuxPyCharm | Preferences | Editor | Code Style | YAML for macOS.
  • IntelliJ IDEA – This is another JetBrains IDE used to develop in Java and Javascript. It has numerous plugins used to assist in validating YAML files.

YAML Online Validators

In addition to the software IDEs, multiple online YAML validators exist which perform the same or similar functions as a linter within a software development application. Here are several of the most popular ones. 

Best Practices

  1. Always utilize monospaced fonts when working on a YAML file. This font allows us to quickly spot indentation errors, which can prevent our file from being validated.
  2. Use a code editor. Nothing works better at providing a fast and smooth interface when writing YAML files. Because many IDEs employ either an internal or plugin-based linting function, this makes validating our files that much easier.
  3. If an IDE is not available, employ a linter. 
  4. Never use the default TAB character in a YAML file. Users should only employ the SPACE character. If required, users can configure most modern IDEs to have tabs to behave like spaced characters. 
  5. Only use UTF-8 encoding when saving a YAML file. This provides the best use of the full set of printable Unicode characters. 
  6. Whitespace matters in YAML! YAML uses literal space characters (again, not tabs) to define its structure.
  7. All YAML files should begin with three dashes (—) and end with an ellipsis (…). This is a part of the standard YAML formatting and indicates the beginning and end of a document.
  8. The most typical YAML characters are colons, dashes, and pound symbols, but users can employ a wide range of characters to accomplish many tasks.
  9. Users can use line comments nearly everywhere. 
  10. Use YAML as a Config file. Searching through code to modify a parameter is never fun.
  11. If struggling to write a YAML file, try writing it in Python and then convert it using the pyyaml library to convert the code.
  12. Never store sensitive data in a YAML file.
  13. If unsure, use the YAML reference.

Bottom Line

YAML or YAML Ain’t Markup Language is a human-friendly, easily readable data language widely used in almost all programming mediums today. It uses an indented structure to encompass multiple record types read a logical sequence. YAML requires a structured format to ensure it is cross-compatible with different programming languages. Because YAML is platform-agnostic, developers can utilize it across multiple mediums. Its many uses continue to grow and evolve as new technologies emerge, which require independent and separate configuration files.

We pride ourselves on being The Most Helpful Humans In Hosting™! Our Support Teams are filled with experienced Linux technicians and talented system administrators who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article. Should you have any questions regarding this information, we will always answer any inquiries regarding this article, 24 hours a day, 7 days a week, 365 days a year.

If you are a Fully Managed VPS server, Cloud Dedicated, VMware Private Cloud, VPS reseller server, Managed Cloud Servers, or a Dedicated server owner, and you are uncomfortable with performing any of the steps outlined, we can be reached via chat or support ticket to assist you with this process.

Related articles

Wait! Get exclusive hosting insights

Subscribe to our newsletter and stay ahead of the competition with expert advice from our hosting pros.

Loading form…