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

ts-form-validator

v1.0.3

Published

The TypeScript Form Validator is a utility that simplifies the validation of form data in TypeScript applications.

Downloads

10

Readme

Typescript Form Validator

The TypeScript Form Validator is a utility that simplifies the validation of form data in TypeScript applications.

Installation

Install the module via npm:

npm i ts-form-validator

How to use

Import from package

import {Validator, TypeInteger, TypeString, TypeEmail, TypeBoolean} from 'ts-form-validator';

Creating a Validator

const validator: Validator = new Validator({
    'name': new TypeString({'required': true, min: 1, max: 30}),
    'last_name': new TypeString({'required': false, min: 1, max: 50}),
    'email': new TypeEmail({'required': true, max: 40}),
    'is_investor': new TypeBoolean(),
});

Validator Methods:

validate

  • Description: Validates the form data based on the rules defined in the validator instance.
  • Parameters:
    • form: Form data represented as an object {key: value}.
  • Return: Void.

getError

  • Description: Retrieves the error associated with the specified rule.
  • Parameters:
    • key: key of the rule. Has to be a string
  • Return: The error message if it exists, otherwise null.

hasError

  • Description: Checks if there is an error associated with the specified rule.
  • Parameters:
    • key: Key of the rule (string).
  • Return: True if an error exists, otherwise false.

getErrors

  • Description: Retrieves all errors found.
  • Return: An object containing all errors.

hasErrors

  • Description: Checks if the validator has any errors.
  • Return: True if there are errors, otherwise false.

isValid

  • Description: Checks if the validator is valid.
  • Return: True if the validator is valid, otherwise false.

Validator types

Default attributes:

  • required: Boolean. The default value is false.
  • min: null|number|string.
  • max: null|number|string.

Default Responses:

  • ERROR_REQUIRED: The field is required.

TypeBoolean

  • Responses:
    • ERROR_BOOLEAN_FORMAT_INCORRECT: The boolean format is incorrect.

TypeDate

  • Responses:
    • ERROR_DATE_FORMAT_INCORRECT: The date format is incorrect.
    • ERROR_DATE_MIN: The date is too early.
    • ERROR_DATE_MAX: The date is too late.

TypeEmail

  • Responses:
    • ERROR_EMAIL_FORMAT_INCORRECT: The email format is incorrect.
    • ERROR_EMAIL_MIN: The email is too short.
    • ERROR_EMAIL_MAX: The email is to long.

TypeNumber

  • Attributes:
    • only_integer: boolean.
  • Responses:
    • ERROR_NUMBER_FORMAT_INCORRECT: The number format is incorrect.
    • ERROR_NUMBER_FORMAT_INTEGER_INCORRECT: The integer number format is incorrect.
    • ERROR_NUMBER_MIN: The number is too small.
    • ERROR_NUMBER_MAX: The number is too big.

TypePhone

  • Responses:
    • ERROR_PHONE_FORMAT_INCORRECT: The phone format is incorrect.
    • ERROR_PHONE_MIN: The phone is too short.
    • ERROR_PHONE_MAX: The phone is too long.

TypeString

  • Responses:
    • ERROR_STRING_FORMAT_INCORRECT: The string format is incorrect.
    • ERROR_STRING_MIN: The string is too short.
    • ERROR_STRING_MAX: The string is too long.

TypeUrl

  • Responses:
    • ERROR_URL_FORMAT_INCORRECT: The url format is incorrect.
    • ERROR_URL_MIN: The url is too short.
    • ERROR_URL_MAX: The url is too long.

TypeZip

  • Responses:
    • ERROR_DATE_FORMAT_INCORRECT: The ZIP code format is incorrect.

Custom Validator Types

If you need a different type, you can create your own validator type. Here's an example:

class TestType extends TypeBase implements Type{

    constructor(options = {}) {
        super(options);
    }

    validate(value:string):void{
        if(value !== 'test'){
            this.error = 'ERROR_TEST';
        }
    }
}

const validator:Validator = new Validator({
    'field_1': new TestType()
});