npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

micro-validator

v0.0.5

Published

A minimalistic & extensible validation library for javascript

Downloads

54

Readme

micro-validator

A minimalistic & extensible validation library for javascript

How to install

$ yarn add micro-validator

Usage

import microValidator from 'micro-validator' 

const validate = (data) => {
    const errors = microValidator.validate({
        name: {
            required: {
                errorMsg: `Company Name is required`
            }
        },
        email: {
            required: {
                errorMsg: `Email is required`
            },
            email: {
                errorMsg: `Enter a valid email`
            }
        },
        website: {
            required: {
                errorMsg: `Apply link is required`
            },
            validUrl: {
                errorMsg: `Please enter a valid url`
            }
        }
    }, data)
    
    return errors
}

validate({
    name: 'micro-validator',
    email: '[email protected]',
    website: 'github.com'
})

Methods

microValidator.addRule(rule:string, validator:function)

Add a new validation rule

Developer can add new rules to microValidator by calling addRule method with rule (rule name) and validator function which will validate the given input.

The provided validator function will receive two arguments value and options passed when defining the validation schema

If validator function returns true it means value is valid and if it returns false it means value is invalid.

import microValidator from 'micro-validator' 

function checkBetween (value, options) {
    return value > options.from && options.to > value
}

microValidator.addRule('between', checkBetween)

let validationSchema = {
    count: {
        between: {
            from: 10,
            to: 20,
            errorMsg: 'Count should be between 10 to 20'
        }
    }
}

let errors = microValidator.validate(validationSchema, { count: 21 })
console.log(errors) // { count: [ 'Count should be between 10 to 20' ] }
microValidator.validate(validationSchema, { count: 11 })
console.log(errors) // {}

microValidator.validate(validationSchema:object, data:object)

validate data according to validation schema provided

validate method takes two arguments

  1. validationSchema: An object containing information about how to validate data
  2. data: data to validate

validationSchema needs to be structured something like this

{
    keyName1: {
        rule: { ...options },
        rule: { ...options }
    },
    keyName:2 {
        rule: { ...options }
    }
}

and data should be

{
    keyName1: 'Hello',
    keyName2: 'World'
}

Where keyName is the property of data we want to apply our validation rules to, rule is the name of the rule we want to apply and options are the options that we want to send to validator function.

If all of the data validate successfully validate method will return an empty object. If there are some errors an object will be returned which will contains all of the errors corresponding to their property.

{
    keyName1: [
        'first rule failed',
        'second rule faild'
    ],
    keyName2: [
        'first rule failed'
    ]
}

options

microValidor doesn't control what options you pass in validationSchema because developers can easily role their own custom rules. Only option errorMsg is valid. You can pass errorMsg to rules which will be returned if the validation fails for a specific rule.

Inbuilt rules

1. required

Checks if a value is empty or not

{
    // your field name for demo I am using *key*
    key: {
        required: {
            errorMsg: 'key is required'
        }
    }
}

2. length

Check if the length of a given string falls between min and max ( You can pass min and max in the options )

{
    // your field name for demo I am using *key*
    key: {
        length: {
            min: 8,
            max: 16,
            errorMsg: 'key length should be between 8 and 16'
        }
    }
}

3. email

Check if a given string is a valid email or not

{
    // your field name for demo I am using *key*
    key: {
        email: {
            errorMsg: 'Please enter a valid email'
        }
    }
}

4. validUrl

Check if a given string is a valid url or not

{
    // your field name for demo I am using *key*
    key: {
        validUrl: {
            errorMsg: 'key should be a valid URL'
        }
    }
}

5. equals

Check if a given value is equals to value provided

{
    // your field name for demo I am using *key*
    key: {
        equals: {
            to: 'password', // you can pass anything here for e.g. variables
            errorMsg: 'key and other value are not same'
        }
    }
}

6. regex

Check if a given value matches provided regex

{
    // your field name for demo I am using *key*
    key: {
        regex: {
            pattern: /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im,
            errorMsg: 'Please enter a valid Phone Number'
        }
    }
}

microValidator focus is to become an extensible validation library for Javascript that is why there is not many inbuilt rules. You can always create your own custom rules or contribute to this project.

About Me