DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for easy way to validate data in javascript
alguerocode
alguerocode

Posted on

easy way to validate data in javascript

introduction

we build project and websites , sometimes encounter problems that need solve, but what if we come out with problem of how we validate data in correct way, this is major question and what the data structure you want to validate!, like you want 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 anythings like that.

how to solve ?

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

what is volder

volder is 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 for describe single data.
  • volder support 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);
Enter fullscreen mode Exit fullscreen mode

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

how to help me

there 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.

thank for reading

Latest comments (1)

Collapse
 
lukeshiru profile image
Luke Shiru

If you read the terms:

  • Users must make a good-faith effort to share content that is on-topic, of high-quality, and is not designed primarily for the purposes of promotion or creating backlinks.
  • Posts must contain substantial content β€” they may not merely reference an external link that contains the full post.
  • If a post contains affiliate links, that fact must be clearly disclosed. For instance, with language such as: β€œThis post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.”

If you only want to promote your product like an Ad, you can do it using the listings option in DEV. You should avoid spamming because it goes against the terms.

Thanks.

In defense of the modern web

I expect I'll annoy everyone with this post: the anti-JavaScript crusaders, justly aghast at how much of the stuff we slather onto modern websites; the people arguing the web is a broken platform for interactive applications anyway and we should start over;

React users; the old guard with their artisanal JS and hand authored HTML; and Tom MacWright, someone I've admired from afar since I first became aware of his work on Mapbox many years ago. But I guess that's the price of having opinions.