TOML Validator and Comment-Safe Formatter

Tidy TOML that keeps its comments and its meaning.

  • Errors by line
  • Comments kept
  • Meaning unchanged
A soft clay comb lying across ribbons: on one side the ribbons are tangled in a small bundle, on the other they lie in neat parallel rows beside a little brush.

The guide

A TOML formatter that keeps your comments must read the file as text, because a parser keeps only the meaning

TOML maps to a table of values and a comment is not a value, so tidying a file without losing its notes means working on the syntax tree and proving nothing moved.

  • line 5, column 1 where a header over dotted keys is refused
  • 205 + 474 TOML 1.0.0 cases in toml-test v2.2.0, valid and invalid
  • 128 or 256 levels of nesting the toml-test notes call plenty
  • 2025-12-18 the date of TOML 1.1.0
Read the full guide Fold the guide 7 min · 15 sources

A configuration file is a small document that two kinds of reader share. A program reads it to learn settings. A person reads it to learn why those settings are what they are, and the why lives in the comments.

TOML says in its first lines what it is for: a minimal format that maps unambiguously to a hash table. Everything in the specification serves that mapping. A comment is not part of it. The specification keeps comments simple: a hash symbol marks the rest of the line, and control characters other than tab are not allowed inside one. The 1.1.0 changelog adds a clarification that comments never affect the tables a parser produces.

What a parser keeps and what it throws away

Hand a file to an ordinary parser and you get back a structure of tables, arrays and values. The Python standard library module for this returns a dictionary, and its documentation states that the module does not support writing TOML. That is a sensible split of duties, and it explains a familiar disappointment.

Any tool that reads the file into data and writes it back out has already lost the comments, the blank lines between groups, the way you spelled a number and the order you chose. None of those were data, so none of them survived.

A formatter that wants to keep them cannot go through the data at all. It needs what a compiler writer calls a concrete syntax tree, a tree that reflects the syntax of the text itself and not only its meaning. The tool here reads each line into its pieces: the indentation, the key with its quotes, the spacing around the equals sign, the value exactly as written, the gap before a comment, the comment, and the line ending. Putting the pieces back together returns the original file byte for byte, and that round trip is checked on every valid case of the official test suite.

Pretty-printing, in the general sense, is the application of stylistic formatting conventions to text files such as source code and markup. TOML defines no canonical layout, so every layout rule on this page is a stated choice of the tool, can be switched off, and by default changes spacing only.

How a table gets defined, and why twice is an error

The part of TOML that surprises people is that a table can come into being in four ways. A header such as [server] defines one. Dotted keys such as fruit.apple.color define a table for every part before the last. An inline table defines itself inside its braces. A double-bracket header starts an array of tables, and every repeat of that header adds an element.

Tables cannot be defined more than once, and that single sentence produces most of the errors a validator reports. The specification is careful about dotted keys: they create and define the tables before their last part, so a later header cannot define one again, while a header can still add sub-tables below it.

Here is a case you can load from the example list. A file says [fruit], then pear.color = "red", then pear.taste.sweet = true, a blank line, and then [fruit.pear]. The dotted keys already defined fruit.pear, so the header at line 5, column 1 is refused, with the line where the table first appeared.

Inline tables are the strictest. They are fully self-contained, so type = { name = "Nail" } followed by type.edible = false is an error at the second line. A bracketed array value is just as fixed: fruits = [] followed by [[fruits]] is refused even though the array is empty.

An array of tables adds one more rule. A header that names a child refers to the most recently defined element of the array. The specification says that reversing that order must produce an error, and that a normal table cannot share a name with an array of tables.

Reading an error: stop once, collect many

A syntax error is different in kind from a definition error. When the text itself stops making sense, such as port = 80 80, nothing after that point can be read with confidence, so any parser must stop at the first one. The page says so, and lists the definition errors found above that point.

Definition errors need no such mercy. The text parsed fine; it is the meaning that conflicts. They are collected from the tree, all of them, each with its line and column counted in characters from 1, so a file with three duplicated keys reports three problems in one pass.

Every message names the rule in plain words. The rule is not decoration. Two people reading the same line often disagree about whether it is a typo or a design problem, and a stated rule settles the argument.

Limits belong in the same category. The notes of the toml-test project observe that a short document with several thousand nested tables can use an inordinate amount of memory, and that a limit of 128 or 256 levels is more than enough for real documents. This page accepts 256 levels and refuses deeper nesting with a message that names the cap and the position, because a hostile file should end in a sentence and not in a frozen tab. Large files are checked in a web worker, a background thread that leaves the page free to respond, and a Cancel button ends the work.

Which rewrites are safe

Not every tidy-up is neutral, and the specification says which are. Key and value pairs within a table are not guaranteed to be in any particular order, so sorting keys inside one table cannot change what a parser sees. Whole sections are more delicate. Tables can move, but only if their parents and children keep a sensible relationship.

Arrays of tables must never be reordered, because the elements are inserted in the order encountered and a later header points at the latest one. Swap two [[plugins]] blocks and you have changed the data, perhaps silently. This tool keeps every array-of-tables section in its original relative order, and moves any child section together with the element it belongs to.

Comments follow a stated rule of attachment. A comment on its own line belongs to the next key line in its section. A comment after a value belongs to that line. Comments directly above a header belong to that header. The first block of the file, when a blank line follows it, stays at the top. When a line moves, its comments move with it.

A worked example from the tool’s own check: the line serde={version="1.0",features=["derive"]} has 41 characters, and under the default rules it becomes serde = { version = "1.0", features = ["derive"] }, 50 characters. Only spacing changed. A trailing note written after three spaces is pulled back to two. Nothing else moved.

The proof is what lets you trust the result. After formatting, the text is read again from scratch. Its data must equal the input’s, value by value and type by type, and its comments must sit on the same lines in the same order, or the format is refused and the first difference is shown. Formatting the result a second time must change nothing.

The same reading is checked against the official suite. For TOML 1.0.0 the toml-test lists name 205 + 474 cases, valid and invalid; for 1.1.0 they name 214 valid and 467 invalid. The tool accepts every valid case, refuses every invalid one, and its formatted output reads back to the suite’s own expected data under every sorting mode.

Version 1.1.0, released on 2025-12-18, relaxed inline tables, added two escapes and made seconds optional, so the page carries a version switch. It also says that implementations are free to support any integer size. This tool stays at the signed 64-bit range under both versions, so a file it accepts is readable by the widest set of programs.

Lint, and what no check can promise

Lint is not an error. The specification calls defining tables out of order discouraged, not forbidden: [fruit.apple], then [animal], then [fruit.orange] is valid, though the recommended order keeps the fruit tables together. A validator that reported this as an error would be wrong, so these notes live in their own list, and each can be switched off. The same goes for mixed quote styles, long lines, trailing whitespace, a missing final line ending and mixed tabs and spaces. They make files harder to review. They do not make them invalid.

A valid file can still be refused by the program that reads it. TOML defines how text maps to tables and types. It does not know which keys your program expects or which types it will accept for them. A misspelled key is perfectly good TOML, and only that program’s own documentation or schema can say it is wrong. A page like this one can promise that the file is well formed and that its tidy copy means the same thing. It cannot promise that the program will like what it says.

Sources

Every source consulted for this guide. Links checked November 9, 2026.

  1. TOML: English v1.0.0 TOML (toml.io), Tom Preston-Werner, Pradyun Gedam, et al.
  2. toml/toml.md at 1.0.0 toml-lang/toml on GitHub, MIT License
  3. toml/LICENSE at 1.0.0 toml-lang/toml on GitHub
  4. TOML: English v1.1.0 TOML (toml.io)
  5. toml/CHANGELOG.md at 1.1.0 toml-lang/toml on GitHub, MIT License
  6. toml-lang/toml-test at v2.2.0 toml-lang/toml-test on GitHub, MIT License
  7. toml-test/LICENSE at v2.2.0 toml-lang/toml-test on GitHub
  8. toml-test/tests/files-toml-1.0.0 at v2.2.0 toml-lang/toml-test on GitHub, MIT License
  9. toml-test/tests/files-toml-1.1.0 at v2.2.0 toml-lang/toml-test on GitHub, MIT License
  10. TOML - Wikipedia Wikipedia
  11. Parse tree - Wikipedia Wikipedia
  12. Pretty-printing - Wikipedia Wikipedia
  13. Configuration file - Wikipedia Wikipedia
  14. Web Workers API - Web APIs MDN Web Docs (Mozilla)
  15. tomllib - Parse TOML files Python Software Foundation, Python 3.15 documentation