{"id":65473,"date":"2019-05-17T04:15:58","date_gmt":"2019-05-17T11:15:58","guid":{"rendered":"http:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/"},"modified":"2019-05-17T04:15:58","modified_gmt":"2019-05-17T11:15:58","slug":"creating-your-own-react-validation-library-the-basics-part-1","status":"publish","type":"post","link":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/","title":{"rendered":"Creating Your Own React Validation Library: The Basics (Part 1)"},"content":{"rendered":"<link rel=\"canonical\" href=\"https:\/\/www.smashingmagazine.com\/2019\/05\/react-validation-library-basics-part1\/\"><title>Creating Your Own React Validation Library: The Basics (Part 1)<\/title><\/p>\n<article>\n<header>\n<h1>Creating Your Own React Validation Library: The Basics (Part 1)<\/h1>\n<address>Kristofer Selbekk<\/address>\n<p>                  <time datetime=\"2019-05-16T13:00:59+02:00\">2019-05-16T13:00:59+02:00<\/time><time datetime=\"2019-05-17T11:05:38+00:00\">2019-05-17T11:05:38+00:00<\/time><\/header>\n<p>I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I do is constructing more or less complex forms with validation rules that depend on earlier choices and paths. Understanding how a form validation library would work is paramount.<\/p>\n<p>Last year, I wrote one such form validation library. I named it \u201c<a href=\"https:\/\/github.com\/selbekk\/calidation\">Calidation<\/a>\u201d, and you can read the introductory blog post <a href=\"https:\/\/medium.com\/@selbekk\/introducing-calidation-7d9a79453f7\">here<\/a>. It\u2019s a good library that offers a lot of flexibility and uses a slightly different approach than the other ones on the market. There are tons of other great libraries out there too, though \u2014 mine just worked well for <em>our<\/em> requirements.<\/p>\n<p>Today, I\u2019m going to show you how to write <em>your very own validation library<\/em> for React. We will go through the process step by step, and you\u2019ll find CodeSandbox examples as we go along. By the end of this article, you will know how to write your own validation library, or at the very least have a deeper understanding of how other libraries implement \u201cthe magic of validation\u201d.<\/p>\n<ul>\n<li><strong>Part 1: The Basics<\/strong><\/li>\n<li>Part 2: The Features<\/li>\n<li>Part 3: The Experience<\/li>\n<\/ul>\n<div data-component=\"FeaturePanel\" data-audience=\"non-subscriber\" data-remove=\"true\"><\/div>\n<h3>Step 1: Designing The API<\/h3>\n<p>The first step of creating any library is designing how it\u2019s going to be used. It lays the foundation for a lot of the work to come, and in my opinion, it\u2019s the single most important decision you\u2019re going to make in your library.<\/p>\n<p>It\u2019s important to create an API that\u2019s \u201ceasy to use\u201d, and yet flexible enough to allow for future improvements and advanced use cases. We\u2019ll try to hit both of these goals.<\/p>\n<p>We\u2019re going to create a <a href=\"https:\/\/reactjs.org\/docs\/hooks-custom.html\">custom hook<\/a> that will accept a single configuration object. This will allow for future options to be passed without introducing breaking changes.<\/p>\n<h4>A Note On Hooks<\/h4>\n<p>Hooks is a pretty new way of writing React. If you\u2019ve written React in the past, you might not recognize a few of these concepts. In that case, please have a look at <a href=\"https:\/\/reactjs.org\/docs\/hooks-intro.html\">the official documentation<\/a>. It\u2019s incredibly well written, and takes you through the basics you need to know.<\/p>\n<p>We\u2019re going to call our custom hook <code>useValidation<\/code> for now. Its usage might look something like this:<\/p>\n<div>\n<pre><code>const config = {\n  fields: {\n    username: {\n      isRequired: { message: 'Please fill out a username' },\n    },\n    password: {\n      isRequired: { message: 'Please fill out a password' },\n      isMinLength: { value: 6, message: 'Please make it more secure' }\n    }\n  },\n  onSubmit: e => { \/* handle submit *\/ }\n};\nconst { getFieldProps, getFormProps, errors } = useValidation(config);\n<\/code><\/pre>\n<\/div>\n<p>The <code>config<\/code> object accepts a <code>fields<\/code> prop, which sets up the validation rules for each field. In addition, it accepts a callback for when the form submits.<\/p>\n<p>The <code>fields<\/code> object contains a key for each field we want to validate. Each field has its own config, where each key is a validator name, and each value is a configuration property for that validator. Another way of writing the same would be:<\/p>\n<div>\n<pre><code>{\n  fields: {\n    fieldName: {\n      oneValidator: { validatorRule: 'validator value' },\n      anotherValidator: { errorMessage: 'something is not as it should' }\n    }\n  }\n}\n<\/code><\/pre>\n<\/div>\n<p>Our <code>useValidation<\/code> hook will return an object with a few properties \u2014 <code>getFieldProps<\/code>, <code>getFormProps<\/code> and <code>errors<\/code>. The two first functions are what <a href=\"https:\/\/twitter.com\/kentcdodds\">Kent C. Dodds<\/a> calls \u201cprop getters\u201d (<a href=\"https:\/\/kentcdodds.com\/blog\/how-to-give-rendering-control-to-users-with-prop-getters\">see here for a great article on those<\/a>), and is used to get the relevant props for a given form field or form tag. The <code>errors<\/code> prop is an object with any error messages, keyed per field.<\/p>\n<p>This usage would look like this:<\/p>\n<div>\n<pre><code>const config = { ... }; \/\/ like above\nconst LoginForm = props => {\n  const { getFieldProps, getFormProps, errors } = useValidation(config);\n  return (\n    <form {...getFormProps()}>\n      <label>\n        Username<br\/>\n        <input {...getFieldProps('username')} \/>\n        {errors.username && <div className=\"error\">{errors.username}<\/div>}\n      <\/label>\n      <label>\n        Password<br\/>\n        <input {...getFieldProps('password')} \/>\n        {errors.password && <div className=\"error\">{errors.password}<\/div>}\n      <\/label>\n      <button type=\"submit\">Submit my form<\/button>\n    <\/form>\n  );\n};\n<\/code><\/pre>\n<\/div>\n<p>Alrighty! So we\u2019ve nailed the API.<\/p>\n<ul>\n<li><a href=\"https:\/\/codesandbox.io\/s\/ovm0vyv1lq\">See CodeSandbox demo<\/a><\/li>\n<\/ul>\n<p>Note that we\u2019ve created a mock implementation of the <code>useValidation<\/code> hook as well. For now, it\u2019s just returning an object with the objects and functions we require to be there, so we don\u2019t break our sample implementation.<\/p>\n<div><\/div>\n<h3>Storing The Form State ?<\/h3>\n<p>The first thing we need to do is storing all of the form state in our custom hook. We need to remember the values of each field, any error messages and whether or not the form has been submitted. We\u2019ll use the <a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usereducer\"><code>useReducer<\/code><\/a> hook for this since it allows for the most flexibility (and less boilerplate). If you\u2019ve ever used <a href=\"https:\/\/redux.js.org\/introduction\/getting-started\">Redux<\/a>, you\u2019ll see some familiar concepts \u2014 and if not, we\u2019ll explain as we go along! We\u2019ll start off by writing a reducer, which is passed to the <code>useReducer<\/code> hook:<\/p>\n<div>\n<pre><code>const initialState = {\n  values: {},\n  errors: {},\n  submitted: false,\n};\n\nfunction validationReducer(state, action) {\n  switch(action.type) {\n    case 'change': \n      const values = { ...state.values, ...action.payload };\n      return { \n        ...state, \n        values,\n      };\n    case 'submit': \n      return { ...state, submitted: true };\n    default: \n      throw new Error('Unknown action type');\n  }\n}\n<\/code><\/pre>\n<\/div>\n<h3>What\u2019s A Reducer? ?<\/h3>\n<p>A reducer is a function that accepts an object of values and an \u201caction\u201d and returns an augmented version of the values object.<\/p>\n<p>Actions are plain JavaScript objects with a <code>type<\/code> property. We\u2019re using a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/switch\"><code>switch<\/code> statement<\/a> to handle each possible action type.<\/p>\n<p>The \u201cobject of values\u201d is often referred to as <strong>state<\/strong>, and in our case, it\u2019s the state of our validation logic.<\/p>\n<p>Our state consists of three pieces of data \u2014 <code>values<\/code> (the current values of our form fields), <code>errors<\/code> (the current set of error messages) and a flag <code>isSubmitted<\/code> indicating whether or not our form has been submitted at least once.<\/p>\n<p>In order to store our form state, we need to implement a few parts of our <code>useValidation<\/code> hook. When we call our <code>getFieldProps<\/code> method, we need to return an object with the value of that field, a change-handler for when it changes, and a name prop to track which field is which.<\/p>\n<div>\n<pre><code>function validationReducer(state, action) {\n  \/\/ Like above\n}\n\nconst initialState = { \/* like above *\/ };\n\nconst useValidation = config => {\n  const [state, dispatch] = useReducer(validationReducer, initialState);\n  \n  return {\n    errors: state.errors,\n    getFormProps: e => {},\n    getFieldProps: fieldName => ({\n      onChange: e => {\n        if (!config.fields[fieldName]) {\n          return;\n        }\n        dispatch({ \n          type: 'change', \n          payload: { [fieldName]: e.target.value } \n        });\n      },\n      name: fieldName,\n      value: state.values[fieldName],\n    }),\n  };\n};\n<\/code><\/pre>\n<\/div>\n<p>The <code>getFieldProps<\/code> method now returns the props required for each field. When a change event is fired, we ensure that field is in our validation configuration, and then tell our reducer a <code>change<\/code> action took place. The reducer will handle the changes to the validation state.<\/p>\n<ul>\n<li><a href=\"https:\/\/codesandbox.io\/s\/6nx2zzo0rn\">See CodeSandbox demo<\/a><\/li>\n<\/ul>\n<div><\/div>\n<h3>Validating Our Form ?<\/h3>\n<p>Our form validation library is looking good, but isn\u2019t doing much in terms of validating our form values! Let\u2019s fix that. ?<\/p>\n<p>We\u2019re going to validate all fields on every change event. This might not sound very efficient, but in the real world applications I\u2019ve come across, it isn\u2019t really an issue.<\/p>\n<p>Note, we\u2019re not saying you have to show every error on every change. We\u2019ll revisit how to show errors only when you submit or navigates away from a field, later in this article.<\/p>\n<h4>How To Pick Validator Functions<\/h4>\n<p>When it comes to validators, there are tons of libraries out there that implement all the validation methods you\u2019d ever need. You can also write your own if you want. It\u2019s a fun exercise!<\/p>\n<p>For this project, we\u2019re going to use a set of validators I wrote some time ago \u2014 <a href=\"https:\/\/github.com\/selbekk\/calidators\"><code>calidators<\/code><\/a>. These validators have the following API:<\/p>\n<pre><code>function isRequired(config) {\n  return function(value) {\n    if (value === '') {\n      return config.message;\n    } else {\n      return null;\n    }\n  };\n}\n\n\/\/ or the same, but terser\n\nconst isRequired = config => value => \n    value === '' ? config.message : null;\n<\/code><\/pre>\n<p>In other words, each validator accepts a configuration object and returns a fully-configured validator. When <em>that<\/em> function is called with a value, it returns the <code>message<\/code> prop if the value is invalid, or <code>null<\/code> if it\u2019s valid. You can look at how some of these validators are implemented by looking at <a href=\"https:\/\/github.com\/selbekk\/calidators\/tree\/master\/src\">the source code<\/a>.<\/p>\n<p>To access these validators, install the <code>calidators<\/code> package with <code>npm install calidators<\/code>.<\/p>\n<h4>Validate a single field<\/h4>\n<p>Remember the config we pass to our <code>useValidation<\/code> object? It looks like this:<\/p>\n<div>\n<pre><code>{ \n  fields: {\n    username: {\n      isRequired: { message: 'Please fill out a username' },\n    },\n    password: {\n      isRequired: { message: 'Please fill out a password' },\n      isMinLength: { value: 6, message: 'Please make it more secure' }\n    }\n  },\n  \/\/ more stuff\n}\n<\/code><\/pre>\n<\/div>\n<p>To simplify our implementation, let\u2019s assume we only have a single field to validate. We\u2019ll go through each key of the field\u2019s configuration object, and run the validators one by one until we either find an error or are done validating.<\/p>\n<div>\n<pre><code>import * as validators from 'calidators';\n\nfunction validateField(fieldValue = '', fieldConfig) {\n  for (let validatorName in fieldConfig) {\n    const validatorConfig = fieldConfig[validatorName];\n    const validator = validators[validatorName];\n    const configuredValidator = validator(validatorConfig);\n    const errorMessage = configuredValidator(fieldValue);\n\n    if (errorMessage) {\n      return errorMessage;\n    }\n  }\n  return null;\n}\n<\/code><\/pre>\n<\/div>\n<p>Here, we\u2019ve written a function <code>validateField<\/code>, which accepts the value to validate and the validator configs for that field. We loop through all of the validators, pass them the config for that validator, and run it. If we get an error message, we skip the rest of the validators and return. If not, we try the next validator.<\/p>\n<h3>Note: On validator APIs<\/h3>\n<p>If you choose different validators with different APIs (like the very popular <a href=\"https:\/\/www.npmjs.com\/package\/validator\"><code>validator.js<\/code><\/a>), this part of your code might look a bit different. For brevity\u2019s sake, however, we let that part be an exercise left to the reader.<\/p>\n<h3>Note: On for\u2026in loops<\/h3>\n<p>Never used <code>for...in<\/code> loops before? That\u2019s fine, this was my first time too! Basically, it iterates over the keys in an object. You can read more about them at <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/for...in\">MDN<\/a>.<\/p>\n<h4>Validate all the fields<\/h4>\n<p>Now that we\u2019ve validated one field, we should be able to validate all fields without too much trouble.<\/p>\n<div>\n<pre><code>function validateField(fieldValue = '', fieldConfig) {\n  \/\/ as before\n}\n\nfunction validateFields(fieldValues, fieldConfigs) {\n  const errors = {};\n  for (let fieldName in fieldConfigs) {\n    const fieldConfig = fieldConfigs[fieldName];\n    const fieldValue = fieldValues[fieldName];\n\n    errors[fieldName] = validateField(fieldValue, fieldConfig);\n  }\n  return errors;\n}\n<\/code><\/pre>\n<\/div>\n<p>We\u2019ve written a function <code>validateFields<\/code> that accepts all field values and the entire field config. We loop through each field name in the config and validate that field with its config object and value.<\/p>\n<h4>Next: Tell our reducer<\/h4>\n<p>Alrighty, so now we have this function that validates all of our stuff. Let\u2019s pull it into the rest of our code!<\/p>\n<p>First, we\u2019re going to add a <code>validate<\/code> action handler to our <code>validationReducer<\/code>.<\/p>\n<pre><code>function validationReducer(state, action) {\n  switch (action.type) {\n    case 'change':\n      \/\/ as before\n    case 'submit':\n      \/\/ as before\n    case 'validate': \n      return { ...state, errors: action.payload };\n    default:\n      throw new Error('Unknown action type');\n  }\n}\n<\/code><\/pre>\n<p>Whenever we trigger the <code>validate<\/code> action, we replace the errors in our state with whatever was passed alongside the action.<\/p>\n<p>Next up, we\u2019re going to trigger our validation logic from a <a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#useeffect\"><code>useEffect<\/code><\/a> hook:<\/p>\n<div>\n<pre><code>const useValidation = config => {\n  const [state, dispatch] = useReducer(validationReducer, initialState);\n\n  useEffect(() => {\n    const errors = validateFields(state.fields, config.fields);\n    dispatch({ type: 'validate', payload: errors });\n  }, [state.fields, config.fields]);\n  \n  return {\n    \/\/ as before\n  };\n};\n<\/code><\/pre>\n<\/div>\n<p>This <code>useEffect<\/code> hook runs whenever either our <code>state.fields<\/code> or <code>config.fields<\/code> changes, in addition to on first mount.<\/p>\n<h5>Beware Of Bug ?<\/h5>\n<p>There\u2019s a super subtle bug in the code above. We\u2019ve specified that our <code>useEffect<\/code> hook should only re-run whenever the <code>state.fields<\/code> or <code>config.fields<\/code> change. Turns out, \u201cchange\u201d doesn\u2019t necessarily mean a change in value! <code>useEffect<\/code> uses <code>Object.is<\/code> to ensure equality between objects, which in turn uses reference equality. That is \u2014 if you pass a new object with the same content, it won\u2019t be the same (since the object itself is new).<\/p>\n<p>The <code>state.fields<\/code> are returned from <code>useReducer<\/code>, which guarantees us this reference equality, but our <code>config<\/code> is specified inline in our function component. That means the object is re-created on every render, which in turn will trigger the <code>useEffect<\/code> above!<\/p>\n<p>To solve this, we need to use for the <a href=\"https:\/\/github.com\/kentcdodds\/use-deep-compare-effect\"><code>use-deep-compare-effect<\/code><\/a> library by Kent C. Dodds. You install it with <code>npm install use-deep-compare-effect<\/code>, and replace your <code>useEffect<\/code> call with this instead. This makes sure we do a deep equality check instead of a reference equality check.<\/p>\n<p>Your code will now look like this:<\/p>\n<div>\n<pre><code>import useDeepCompareEffect from 'use-deep-compare-effect';\n\nconst useValidation = config => {\n  const [state, dispatch] = useReducer(validationReducer, initialState);\n\n  useDeepCompareEffect(() => {\n    const errors = validateFields(state.fields, config.fields);\n    dispatch({ type: 'validate', payload: errors });\n  }, [state.fields, config.fields]);\n  \n  return {\n    \/\/ as before\n  };\n};\n<\/code><\/pre>\n<\/div>\n<h5>A Note On useEffect<\/h5>\n<p>Turns out, <code>useEffect<\/code> is a pretty interesting function. <a href=\"https:\/\/twitter.com\/dan_abramov\">Dan Abramov<\/a> wrote <a href=\"https:\/\/overreacted.io\/a-complete-guide-to-useeffect\/\">a really nice, long article on the intricacies of <code>useEffect<\/code><\/a> if you\u2019re interested in learning all there is about this hook.<\/p>\n<p>Now things are starting to look like a validation library!<\/p>\n<ul>\n<li><a href=\"https:\/\/codesandbox.io\/s\/486p81o644\">See CodeSandbox demo<\/a><\/li>\n<\/ul>\n<h3>Handling Form Submission<\/h3>\n<p>The final piece of our basic form validation library is handling what happens when we submit the form. Right now, it reloads the page, and nothing happens. That\u2019s not optimal. We want to prevent the default browser behavior when it comes to forms, and handle it ourselves instead. We place this logic inside the <code>getFormProps<\/code> prop getter function:<\/p>\n<div>\n<pre><code>const useValidation = config => {\n  const [state, dispatch] = useReducer(validationReducer, initialState);\n  \/\/ as before\n  return {\n    getFormProps: () => ({\n      onSubmit: e => {\n        e.preventDefault();\n        dispatch({ type: 'submit' });\n        if (config.onSubmit) {\n          config.onSubmit(state);\n        }\n      },\n    }),\n    \/\/ as before\n  };\n};\n<\/code><\/pre>\n<\/div>\n<p>We change our <code>getFormProps<\/code> function to return an <code>onSubmit<\/code> function, that is triggered whenever the <code>submit<\/code> DOM event is triggered. We prevent the default browser behavior, dispatch an action to tell our reducer we submitted, and call the provided <code>onSubmit<\/code> callback with the entire state \u2014 if it\u2019s provided.<\/p>\n<h3>Summary<\/h3>\n<p>We\u2019re there! We\u2019ve created a simple, usable and pretty cool validation library. There\u2019s still tons of work to do before we can dominate the interwebs, though.<\/p>\n<p>Stay tuned for Part 2 next week!<\/p>\n<div>\n  <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.taterboy.com\/blog\/wp-content\/uploads\/2019\/05\/logo-red-11.png?w=900\" alt=\"Smashing Editorial\"><span>(dm, il)<\/span>\n<\/div>\n<\/article>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating Your Own React Validation Library: The Basics (Part 1) Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00 I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of&#8230;<a class=\"moretag\" href=\"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/\"> Read the full article&#8230;<\/a><\/p>\n","protected":false},"author":4,"featured_media":65474,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[371],"tags":[],"class_list":["post-65473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-user-experience"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Guest Contribution\"\/>\n\t<meta name=\"keywords\" content=\"user experience\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Design for Immersive Technologies | User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies\" \/>\n\t\t<meta property=\"og:description\" content=\"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-05-17T11:15:58+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2019-05-17T11:15:58+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#article\",\"name\":\"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies\",\"headline\":\"Creating Your Own React Validation Library: The Basics (Part 1)\",\"author\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.taterboy.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/logo-red-11.png?fit=60%2C80&ssl=1\",\"width\":60,\"height\":80},\"datePublished\":\"2019-05-17T04:15:58-07:00\",\"dateModified\":\"2019-05-17T04:15:58-07:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#webpage\"},\"articleSection\":\"User Experience\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.taterboy.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/user-experience\\\/#listItem\",\"name\":\"User Experience\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/user-experience\\\/#listItem\",\"position\":2,\"name\":\"User Experience\",\"item\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/user-experience\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#listItem\",\"name\":\"Creating Your Own React Validation Library: The Basics (Part 1)\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#listItem\",\"position\":3,\"name\":\"Creating Your Own React Validation Library: The Basics (Part 1)\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/category\\\/user-experience\\\/#listItem\",\"name\":\"User Experience\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#organization\",\"name\":\"Design for Immersive Technologies\",\"description\":\"User Experience for Games, Virtual Reality (VR) and Augmented\\\/Mixed Reality (AR\\\/MR)\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/\",\"name\":\"Guest Contribution\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d2aad73eb0f48c67b141e9fc978a0e498969791ed623346e361d02d19775b0bb?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Guest Contribution\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#webpage\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/\",\"name\":\"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies\",\"description\":\"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\\u2019ve always thought form validation libraries were pretty cool. I know, it\\u2019s a niche interest to have \\u2014 but we use them so much! At least in my job \\u2014 most of what I\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/author\\\/guestcontribution\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.taterboy.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/logo-red-11.png?fit=60%2C80&ssl=1\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#mainImage\",\"width\":60,\"height\":80},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/2019\\\/05\\\/creating-your-own-react-validation-library-the-basics-part-1\\\/#mainImage\"},\"datePublished\":\"2019-05-17T04:15:58-07:00\",\"dateModified\":\"2019-05-17T04:15:58-07:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/\",\"name\":\"Design for Immersive Technologies\",\"description\":\"User Experience for Games, Virtual Reality (VR) and Augmented\\\/Mixed Reality (AR\\\/MR)\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.taterboy.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies","description":"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I","canonical_url":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/","robots":"max-image-preview:large","keywords":"user experience","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#article","name":"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies","headline":"Creating Your Own React Validation Library: The Basics (Part 1)","author":{"@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author"},"publisher":{"@id":"https:\/\/www.taterboy.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/www.taterboy.com\/blog\/wp-content\/uploads\/2019\/05\/logo-red-11.png?fit=60%2C80&ssl=1","width":60,"height":80},"datePublished":"2019-05-17T04:15:58-07:00","dateModified":"2019-05-17T04:15:58-07:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#webpage"},"isPartOf":{"@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#webpage"},"articleSection":"User Experience"},{"@type":"BreadcrumbList","@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.taterboy.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/category\/user-experience\/#listItem","name":"User Experience"}},{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/category\/user-experience\/#listItem","position":2,"name":"User Experience","item":"https:\/\/www.taterboy.com\/blog\/category\/user-experience\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#listItem","name":"Creating Your Own React Validation Library: The Basics (Part 1)"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#listItem","position":3,"name":"Creating Your Own React Validation Library: The Basics (Part 1)","previousItem":{"@type":"ListItem","@id":"https:\/\/www.taterboy.com\/blog\/category\/user-experience\/#listItem","name":"User Experience"}}]},{"@type":"Organization","@id":"https:\/\/www.taterboy.com\/blog\/#organization","name":"Design for Immersive Technologies","description":"User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)","url":"https:\/\/www.taterboy.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author","url":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/","name":"Guest Contribution","image":{"@type":"ImageObject","@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/d2aad73eb0f48c67b141e9fc978a0e498969791ed623346e361d02d19775b0bb?s=96&d=mm&r=g","width":96,"height":96,"caption":"Guest Contribution"}},{"@type":"WebPage","@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#webpage","url":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/","name":"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies","description":"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.taterboy.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#breadcrumblist"},"author":{"@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author"},"creator":{"@id":"https:\/\/www.taterboy.com\/blog\/author\/guestcontribution\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/www.taterboy.com\/blog\/wp-content\/uploads\/2019\/05\/logo-red-11.png?fit=60%2C80&ssl=1","@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#mainImage","width":60,"height":80},"primaryImageOfPage":{"@id":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/#mainImage"},"datePublished":"2019-05-17T04:15:58-07:00","dateModified":"2019-05-17T04:15:58-07:00"},{"@type":"WebSite","@id":"https:\/\/www.taterboy.com\/blog\/#website","url":"https:\/\/www.taterboy.com\/blog\/","name":"Design for Immersive Technologies","description":"User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.taterboy.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Design for Immersive Technologies | User Experience for Games, Virtual Reality (VR) and Augmented\/Mixed Reality (AR\/MR)","og:type":"article","og:title":"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies","og:description":"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I","og:url":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/","article:published_time":"2019-05-17T11:15:58+00:00","article:modified_time":"2019-05-17T11:15:58+00:00","twitter:card":"summary","twitter:title":"Creating Your Own React Validation Library: The Basics (Part 1) | Design for Immersive Technologies","twitter:description":"Creating Your Own React Validation Library: The Basics (Part 1)Creating Your Own React Validation Library: The Basics (Part 1) Kristofer Selbekk 2019-05-16T13:00:59+02:002019-05-17T11:05:38+00:00I\u2019ve always thought form validation libraries were pretty cool. I know, it\u2019s a niche interest to have \u2014 but we use them so much! At least in my job \u2014 most of what I"},"aioseo_meta_data":{"post_id":"65473","title":null,"description":null,"keywords":null,"keyphrases":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_custom_image_width":null,"og_custom_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"created":"2021-02-07 20:02:56","updated":"2026-09-01 10:41:40","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"twitter_image_url":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.taterboy.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.taterboy.com\/blog\/category\/user-experience\/\" title=\"User Experience\">User Experience<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCreating Your Own React Validation Library: The Basics (Part 1)\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.taterboy.com\/blog"},{"label":"User Experience","link":"https:\/\/www.taterboy.com\/blog\/category\/user-experience\/"},{"label":"Creating Your Own React Validation Library: The Basics (Part 1)","link":"https:\/\/www.taterboy.com\/blog\/2019\/05\/creating-your-own-react-validation-library-the-basics-part-1\/"}],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8wzr5-h21","jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.taterboy.com\/blog\/wp-content\/uploads\/2019\/05\/logo-red-11.png?fit=60%2C80&ssl=1","_links":{"self":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/posts\/65473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/comments?post=65473"}],"version-history":[{"count":0,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/posts\/65473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/media\/65474"}],"wp:attachment":[{"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/media?parent=65473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/categories?post=65473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.taterboy.com\/blog\/wp-json\/wp\/v2\/tags?post=65473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}