<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Renato Sinohara</title>
    <description>The latest articles on DEV Community by Renato Sinohara (@rsinohara).</description>
    <link>https://dev.to/rsinohara</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F148152%2Fe89802ba-9410-4fe3-b8e2-2136d4362af9.jpg</url>
      <title>DEV Community: Renato Sinohara</title>
      <link>https://dev.to/rsinohara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rsinohara"/>
    <language>en</language>
    <item>
      <title>Create a self verifying type library with Zod</title>
      <dc:creator>Renato Sinohara</dc:creator>
      <pubDate>Sun, 12 Sep 2021 07:35:52 +0000</pubDate>
      <link>https://dev.to/rsinohara/create-a-self-verifying-type-library-with-zod-2l74</link>
      <guid>https://dev.to/rsinohara/create-a-self-verifying-type-library-with-zod-2l74</guid>
      <description>&lt;p&gt;If you use Typescript, you probably wished it could do 2 more tings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verify at run time if a type matches your schema&lt;/li&gt;
&lt;li&gt;Make sure no unexpected fields exist in objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Zod
&lt;/h2&gt;

&lt;p&gt;Zod (&lt;a href="https://github.com/colinhacks/zod"&gt;github&lt;/a&gt;) allows you to do exactly that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;zod&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// creating a schema for strings&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foodNameSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;foodNameSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tuna&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; "tuna"&lt;/span&gt;
&lt;span class="nx"&gt;foodNameSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; throws ZodError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zod can be used to create Typescript types, so we also have all the compile time goodness.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;FoodName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;infer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;foodNameSchema&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, some might say parsing will add performance impact. This might matter to you.&lt;br&gt;
But in this article, we show a different usage that dodges that completely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Self verifying type library
&lt;/h2&gt;

&lt;p&gt;Chances are you have an internal library to share your common types. Especially if you are or intend to use micro frontends.&lt;/p&gt;

&lt;p&gt;What we have been doing in my team is to use Zod's parsing to make sure this type library is always up to date with our live formats.&lt;/p&gt;

&lt;p&gt;By ensuring all production types match our schemas exactly, we can rely on our types 100%. Call it contract testing, but we also use the same schemas in our code.&lt;/p&gt;

&lt;p&gt;This also has no runtime impact. In fact, our other projects need not even import Zod.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Any typescript project can import the types or Zod schemas at will. Pure JS projects can also use Zod schemas, if desired.&lt;br&gt;
Zod can be exported to JSON schema, so other languages could also benefit.&lt;/p&gt;

&lt;p&gt;During deployment, the type library crawls and checks the types of API and CSR initial props, making sure the schemas are all correct.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>cicd</category>
      <category>schemavalidation</category>
    </item>
  </channel>
</rss>
