What do you think of OCaml's tyxml for generating ultra type-safe HTML?
retained text
<!-- SC_OFF --><div class="md"><p>I'm working on a <em>personal one-man</em> project.</p> <p><em>It's very simple</em>: it's a static website generated from some data stored in a JSON file. I have a prototype written in TypeScript/TSX, consisting of fewer than ten files (views), each containing an average of less than fifty lines of code. Only two of the pages retrieve data from JSON; the rest are simple TSX files that describe the project (pages like "About" or the "Privacy" page).</p> <p>Given how simple it is, I thought I'd go the extra mile and focus heavily on ensuring the entire code, from the build process to the distribution, is 1000% correct, stuff like:</p> <ul> <li><strong>crazy type safety</strong></li> <li><strong>0% chance of logical and consistency errors</strong></li> <li><strong>validation of JSON against a schema</strong></li> <li><strong>HTML (and attributes) that conforms to the specifications</strong> (no mains inside spans, no booleans inside hrefs...)</li> </ul> <p>I started exploring the various programming languages that allowed me to do all these things at once, and I found <strong>OCaml</strong>. It:</p> <ul> <li>is statically typed</li> <li>has <a href="https://github.com/ocaml-community/yojson">yojson</a> to parse JSON and into a nested OCaml tree data structures</li> <li>has <a href="https://github.com/ocsigen/tyxml">tyxml</a> to build valid HTML. If I understood correctly, it has the distinctive feature of performing strict checks on (HTML) element's attributes while libraries in other languages simply accept any string</li> </ul> <p>To be honest, I also looked into <strong>Elm</strong>, which seems to be even more lenient when it comes to error handling; however, <a href="https://github.com/elm/html">its HTML generation library</a> doesn't seem to have strict controls over attributes, not nearly as strict as tyxml.</p> <p>Is there <strong>something even more powerful</strong> that allows me to achieve what I want (code safety and error free) or is OCaml already the best? If so, what has been your experience with it? Any advice?</p> <p>I'll say it again: the project is so simple that you could rewrite it in any programming language in an hour, it's no problem for me. It's a chance to learn something new.</p> <p>Thanks in advance.</p> </div><!-- SC_ON -->   submitted by   <a href="https://www.reddit.com/user/Wise_Stick9613"> /u/Wise_Stick9613 </a> <br /> <span><a href="https://www.reddit.com/r/webdev/comments/1txphoj/what_do_you_think_of_ocamls_tyxml_for_generating/">[link]</a></span>   <span><a href="https://www.reddit.com/r/webdev/comments/1txphoj/what_do_you_think_of_ocamls_tyxml_for_generating/">[comments]</a></span>
document fetched 2026-06-06 08:28