<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Spiralstack: Notes</title>
  <link rel="alternate" type="text/html" href="https://spiralstack.com/notes"/>
  <link rel="self" type="application/atom+xml" href="https://spiralstack.com/feeds/atom/notes"/>
  <id>https://spiralstack.com/feeds/atom/notes</id>
  <icon>https://spiralstack.com/build/_assets/logo-128-3RCJGPF7.png</icon>
  <logo>https://spiralstack.com/build/_assets/logo-512-LOD6LFU2.png</logo>
  <author>
    <name>spiralstack</name>
  </author>
  <updated>2022-02-12T21:55:14.606Z</updated>
  <rights>Copyright © 2022, Spiralstack</rights>
  <entry>
    <title>Validate schemas and declare types with Zod</title>
    <id>https://spiralstack.com/notes/validate-schemas-and-declare-types-with-zod</id>
    <updated>2022-02-12T21:55:14.606Z</updated>
    <published>2022-02-12T21:32:50.863Z</published>
    <link rel="alternate" href="https://spiralstack.com/notes/validate-schemas-and-declare-types-with-zod"/>
    <summary type="text">Zod is a Typescript package that lets you declare your data types and do runtime type checking.</summary>
    <content type="html" xml:base="https://spiralstack.com/" xml:lang="en"><![CDATA[ <div data-reactroot=""><p>Zod is a Typescript package that lets you declare your data types and do runtime type checking.</p><p>Lots of possible utilities for this <a href="https://github.com/colinhacks/zod">library</a>. Validation and <a href="https://spiralstack.com/articles/complex-type-guards-for-the-unknown">type guards</a> come to mind first, but Zod also includes a lot of helpful methods such as <code>default</code> and <code>transform</code>.</p><h2>Quick example</h2><pre><code data-lang="typescript">import { z } from &#x27;zod&#x27;

const Post = z.object({
  id: z.string().uuid(),
  title: z.string().min(1),
  body: z.string()
})

// Declare the type
type Post = z.infer&lt;typeof Post&gt;

const myPost = {
  id: &#x27;c2cffa40-8169-4e9b-bce4-af21bc17b033&#x27;,
  title: &#x27;Validate schemas and declare types with Zod&#x27;,
  body: &#x27;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&#x27;
}

Post.safeParse(myPost)

// {
//   success: true,
//   data: {
//     id: &#x27;c2cffa40-8169-4e9b-bce4-af21bc17b033&#x27;,
//     title: &#x27;Validate schemas and declare types with Zod&#x27;,
//     body: &#x27;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&#x27;
//   }
// }</code></pre><p></p><p></p></div> ]]></content>
  </entry>
  <entry>
    <title>Syntax highlighting ASTs</title>
    <id>https://spiralstack.com/notes/syntax-highlighting-asts</id>
    <updated>2022-01-28T22:46:00.477Z</updated>
    <published>2022-01-28T22:24:07.663Z</published>
    <link rel="alternate" href="https://spiralstack.com/notes/syntax-highlighting-asts"/>
    <summary type="text">Useful libraries for coloring programming language syntax.</summary>
    <content type="html" xml:base="https://spiralstack.com/" xml:lang="en"><![CDATA[ <div data-reactroot=""><p>Useful libraries for coloring programming language syntax.</p><p><a href="https://github.com/wooorm/lowlight">Lowlight</a> and <a href="https://github.com/wooorm/refractor">Refractor</a> are two interesting libraries that wrap <a href="https://highlightjs.org/">highlight.js</a> and <a href="https://prismjs.com/">Prism</a> respectively. </p><p>From <a href="https://wooorm.com/">Titus Wormer</a>:</p><blockquote><p>Virtual syntax highlighting for virtual DOMs and non-HTML things.</p></blockquote><p>Instead of returning a HTML string with <code>&lt;span&gt;</code>s and <code>class</code> names, it returns an array of objects that you can use to create HTML or anything else really. Super useful for creating React components without using something like <a href="https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml"><code>dangerouslySetInnerHTML</code></a>.</p></div> ]]></content>
  </entry>
</feed>
