NTFS Path Mockery

    @turtlemint/validators
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.2 • Public • Published

    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("test@test.com");
    
    // isValid will be a boolean value

    Install

    npm i @turtlemint/validators

    DownloadsWeekly Downloads

    0

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    56.4 kB

    Total Files

    21

    Last publish

    Collaborators

    • vipinyattm
    • rahularram001