πŸ“¦ Valdation Box

Configuration

Validation Box provides flexible validation methods with various configuration options.

Validation Schemas

Instead of validating fields one by one, you can define a schema to handle multiple validations at once.

Creating a Validation Schema

Now you can create structured validation using vboxSchema instead of calling each validator separately.

import { vboxSchema, validator } from "validation-box";
 
const userSchema = new vboxSchema({
  username: validator.username({ minLength: 5, bannedWords: ["admin", "root"] }),
  email: validator.email({ allowedDomains: ["gmail.com", "outlook.com"] }),
  password: validator.password({ minLength: 8, allowSpecialChars: "!@#$%^&*" }),
});
 
const result = userSchema.validate({
  username: "validUser",
  email: "user@gmail.com",
  password: "Secure@123",
});
 
console.log(result);

Schema Configuration Options

When defining a schema with vboxSchema, each field can be customized using various options. These options allow you to set constraints, such as minimum and maximum length, allowed special characters, and even restricted words.

The table below outlines the available configuration options for different validation fields:

MethodDescription
username()All available options for the validateUsername function.
user()All available options for the validateUser function.
email()All available options for the validateEmail function.
password()All available options for the validatePassword function.
age()All available options for the validateAge function.

Configuration Guide

Validation Box lets you fully customize your validation rules. Below are some key options you can configure.

Validation options table

ValidationPropertiesTypeDefaultDescription
validateUsernameminnumber3Minimum username length.
maxnumber20Maximum username length.
allowSpecialCharsstring"_"Allowed special characters.
bannedWordsstring[][]List of forbidden words.
messagesobject{}Custom error messages.
validateUserminnumber3Minimum name length.
maxnumber30Maximum name length.
allowSpecialCharsstring"'’\\s"Allowed special characters.
bannedWordsstring[][]List of forbidden words.
messagesobject{}Custom error messages.
validateEmailallowedDomainsstring[][]Allowed domains (e.g., ["gmail.com"]).
messagesobject{}Custom error messages.
validatePasswordminnumber8Minimum password length.
maxnumber100Maximum password length.
allowSpecialCharsstring"!@#$%^&*()_+"Required special characters.
bannedWordsstring[][]List of forbidden words.
messagesobject{}Custom error messages.
validateBirthDate---Must be a valid date in the past (YYYY-MM-DD).
validateAgeminnumber18Minimum allowed age.
maxnumber120Maximum allowed age.
messagesobject{}Custom error messages.

One of the trufuns of validation-box over other libraries is country-specific validation.

Currently the list is short, but functional. We are working to make it as global as possible. Want to help us?

Validation options table (Countries)

CountryValidation FunctionAccepted FormatsExample Inputs
πŸ‡¦πŸ‡΄ AngolavalidatePhoneAO+244XXXXXXXXX (international)+244923456789
+244 XXX XXX XXX (code + spaced)+244 923 456 789
XXXXXXXXX (without code)923456789
923 456 789 (spaced)923 456 789
πŸ‡§πŸ‡· BrazilvalidatePhoneBR+55XXXXXXXXXXX (international)+559812345678
+55 XX XXXXX XXXX (code + spaced)+55 11 98765 4321
XXXXXXXXXXX (without code)11987654321
XX XXXXX XXXX (spaced)11 98765 4321
πŸ‡ΊπŸ‡Έ USAvalidatePhoneUS+1XXXXXXXXXX (international)+11234567890
+1 XXX XXX XXXX (code + spaced)+1 123 456 7890
XXXXXXXXXX (without cod)1234567890
XXX XXX XXXX (spaced)123 456 7890

On this page