Is my YAML valid?
Paste the file and click Validate. If it parses, you'll see "Valid YAML." (with the number of documents when there's more than one). If not, you get the parser's first error with its line and column, followed by a few lines of your input and a caret pointing at the exact character. Most YAML errors are indentation or quoting mistakes that this pinpoints in seconds.
The check is a parse: the js-yaml library (version 5.4.2) loads every document in the input using the YAML 1.2 core schema from the YAML 1.2.2 specification, plus the YAML 1.1 merge key that many configuration files use. That catches syntax errors and a few structural ones (duplicate keys, undefined aliases). It does not lint style, and it doesn't know what your file is supposed to contain.
Reading an error
Here's what the validator prints for a nested block whose third line is indented by one space instead of two:
Invalid YAML: bad indentation of a mapping entry (3:2)
1 | a:
2 | b: 1
3 | c: 2
------^
(3:2) means line 3, column 2. The caret sits under the character where the parser gave up, which is usually at or just after the real mistake. Fix that, validate again, and repeat; only the first error is reported each time.
Errors you'll actually hit
| Message | Typical cause | Fix |
|---|---|---|
| bad indentation of a mapping entry | A key is indented differently from its siblings | Line up keys at the same depth with spaces |
| bad indentation of a mapping entry, on a single line | An unquoted value contains colon-space, e.g. title: Note: read this |
Quote the value: title: "Note: read this" |
| tab characters must not be used in indentation | A tab before a key | Replace tabs with spaces; YAML forbids tabs for indentation |
| duplicated mapping key | The same key appears twice in one mapping | Delete or rename one; the YAML spec requires unique keys |
| unidentified alias "name" | *name used before, or without, &name |
Define the anchor first, or fix the spelling |
| unknown scalar tag / unknown mapping tag | !Ref, !!binary, !!set and other tags |
Remove the tag to check the rest of the syntax |
What passes but might still be wrong
A clean result only means the text parses. These all validate, and all can bite later:
- Unexpected types.
version: 1.10parses as the number 1.1, andzip: 01234as 1234. Quote values that must stay text. You can see the resolved types by running the file through the YAML to JSON converter. - Merge keys in other tools. A merge key line (two less-than signs, a colon and an alias) is applied here, as most YAML tools do. A strict YAML 1.2 parser treats it as an ordinary key, so check how the tool that reads your file behaves if you rely on merging.
- Wrong field names. The validator has no schema, so typos in key names pass silently.
- Strings that look like other things.
yes,no,onandoffare strings under YAML 1.2. Older YAML 1.1 parsers (still used by some tools) read them as booleans, so the same file can behave differently elsewhere.
A quick checking routine
- Validate here to fix syntax, one error at a time.
- Convert to JSON to confirm values came out with the types you meant.
- Run the format's own tool (for example
kubectl apply --dry-run=clientordocker compose config) for schema-level checks.
For editing with syntax highlighting and indentation guides, open the file in the online YAML editor. If your data is JSON rather than YAML, use the JSON validator, which gives JSON-specific error messages; for XML documents there's the XML validator.
Last updated