Extra validators for yup.
This package published as validators
module in umd.
<script src="https://unpkg.com/@termehui/validators"></script>
npm i @termehui/validators
import {
registerAll,
registerCreditCardValidator,
registerUsernameValidator,
} from "@termehui/validators";
// Register all validators
registerAll();
// Register custom validator only
registerCreditCardValidator();
registerUsernameValidator();
Validate package contains following helper functions:
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"]}
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"]}
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"}
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"}
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'
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.
Validate string contains alpha-numeric string. This validator accept a string to contains in check/
yup.string().alnum("-"); // alnum and dash
Validate string contains alpha-numeric and persian characters string. This validator accept a string to contains in check/
yup.string().alnumfa("");
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");
Validate iran iban (sheba) number.
// 20 digit
yup.string().iban();
Validate id number string (0 to 10 digit string).
yup.string().idNumber();
Validate number greater than 1.
yup.number().identifier();
Validate IPv4 string.
yup.string().ip();
Validate IPv4 string with port.
yup.string().ipPort();
Validate jalali date string by format.
yup.string().jalali("YYYY/MM/DD");
Validate persian mobile number. accept (09xx) xxx-xxx and 09xxxxxxxx formats.
yup.string().mobile();
Validate persian national code. accept xxx-xxxxxx-x and xxxxxxxxxx formats.
yup.string().nationalCode();
Validate numeric string.
yup.string().numeric();
Validate persian postal code. accept xxxxx-xxxxx and xxxxxxxxxx formats.
yup.string().postalCode();
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);
Validate unsigned number (0 or greater).
yup.number().unsigned();
Validate username. username can contains numbers, alpha, dash, dot and underscore characters.
yup.string().username();
Validate uuid string.
yup.string().uuid();