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

typescript-form-validator

v1.0.5

Published

Typescript Form Validator

Downloads

41

Readme

Typescript Form Validator

A simple yet handy tool to validate your form fields.

you can use this validator in React, Vue and plain Typescript .

Install

npm i typescript-form-validator

Import

import validator from 'typescript-form-validator'

Simple usage

try{
    await validator([
        { "name": "john", rules:['required'] },
    ])

    console.log('validation passed')
}
catch(err){
    console.log('validation error:', err.message)
}

Rules

Note: rules will be checked only if the field has a NON empty value

try{
    await validator([
    // checks if the field has a value
    // NOTE: null, undefined, "", "  " are considered empty and "required" rule will throw an error 
    {"fieldname": "field value", rules: ['required']},

    // value is required if true, not required if false
    // you can call a function instead of manually passing true
    // e.g. `required_if:${myFunction()}`
    {"fieldname": "typescript", rules: ['required_if:true']},

    // value should be a valid integer number
    {"fieldname": 1, rules: ['integer']},

    // value should be a valid float number
    {"fieldname": 1.5, rules: ['float']},

    // value should be a valid string
    // number values will cause an exception
    {"fieldname": "some string", rules: ['string']},

    // value should be a valid number
    // string values will cause an exception
    {"fieldname": 1, rules: ['number']},

    // value should be a valid boolean
    // anything other than true or false will cause an exception
    {"fieldname": true, rules: ['boolean']},

    // value should be a valid array
    {"fieldname": [1, 2, 'a', 'b'], rules: ['array']},

    // value should be a valid file
    {"fieldname": file, rules: ['file']},

    // value should be a numeric >= 2
    {"fieldname": 3, rules: ['min:2']},

    // value should be a numeric <= 2
    {"fieldname": 1, rules: ['max:2']},

    // value should be an string with a length == 5 chars
    {"fieldname": "abcde", rules: ['length:5']},

    // value should be an string with a length >= 5 chars
    {"fieldname": "abcdefg", rules: ['min_length:5']},

    // value should be an string with a length <= 5 chars
    {"fieldname": "abc", rules: ['max_length:5']},

    // value should be equal to the specified value
    // if equal is used with one value e.g equal:abc
    // the validation error will be like this:
    //  fieldname should be equal to 'abc' .
    {"fieldname": "abc", rules: ['equal:abc']},
    // however, you can use equal as below too:
    {"fieldname1": "abc", rules: ['equal:fieldname2,abc']},
    // this way, abc and abc will be compared but the error will be:
    // fieldname1 should be equal to fieldname2
    // this is useful for comparing things like password and password confirmation

    // value should NOT be equal to the specified value
    // same usage as "equal" rule...
    {"fieldname": "123", rules: ['not_equal:abc']},

    // value should be one of the specified values
    {"fieldname": "2", rules: ['in:a,2,hi']},

    // value should NOT be one of the specified values
    {"fieldname": "00", rules: ['not_in:a,2,hi']},

    // value should follow the regex pattern
    {"fieldname": "a", rules: ['regex:^[a-zA-Z]*$']},

    // value should be an string ending with the specified value
    {"fieldname": "faked", rules: ['ends_with:ed']},

    // value should be an string starting with the specified value
    {"fieldname": "typescript", rules: ['starts_with:ty']},
   
])

    console.log('validation passed')
}
catch(err){
    console.log('validation error:', err.message)
}

Throwing exception

default behaviour of validator is to throw an Error Exception, But you can override this behaviour by passing false to the second argument of the "validator" function

const result = await validator([
    {"fieldname": "abc", rules: ['required']},
], false)

// result will be true if validation rules all pass
// and false if one of the rules fails
console.log(result)

"1" and 1

default behaviour of validator is to treat string values that are numeric as numbers. meaning that any string that is numeric will pass the validations meant for numbers.

e.g. "4" and 4 will pass "integer" rule but

you can override this behaviour by passing false to third argument of the validator function

const result = await validator([
    {"fieldname": "4", rules: ['integer']},
], false, false)    

// result will be false because "4" is a string
console.log(result)

language

currently validator only supports English and Persian, BUT more languages will be added in the future.

you can select the english or persian by passing the name to the fourth argument

const result = await validator([
    {"fieldname": "4", rules: ['integer']},
], false, false, "english")    

however, you can pass your own message for each field validation rule

await validator([
    {"name": undefined, rules: ['required'], messages: {'required': 'enter the name'} },
])