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

@turtlemint/validators

v1.1.2

Published

set of validators

Downloads

23

Readme

Validators

Set of validators for React

yarn add @turtlemint/validators

Introduction

This is a validation library for React!

It provides you with flexible validations and a handler.

What is Validation handler?

The validationHandler is pure function which accepts a value and bunch of rules to validate against.

It will test the validation rules against a given value and return an object with validation status and an error message.

Example

Let's say you have to validate a string "lorem imspun asdff dl ppplkj d" against following rules:

  1. Should be present (not null)
  2. Not greater than 50 character
  3. Does not contain any special characters

Step 1: Specify your validators

Specify a group of validators as an array of string or objects.

const rules:IValidationArray = [
    { name:"required" },
    { name:"maxLength", maxLength:50 },
    { name:"alphaSpace" },
]

Step 2: Validate the value

Alright, so this is how it looks:

import {
    validationHandler,
    IValidationArray,
    IValidation
} from "@turtlemint/validators";

const rules:IValidationArray = [
    { name:"required" },
    { name:"maxLength", maxLength:50 },
    { name:"alphaSpace" },
]

const validationObj: IValidation = validationHandler(value,rules);

// Here
validationObj = { isValid:true, message:"" }

You can also customize error message corresponding to a validator

const rules:IValidationArray = [
    { name:"required", message:"Field is required" },
    { name:"maxLength", maxLength:50, message:"It has exceeded the max limit of 50 characters" },
    { name:"alphaSpace", message:"Please remove the special characters" },
]

How Validation handler works?

Validations are executed in the same order as passed. If any validation fails, execution stops and will return an error message.

There are certain validators which only check if value is present when used with handler.

eg: email validation only checks for valid email, if the value is not empty. If the value is blank, it will return true as by default it has optional check enabled

Valid default(optional) check:

const rules:IValidationArray = [
    { name:"email", message:"Invalid email" },
]

const validationObj = validationHandler("",rules);

The above will return isValid: true as the value is blank.

Invalid check:

const rules:IValidationArray = [
    { name:"email", message:"Invalid email" },
]

const validationObj = validationHandler("test@te",rules);

The above will return isValid: false as the value is invalid.

Valid check with mandatory value:

const rules:IValidationArray = [
    { name:"required", message:"Kindly enter a value" },
    { name:"email", message:"Invalid email" },
]

const validationObj = validationHandler("",rules);

The above will return isValid: false as the required validator fails the check.

Validators

List of validators:

  • Single param

    • integer
    • email
    • mobile
    • required
    • pan
    • aadhar
    • gstin
    • alphaNumeric
    • alphaNumericWithSpace
  • Multi param

    • customHandler
    • regex
    • maxValue
    • minValue
    • minLength
    • maxLength
    • multipleOf

Single param - Validators that only need a value to check and no additional config

They can be passed as string in rules array

 // In this case default error message will be picked up
    const rules = ["required","email"]

 // Customized error message
    const rules = [
        { name:"required", message:"Kindly enter a value" },
        { name:"email", message:"Invalid email" },
    ]

Multi param - Validators that require value along with some additional params to validate

 // In this case default error message will be picked up
    const rules = [
        {name:"maxLength", maxLength:50},
        {name:"regex", expression:"/[A-Z]{5}[0-9]{4}[A-Z]{1}/i"}
    ]

 // Customized error message
    const rules = [
        {name:"maxLength", maxLength:50, message:"Should be less than 50 characters"},
        {name:"regex", expression:"/[A-Z]{5}[0-9]{4}[A-Z]{1}/i", message:"Invalid string"}
    ]

Using validators as individual functions will only return a boolean. It do not support error messaging


import { email } from "@turtlemint/validators";

const isValid = email("[email protected]");

// isValid will be a boolean value