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

@termehui/validators

v1.0.5

Published

Extra validators for Yup

Downloads

13

Readme

Validator

Extra validators for yup.

Installation

CDN

This package published as validators module in umd.

<script src="https://unpkg.com/@termehui/validators"></script>

NPM

npm i @termehui/validators
import {
  registerAll,
  registerCreditCardValidator,
  registerUsernameValidator,
} from "@termehui/validators";
// Register all validators
registerAll();
// Register custom validator only
registerCreditCardValidator();
registerUsernameValidator();

Utility Functions

Validate package contains following helper functions:

getErrorRules

Get error rules from keyed error.

// Signature:
export function getErrorRules(errors: any): ErrorMap;

// Example
// Error Response => {"username": {"max": "username max is 20 char", "unique": "username must unique"}, "password": {"min" :"password must have 10 char at least"}}
// result => {"username": ["max","unique"], "password": ["min"]}

getErrorMessages

Get error messages from keyed error.

// Signature:
export function getErrorMessages(errors: any): ErrorMap;

// Example
// Error Response => {"username": {"max": "username max is 20 char", "unique": "username must unique"}, "password": {"min" :"password must have 10 char at least"}}
// result => {"username": ["username max is 20 char","username must unique"], "password": ["password must have 10 char at least"]}

getFirstRule

Get first error rule from errors list.

// Signature:
export function getFirstRule(errors: any): ErrorField;

// Example
// Error Response => {"username": {"max": "username max is 20 char", "unique": "username must unique"}, "password": {"min" :"password must have 10 char at least"}}
// result => {"username": "max", "password": "min"}

getFirstMessage

Get first error message from errors list.

// Signature:
export function getFirstMessage(errors: any): ErrorField;

// Example
// Error Response => {"username": {"max": "username max is 20 char", "unique": "username must unique"}, "password": {"min" :"password must have 10 char at least"}}
// result => {"username": "username max is 20 char", "password": "password must have 10 char at least"}

makeYupKeyedErrors

This function override yup default translator and return error key as error message.

// Signature:
export function makeYupKeyedErrors();

// Example
// instead of 'this field is required' message return 'required'

Usage

Validator contains following validators:

Note by default all validators return validator key as error message (in lowercase). you can pass error message on create time or use time to override this behavior.

alnum

Validate string contains alpha-numeric string. This validator accept a string to contains in check/

yup.string().alnum("-"); // alnum and dash

alnumfa

Validate string contains alpha-numeric and persian characters string. This validator accept a string to contains in check/

yup.string().alnumfa("");

credit

Validate credit card number. this validator accept tow mode, long (20 digit) and short (16 digit) card number. Allowed formats:

// 20 digit
yup.string().credit(true);
// 16 digit
yup.string().credit(false, "invalid credit card number");

iban

Validate iran iban (sheba) number.

// 20 digit
yup.string().iban();

idNumber

Validate id number string (0 to 10 digit string).

yup.string().idNumber();

identifier

Validate number greater than 1.

yup.number().identifier();

ip

Validate IPv4 string.

yup.string().ip();

ipPort

Validate IPv4 string with port.

yup.string().ipPort();

jalali

Validate jalali date string by format.

yup.string().jalali("YYYY/MM/DD");

mobile

Validate persian mobile number. accept (09xx) xxx-xxx and 09xxxxxxxx formats.

yup.string().mobile();

nationalCode

Validate persian national code. accept xxx-xxxxxx-x and xxxxxxxxxx formats.

yup.string().nationalCode();

numeric

Validate numeric string.

yup.string().numeric();

postalCode

Validate persian postal code. accept xxxxx-xxxxx and xxxxxxxxxx formats.

yup.string().postalCode();

tel

Validate persian tel. accept (0xx) xxxx-xxxx and 0xxxxxxxxxx formats for prefixed mode and xxxx-xxxx and xxxxxxxx for non-prefixed mode.

// Prefixed mode (11 digit)
yup.string().tel(true);
// Non-prefixed mode (8 digit)
yup.string().tel(false);

unsigned

Validate unsigned number (0 or greater).

yup.number().unsigned();

username

Validate username. username can contains numbers, alpha, dash, dot and underscore characters.

yup.string().username();

uuid

Validate uuid string.

yup.string().uuid();