<?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: Bello Shehu</title>
    <description>The latest articles on DEV Community by Bello Shehu (@belloshehu).</description>
    <link>https://dev.to/belloshehu</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%2F642241%2F125d0f40-f475-4db7-99a8-ad684bb4583a.jpeg</url>
      <title>DEV Community: Bello Shehu</title>
      <link>https://dev.to/belloshehu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/belloshehu"/>
    <language>en</language>
    <item>
      <title>Using yup to build schema with value parsing and validation.</title>
      <dc:creator>Bello Shehu</dc:creator>
      <pubDate>Sat, 22 Jun 2024 11:36:12 +0000</pubDate>
      <link>https://dev.to/belloshehu/using-yup-to-build-schema-with-value-parsing-and-validation-7if</link>
      <guid>https://dev.to/belloshehu/using-yup-to-build-schema-with-value-parsing-and-validation-7if</guid>
      <description>&lt;p&gt;Validation is a very important aspect of application development, in which user input data is tested against a predetermined format or value to ensure accuracy and quality of data entered into the application. &lt;/p&gt;

&lt;p&gt;There are a bunch of tools used for validating user input both in the frontend and backend applications. Among such tools is &lt;code&gt;yup&lt;/code&gt;; a very popular schema builder with value parsing and validation capabilities. &lt;/p&gt;

&lt;p&gt;Below are examples of how to use yup to build user signup schema with validation in JavaScript.&lt;/p&gt;

&lt;p&gt;First off, we need to install &lt;code&gt;yup&lt;/code&gt; and import it into the file we want to use it: &lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as yup from "yup";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1. Building basic user signup schema&lt;/strong&gt;&lt;br&gt;
This simple yup schema is for user signup containing user's email, password, and username. All fields are required:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const basic_signup_schema = yup.object({
  email: yup.string().email().required("Email required"),
  password: yup.string().required("Password required"),
  username: yup.string().required("Username required"),
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.string&lt;/code&gt; method validates email ID to string. Whereas, the &lt;code&gt;.email&lt;/code&gt; validates any email ID entered. &lt;/p&gt;

&lt;p&gt;If this schema is used with a form library such as Formik, the individual form fields will be validated accordingly. Hence, the form will not be submitted if at least one of the fields is empty. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Adding more validation to signup schema&lt;/strong&gt;&lt;br&gt;
To make the user's password stronger and hence difficult for a hacker to guess, it must contain digits, upper and lower case, special characters and be at least 8 characters long.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const signup_schema = yup.object({
  email: yup.string().email("Invalid email").required("Email required"),
  password: yup
    .string()
    .min(8, "Must be 8 characters long")
    .required("Password required")
    .matches(/[a-z]+/, "Must contain lowercase character")
    .matches(/[A-Z]+/, "Must contain uppercase character")
    .matches(
      /[_@$!%*#?&amp;amp;]+/,
      "Must contain at least one special character among $ ! % * # ? &amp;amp; _ @ "
    )
    .matches(/\d+/, "Must contain digit"),
  username: yup.string().required("Username required"),
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Data validation is used to ensure security, and integrity of data stored in application's database. Hence, using a library such as &lt;code&gt;yup&lt;/code&gt; is an effective and easier way you can ensure that the user enters correct data into your application. &lt;/p&gt;

&lt;p&gt;For suggestions, correction and questions, kindly make a comment in the comment section. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>validation</category>
      <category>yup</category>
      <category>schema</category>
    </item>
  </channel>
</rss>
