<?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: Ken</title>
    <description>The latest articles on DEV Community by Ken (@kennetharvinrodriguez).</description>
    <link>https://dev.to/kennetharvinrodriguez</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%2F374406%2Ffe60d7fc-40a8-4596-b5ae-1573f8f43aea.jpg</url>
      <title>DEV Community: Ken</title>
      <link>https://dev.to/kennetharvinrodriguez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kennetharvinrodriguez"/>
    <language>en</language>
    <item>
      <title>Using Vuelidate to verify address data from API</title>
      <dc:creator>Ken</dc:creator>
      <pubDate>Thu, 13 Jul 2023 08:19:48 +0000</pubDate>
      <link>https://dev.to/kennetharvinrodriguez/using-vuelidate-to-verify-address-data-from-api-384p</link>
      <guid>https://dev.to/kennetharvinrodriguez/using-vuelidate-to-verify-address-data-from-api-384p</guid>
      <description>&lt;p&gt;(Cover photo by Liza Summer on &lt;a href="https://www.pexels.com/photo/woman-preparing-box-with-parcel-for-sending-6347513/" rel="noopener noreferrer"&gt;pexels.com&lt;/a&gt;)&lt;/p&gt;




&lt;h2&gt;
  
  
  Background of the problem
&lt;/h2&gt;

&lt;p&gt;Different shopfronts under our organization have their individual checkout processes. In pursuit of a single branding, our organization have now started the initiative of having a centralized checkout. So far, we have successfully migrated two shopfronts and are looking to add more in the next couple of years.&lt;/p&gt;

&lt;p&gt;But with this endeavor comes the responsibility of sanitizing existing data from the shopfronts. Some shopfronts previously have collected address data from customers using their own validation rules. Now, these addresses have to be formatted to satisfy the rules set by the central ecommerce platform and order fulfillment system. All is well with customers who have input their address using the central checkout platform, but it's problematic with those whose addresses were input elsewhere. A malformed address data causes us problems down the line when we fulfill the order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thesis
&lt;/h2&gt;

&lt;p&gt;We use the &lt;a href="https://vuelidate.js.org/" rel="noopener noreferrer"&gt;Vuelidate&lt;/a&gt; plugin to help us validate input data as the customer fills out the address form. We wondered if we can reuse the same plugin for validating address data fetched from the API so we can also reuse the validation rules which were created for the address form. After a few days of scouring the internet for solutions, this &lt;a href="https://gist.github.com/autumnwoodberry/d56d4f28d987b846c5216b2142586f7c" rel="noopener noreferrer"&gt;Github gist&lt;/a&gt; gave us the idea that it could be possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proposed solution
&lt;/h2&gt;

&lt;p&gt;The Vuelidate plugin can only be used inside the context of a Vue component. In the Github gist example, the author used Vuelidate inside a "virtual" Vue component—one which will not be rendered.&lt;/p&gt;

&lt;p&gt;We have a separate file that contains helper functions for validating the address form. Below is a snippet of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3rx8otaa1v4n1px4j7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3rx8otaa1v4n1px4j7x.png" alt="address-helper.js" width="800" height="1030"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the component object returned by the &lt;code&gt;createValidator()&lt;/code&gt; helper function, the &lt;code&gt;validations&lt;/code&gt; property gives a preview into the format of the address data. However, in order to run the validation, the address data value must be referenced within the component context, hence the &lt;code&gt;computed&lt;/code&gt; property. Below is a snippet of how we use the helper function in our Vuex store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqx43qf5wyhp5i9xndxo3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqx43qf5wyhp5i9xndxo3.png" alt="address-store.js" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our order confirmation page, we fetch the customer's address. Upon getting a response from the API endpoint, we invoke the helper function and the &lt;a href="https://vuelidate.js.org/#sub-v-methods" rel="noopener noreferrer"&gt;&lt;code&gt;$touch()&lt;/code&gt;&lt;/a&gt; method to check if the customer's address is malformed. If so, in our case, we redirect the customer to the edit address page to correct the errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges
&lt;/h2&gt;

&lt;p&gt;In the helper function, we copied the existing validation rules from the Vue component that renders the address form. We have tried extracting these validation rules so we can have a single source of truth. However, it causes an error in rendering the address form when we do. We think that Vue needs the &lt;code&gt;validations&lt;/code&gt; property already declared in the component object on runtime, and not be fetched from somewhere.&lt;/p&gt;




&lt;p&gt;Let me know if this post has helped you or if you know a better solution that we could have used.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
