<?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>Forem: Jay Boricha</title>
    <description>The latest articles on Forem by Jay Boricha (@jayboricha07).</description>
    <link>https://forem.com/jayboricha07</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%2F1324948%2F70efd951-f435-4e63-957b-8c57768f3fe6.jpeg</url>
      <title>Forem: Jay Boricha</title>
      <link>https://forem.com/jayboricha07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jayboricha07"/>
    <language>en</language>
    <item>
      <title>Beyond Basic Validation: Elevating Your Form Data Handling Strategy.</title>
      <dc:creator>Jay Boricha</dc:creator>
      <pubDate>Sun, 10 Mar 2024 19:02:25 +0000</pubDate>
      <link>https://forem.com/jayboricha07/beyond-basic-validation-elevating-your-form-data-handling-strategy-1fnn</link>
      <guid>https://forem.com/jayboricha07/beyond-basic-validation-elevating-your-form-data-handling-strategy-1fnn</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Welcome back, everyone. In this blog, we will be talking about what is validation, how it is done at the front end, what tools are used and why should we validate at the front end.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is validation?
&lt;/h2&gt;

&lt;p&gt;Data validation is the process of checking the structure of the data before it is used anywhere in the project. In validation, we already have the structure of the data ready with us and we just check whether the data we have received from the user is matching our structure or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should we do data validation?
&lt;/h2&gt;

&lt;p&gt;Whether it be front end or back end the data validation is one of the most important point part. With the help of data validation one can make sure that the data we are sending from frontend to the backend is in our desired format or not and if it is not in our desired format it won't be able to make it to the backend and it will throw some kind of error. At the same time in the back end when we receive any kind of data first we check the structure means we validate the data which we have received from the front end and if it matches our desired structure only then we proceed further.&lt;/p&gt;

&lt;h2&gt;
  
  
  How validation is done on the front-end side?
&lt;/h2&gt;

&lt;p&gt;To do data validation on the front-end side there are various libraries available like yup, zod, etc.&lt;br&gt;
In this blog, we are gonna use Yup for code examples but you can use the library of your own choice.&lt;/p&gt;



&lt;p&gt;We can install yup in our react or any other front-end project by running the following command in the terminal&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install yup&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;after installing the Yup library we can get started by importing a few things from it as shown in the example below.&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%2Fjcwvpd4zhunmyjyjku1i.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%2Fjcwvpd4zhunmyjyjku1i.png" alt="Image of code sample demostrating the YUP validation" width="800" height="618"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in this example in line no. 4, we are creating a structure of how our data should look like. This structure will be used in the future to validate the input data.&lt;/p&gt;

&lt;p&gt;Yup has a very easy-to-understand syntax that anyone can easily understand by the &lt;code&gt;.object&lt;/code&gt; property we are telling yup that our data will be an object, and with &lt;code&gt;.required()&lt;/code&gt; we are telling that it is a required string. In this schema example we are defining that we this will be the type of object that will have the name as required string, email with string type email and required, and so on. &lt;/p&gt;

&lt;p&gt;After defining our data with help of this Yup schema we can also validate the data with help of &lt;code&gt;.validate()&lt;/code&gt; method, then if there will be any error with validation we can simply get that with help of a catch block.&lt;/p&gt;

&lt;p&gt;This was a simple explanation how we can validate data on the front-end side. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>What is tRPC and how can we use it in our Apps.</title>
      <dc:creator>Jay Boricha</dc:creator>
      <pubDate>Wed, 06 Mar 2024 09:22:29 +0000</pubDate>
      <link>https://forem.com/jayboricha07/what-is-trpc-and-how-can-we-use-it-32ec</link>
      <guid>https://forem.com/jayboricha07/what-is-trpc-and-how-can-we-use-it-32ec</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;There are various ways available out there to make APIs like REST APIs, GraphQL, tRPC, and many more but today in this blog we are gonna have a look at what is tRPC how it works, why one should use tRPC, and a lot more. &lt;/p&gt;

&lt;p&gt;Building our application fully typesafe is a thing that every developer wants to do nowadays so that they would never run into runtime errors like the Reading property of undefined. tRPC allows us to build and use fully typesafe APIs without any schema or code generation like GraphQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why tRPC over REST or GraphQL ?
&lt;/h2&gt;

&lt;p&gt;GraphQL and REST are the dominant way to implement typesafe APIs. If your front-end and back-end both are in Typescript then with the help of tRPC we can share types directly between our client and server, without relying on any extra code generation like we do in GraphQL.&lt;/p&gt;

&lt;p&gt;tRPC is for full-stack Typescript developers. It makes it easy to write endpoints that you can safely use in both the front and backend of your app. Type errors with your API contracts will be caught at build time, reducing the surface for bugs in your application at runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features of tRPC
&lt;/h3&gt;

&lt;p&gt;tRPC comes with a lot of features including &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;well-tested.&lt;/li&gt;
&lt;li&gt;full static type-safety &amp;amp; autocompletion on the client for input, output, and errors.&lt;/li&gt;
&lt;li&gt;No code extra generation.&lt;/li&gt;
&lt;li&gt;tRPC has zero deps and a tiny clint side footprint&lt;/li&gt;
&lt;li&gt;Easy to start with or add to your existing brownfield project.&lt;/li&gt;
&lt;li&gt;The tRPC community has built adapters for all of the most popular frameworks.&lt;/li&gt;
&lt;li&gt;Add typesafe observability to your application.&lt;/li&gt;
&lt;li&gt;Requests made at the same time can be automatically combined into one&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to setup/use tRPC
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Define backend router
&lt;/h3&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%2Fo4ur2h6recic9mh10i5v.jpeg" 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%2Fo4ur2h6recic9mh10i5v.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here in this image we are creating three end point to get all the users, a particular user with I'd and to create a new user.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Creating router instance
&lt;/h3&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%2Fckffq9bl2pqltaeig7nn.jpeg" 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%2Fckffq9bl2pqltaeig7nn.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this step we are initializing the tRPC backend. It's a good practise to do this in a separate file and export the helper function instead of whole tRPC.&lt;/p&gt;

&lt;p&gt;next we will initialize our main router, commonly referred to as &lt;code&gt;appRouter&lt;/code&gt; and we will also export the type of the router which we'll later use on the client.&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%2Fdw338pmwv0s15gmh8rgl.jpeg" 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%2Fdw338pmwv0s15gmh8rgl.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Add a query procedure
&lt;/h3&gt;

&lt;p&gt;use &lt;code&gt;publicProcedure.query()&lt;/code&gt; to add a query procedure to the router.&lt;/p&gt;

&lt;p&gt;In the following image, we are creating a procedure called &lt;code&gt;userList&lt;/code&gt; that returns the list of users from our DB.&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%2Fn07irg9efi5vuiewwn9m.jpeg" 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%2Fn07irg9efi5vuiewwn9m.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Using input parser to validate procedure inputs
&lt;/h3&gt;

&lt;p&gt;To implement the userById procedure, we need to accept input from the client. tRPC lets you define input parsers to validate and parse the input. You can define your own input parser or use a validation library of your choice, like zod, yup, or superstruct.&lt;/p&gt;

&lt;p&gt;You define your input parser on publicProcedure.input(), which can then be accessed on the resolver function as shown below:&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%2Fr5bggnrh9amyyc8pl5en.jpeg" 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%2Fr5bggnrh9amyyc8pl5en.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;here  at line number 8 we will have type of input as :  &lt;code&gt;string&lt;/code&gt;&lt;br&gt;
and at line number 10 we will have type of user as : &lt;code&gt;User | undefined&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  5. Adding a mutation procedure
&lt;/h3&gt;

&lt;p&gt;Similar to GraphQL, tRPC makes a distinction between query and mutation procedures.&lt;/p&gt;

&lt;p&gt;The way a procedure works on the server doesn't change much between a query and a mutation. The method name is different, and the way that the client will use this procedure changes - but everything else is the same!&lt;/p&gt;

&lt;p&gt;Let's add a &lt;code&gt;userCreate&lt;/code&gt; mutation by adding it as a new property on our router object:&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%2Fcg5obmtpjp1vyhlz115s.jpeg" 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%2Fcg5obmtpjp1vyhlz115s.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now we have defined our router, we can serve it. tRPC has many adapter (tRPC is not a server on its own, and must therefore be served using other hosts, such as a simple Node.js HTTP Server, Express, or even Next.js. Most tRPC features are the same no matter which backend you choose. Adapters act as the glue between the host system and your tRPC API.) so you can use any backend framework of your choice. To keep it simple, we'll use the standalone adapter.&lt;/li&gt;
&lt;/ul&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%2F0jetuq7ueuqeqskz97fp.jpeg" 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%2F0jetuq7ueuqeqskz97fp.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  now we can use your backend on the client.
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. setup the tRPC Client
&lt;/h3&gt;

&lt;p&gt;Links in tRPC are similar to links in GraphQL, they let us control the data flow before being sent to the server. In the example above, we use the httpBatchLink, which automatically batches up multiple calls into a single HTTP request&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%2Fbcbqozg56pkth0zc5zze.jpeg" 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%2Fbcbqozg56pkth0zc5zze.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Querying &amp;amp; mutating
&lt;/h3&gt;

&lt;p&gt;You now have access to your API procedures on the trpc object. Try it out!&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%2Fryjcyxdbabrjmwlb862a.jpeg" 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%2Fryjcyxdbabrjmwlb862a.jpeg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now in this image at line no 2 we will automatically have user with type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user : {
            name : string;
            id : string;
     } | undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and createUser with type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const createUser : {
            name : string;
            id : string;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>api</category>
    </item>
  </channel>
</rss>
