how to validate data in javascript

how to validate data in javascript

Hello | السلام عليكم

Learning new technologies and skills is the most critical thing as a programmer.

Before we get started

=> if there are any issues or miss understood in this blog drop a comment below 👇.

=> follow me on twitter: [alguerocode] (twitter.com/home)

=> pls smash that like buttons if you are interested in this blog and put a random comment below.

introduction

we build projects and websites, sometimes encounter problems that need solving, but what if we come out with a problem of how we validate data incorrect way, this is a major question and what data structure you want to validate!, like you won't validate and email and password you want to add specific rules to keep users Entering emails correctly, but that too tough or you are not expert in validating strings or regex or anything like that.

how to solve?

developers like to use the validation library to solve their problems and focus on their business, so this point helps me to start building volder validation library helping people to describe data in good structure and well design.

what is volder

volder is a javascript npm package, it's powerful Object schema validation, it lets you describe your data using a simple and readable schema and transform a value to match the requirements, it has custom error messages, custom types and nested schemas.

let's take an example

in this example we use volder package for user login validation, first thing install volder package npm install --save volder

volder package has

  • Volder constructor to describe and structure your multiple data.
  • SingleVolder function it's used to describe single data.
  • volder supports other types like Email or CreditCard.

this code shows you how to describe your data for user validation

import { Volder, Email } from 'volder';


const userSchema = new Volder({
    username: {
        type: [String, 'username must be in string'],
        alphanumeric: [true, 'username should only contain letters and numbers'],
        minLength: [4, 'username at least 4 characters'],
        maxLength: [16, 'username at most 16 characters'],
        required: [true, 'username is required'],
        trim: true
    },
    email: {
        type: [String ,'email must be in string'],
        pattern: [Email, 'not valid email'],
        maxLength: [150, 'email be at most 150 characters'],
        required: [ true,'email is required'],
        trim: true
    },
    password: {
        type: [String , 'password must be in string'],
        minLength:[8, 'password should be at least 8 characters'],
        maxLength: [30, 'password should be at most 30 characters'],
        required: [ true, 'password is required'],
        matches: ["^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]$", 'password must contain numbers and letters']

    }
})

const { valid, errors, value } = userSchema.validate(input);

see more example: https://github.com/alguerocode/js-volder

how to help me

there is a variety of ways to help me for example install volder and use it in your project, contributing to volder repository or just add a star ⭐ to volder repository and I will thank you very much.

All that being said, take care of yourselves, have a wonderful day and I hope to see you again soon.

social media:

=> follow me on twitter: [alguerocode] (twitter.com/home)

=> follow me on github: [alguerocode] (github.com/alguerocode)

=> support link: "Buy Me A Coffee"

See My projects, don't forget to add a star that is helping me grow.

=> Volder npm package

=> masterJS resource

thank for reading

Did you find this article valuable?

Support salah alhashmi by becoming a sponsor. Any amount is appreciated!