Escaping "Validation Hell" with JSON-Validace (E1).

Escaping "Validation Hell" with JSON-Validace (E1).

Introduction

Ever had a tough time validating user inputs that you end up writing complex and messy codes? Then this article is for you.

Hi, I’m Evans Allison and in this article, I’ll be sharing with you a powerful npm tool that can help you write validation codes easily, first I would like to tell you a short story.

A Short Story

Some time not long ago, a frontend developer named Pamela Halliday, worked remotely with an online business. Her role was to develop user interfaces for websites and lead a team of front-end and back-end developers.

Every website they’ve built requires a form, where users provide their information during signing up or logging in. The information they provide needs validation (making sure that their information meets some criteria) before they are sent to the server for storage. Without it, someone could easily provide information to the wrong form field (for example, someone could input their name where needs their email, also, they can provide passwords that are not strong enough in length and complexity).

Pamela noticed that every code they’ve written for validating users' information differed in every website they’ve built. She was worried about some being a bit messy, hard to read and less structured. Her worries were over when she heard of “JSON-validace” from a friend and started using it.

So, What is JSON-Validace?

JSON-Validace is an open-source schema-based validator that allows developers to validate JSON (Javascript Object Notation) and javascript objects using an object schema.

JSON Validace is an npm package created by Paul Ezekiel-Hart, a software engineer, to aid developers in validating users’ data in form of a javascript object or JSON.

When we get users’ information (such as users’ names, emails, passwords, etc) we can create a javascript object or JSON with them and validate this object with json-validace. As we progress further, we’ll get to understand what this package is and how it works.

How to use the package?

Here are simple steps for using JSON-Validace on your project.

  1. Install the package

    Firstly, you need to install the package on any of your projects by running the following commands on your terminal:

npm install --save json-validace

Or, if you prefer using yarn to install npm packages:

yarn add json-validace
  1. Create a new file

    In your project, create a new .js file where you can import and use the package.

  2. Import the package

    In the newly created .js file, you can import the package with the following codes:

     // Filename: validace.schema.js
     const jsonValidace = require("json-validace")
    

Note: If you're using javascript frameworks (such as Vue, React, Svelte, Angular, etc.), then importing the package should be like this:

// Filename: validace.schema.js
import jsonValidace from 'json-validace'

//Or

import { Schema } from "json-validace"
  1. Create a Schema

    After importing the package, the next step is to create a schema and name it based on its purpose.

    Example:

     // Filename: validace.schema.js
     const jsonValidace = require("json-validace")
    
     // create a validace schema for login-validation.
     const loginSchema = new jsonValidace.Schema({
         password: {
             type: "string",
             required: true,
             minLength: 10,
             maxLength: 20
         },
         email: {
             type: "email",
             required: true
         }
     })
    
     // export loginSchema
     module.exports = loginSchema
    
  2. Import the newly created Schema

    Now, you can import the schema you created to wherever you’re getting your users’ data.

    Note: The following code is to test that the schema works.

     // Filename: test.js
     const { loginSchema } = require("validace.schema.js")
    
     // Usage
     const result = loginSchema.validate({
         email: "test@gmail.com",
         password: "slickcodes"
     })
    
     // log result to console | terminal
     console.log(result)
    
     # Result: in terminal | console
      {
           error: null,
           isValid: true,
           data: { email: 'test@gmail.com', password: 'slickcodes' }
       }
    

Conclusion

Validation is a vital process when developing applications for users. As it's essential it also gets complex to handle at some point. With JSON-Validace, writing validation codes become easy, neat and fast. I hope you find this article helpful.

For more clarity, the documentation link: https://www.npmjs.com/package/json-validace

Thank you. 🖤