Giter VIP home page Giter VIP logo

validators's Introduction

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();

validators's People

Contributors

mekramy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.