validation-box
Version:
The only validation library - with flexible regex - you need.
100 lines (75 loc) β’ 6.57 kB
text/mdx
---
title: Configuration
description: 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.
```ts
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:
| Method | Description |
|---------------|-------------|
| `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
| **Validation** | **Properties** | **Type** | **Default** | **Description** |
|----------------------|-----------------------|---------------------|----------------------|---------------|
| `validateUsername` | `min` | `number` | `3` | Minimum username length. |
| | `max` | `number` | `20` | Maximum username length. |
| | `allowSpecialChars` | `string` | `"_"` | Allowed special characters. |
| | `bannedWords` | `string[]` | `[]` | List of forbidden words. |
| | `messages` | `object` | `{}` | Custom error messages. |
| `validateUser` | `min` | `number` | `3` | Minimum name length. |
| | `max` | `number` | `30` | Maximum name length. |
| | `allowSpecialChars` | `string` | `"'β\\s"` | Allowed special characters. |
| | `bannedWords` | `string[]` | `[]` | List of forbidden words. |
| | `messages` | `object` | `{}` | Custom error messages. |
| `validateEmail` | `allowedDomains` | `string[]` | `[]` | Allowed domains (e.g., `["gmail.com"]`). |
| | `messages` | `object` | `{}` | Custom error messages. |
| `validatePassword` | `min` | `number` | `8` | Minimum password length. |
| | `max` | `number` | `100` | Maximum password length. |
| | `allowSpecialChars` | `string` | `"!@#$%^&*()_+"` | Required special characters. |
| | `bannedWords` | `string[]` | `[]` | List of forbidden words. |
| | `messages` | `object` | `{}` | Custom error messages. |
| `validateBirthDate` | `-` | `-` | `-` | Must be a valid date in the past (`YYYY-MM-DD`). |
| `validateAge` | `min` | `number` | `18` | Minimum allowed age. |
| | `max` | `number` | `120` | Maximum allowed age. |
| | `messages` | `object` | `{}` | 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?](https://github.com/euotiniel/validation-box)
### Validation options table (Countries)
| Country | Validation Function | Accepted Formats | Example Inputs |
|----------|----------------------|-------------------------------------------|-----------------------------|
| π¦π΄ **Angola** | `validatePhoneAO` | `+244XXXXXXXXX` (international) | `+244923456789` |
| | | `+244 XXX XXX XXX` (code + spaced) | `+244 923 456 789` |
| | | `XXXXXXXXX` (without code) | `923456789` |
| | | `923 456 789` (spaced) | `923 456 789` |
| π§π· **Brazil** | `validatePhoneBR` | `+55XXXXXXXXXXX` (international) | `+559812345678` |
| | | `+55 XX XXXXX XXXX` (code + spaced) | `+55 11 98765 4321` |
| | | `XXXXXXXXXXX` (without code) | `11987654321` |
| | | `XX XXXXX XXXX` (spaced) | `11 98765 4321` |
| πΊπΈ **USA** | `validatePhoneUS` | `+1XXXXXXXXXX` (international) | `+11234567890` |
| | | `+1 XXX XXX XXXX` (code + spaced) | `+1 123 456 7890` |
| | | `XXXXXXXXXX` (without cod) | `1234567890` |
| | | `XXX XXX XXXX` (spaced) | `123 456 7890` |