Validate YAML in C#
The same check in .NET with YamlDotNet. It returns the message the page shows:
The two steps catch different things. The Parser checks the syntax: indentation, quotes, brackets and tabs. The deserializer then builds the values, which is where unknown tags, aliases to anchors that do not exist yet and duplicate keys fail.
Common YamlDotNet errors
| Message | Usual cause |
|---|---|
While parsing a block mapping, did not find expected key. | A line indented more or less than the other keys at its level. |
While scanning a plain scalar value, found invalid mapping. | A value that contains : , such as title: Note: read this. Quote the value. |
While parsing a block collection, did not find expected '-' indicator. | A list item and a key at the same indentation, or a list item indented differently from the others. |
While parsing a flow sequence, did not find expected ',' or ']'. | An inline list [a, b that is never closed, or a missing comma. |
While scanning a mapping, found invalid tab as indentation.While scanning a plain scalar, found a tab character that violate indentation. | Tabs. YAML indentation must be spaces. |
While scanning a quoted scalar, found unexpected end of stream. | A quote that is never closed. |
Encountered duplicate key X | The same key twice in one mapping. Without WithDuplicateKeyChecking() the last value wins silently. |
Encountered an unresolved tag '!Ref' | A custom tag. Register it with WithTagMapping, or only check the syntax. |
Alias $x cannot precede anchor declaration | *x is used before &x is defined. |
Expected 'StreamEnd', got 'DocumentStart' | Not a YAML error: Deserialize reads one document, and the file has several. Read them one by one, as the C# above does. |
FAQ
Is valid YAML the same as a valid Kubernetes or Compose file?
No. This checks that the file is YAML that .NET can read. Whether replicas belongs under spec is a schema question for the tool that reads the file.
Why does a file with several documents pass here but fail in my code?
Deserializer.Deserialize reads a single document. Use a Parser and call Deserialize(parser) once per document, as in the C# above.
Which YAML version is checked?
YamlDotNet's, which follows YAML 1.1 and 1.2 syntax. The page reproduces YamlDotNet 18.1.0 exactly, including its quirks.
How do I validate YAML in C#?
Read every event with YamlDotNet's Parser (while (parser.MoveNext()) { }) to check the syntax, then deserialize each document to check tags, anchors and duplicate keys. Both throw a YamlException with the line and column of the problem.
What does "did not find expected key" mean?
It is almost always indentation: a line is indented differently from the other keys at its level, so the parser finds a value where it expects the next key.
Why is my CloudFormation template invalid?
Short-form intrinsic functions such as !Ref and !Sub are custom tags. The syntax is valid YAML, but YamlDotNet's deserializer fails with "Encountered an unresolved tag" until you register the tags with WithTagMapping. Tick Syntax only to check just the syntax.
Is my YAML uploaded?
No. The validator runs in your browser.