Validator-Air
Validator-Air is a lightweight and flexible validation library for JavaScript and TypeScript. It provides a simple way to validate data using built-in and custom validation rules.
Installation
Install Validator-Air via npm or yarn
npm install validator-air
or
yarn add validator-air
Usage
Validator-Air supports both CommonJS and ESModule import styles
CommonJS
const va = require("validator-air");
const { minLength, email } = require("validator-air");
ESModule
import va, { minLength, email } from "validator-air";
Basic Example
Using Validators Directly
import va, { minLength, email } from "validator-air";
const result = va("hello@example.com", minLength(5), email());
console.log(result); // true
Using Configuration Object
import va, { string, alpha, containsUppercase } from "validator-air";
const result = va("Apple", {
validators: [string(), alpha(), containsUppercase()],
});
console.log(result); // true
Custom Validators
Define a custom validator to validate specific rules
import va from "validator-air";
const lengthBetween = (min, max) => (value) =>
value.length >= min && value.length <= max;
const result = va("hello", lengthBetween(3, 6));
console.log(result); // true
Using Match Modes
Match "ALL" (Default)
import va, { email, url } from "validator-air";
const result = va("hello@example.com", {
validators: [email(), url()],
match: "ALL",
});
console.log(result); //false
Match "ANY"
import va, { email, url } from "validator-air";
const result = va("hello@example.com", {
validators: [email(), url()],
match: "ANY", // Passes if any of the validators return true
});
console.log(result); //true
API Reference
va(value, ...args)
Parameters
value
: The value to validate.args
: Pass validators directly or use a configuration object.
Configuration Object
The configuration object supports the following properties
validators
: An array of validator functions.match
: Determines the validation strategy. Can be:"ALL"
(default): All validators must returntrue
."ANY"
: At least one validator must returntrue
.
Returns
boolean
: The result of the validation.
Validator Function
A Validator
is a function with the following signature
type Validator = (value: any) => boolean;
Built-in Validators
Validator | Description | Parameters |
---|---|---|
required() |
Ensures the value is not null, undefined, or an empty string. | |
number() |
Validates if the value is a number. | |
bigint() |
Validates if the value is a bigint. | |
string() |
Validates if the value is a string. | |
boolean() |
Validates if the value is a boolean. | |
array() |
Validates if the value is a array. | |
object() |
Validates if the value is a object. | |
minLength(min) |
Ensures the value has a minimum length. | min (number): The minimum length. |
maxLength(max) |
Ensures the value has a maximum length. | max (number): The maximum length. |
exactLength(length) |
Ensures the value has an exact length. | length (number): The exact length. |
integer() |
Validates if the value is an integer. | |
decimal() |
Validates if the value is a decimal (non-integer) number. | |
positive() |
Ensures the value is a positive number (greater than 0). | |
negative() |
Ensures the value is a negative number (less than 0). | |
greaterThan(value) |
Ensures the value is greater than a specific number. | value (number): The number to compare. |
lessThan(value) |
Ensures the value is less than a specific number. | value (number): The number to compare. |
even() |
Validates if the value is an even number. | |
odd() |
Validates if the value is an odd number. | |
range(min, max) |
Ensures the value is within a specified numeric range. | min (number): Minimum value.max (number): Maximum value. |
decimalPlaces(count) |
Ensures the value has a specific number of decimal places. | count (number): Required decimal places. |
alpha() |
Ensures the value contains only alphabetic characters (A-Z, a-z). | |
alphaNumeric() |
Ensures the value contains only alphanumeric characters (A-Z, a-z, 0-9). | |
uppercase() |
Validates if the value contains only uppercase letters. | |
lowercase() |
Validates if the value contains only lowercase letters. | |
containsUppercase() |
Ensures the value contains at least one uppercase letter. | |
containsLowercase() |
Ensures the value contains at least one lowercase letter. | |
startsWith(prefix) |
Validates if the value starts with a specific prefix. | prefix (string): The prefix to check. |
endsWith(suffix) |
Validates if the value ends with a specific suffix. | suffix (string): The suffix to check. |
numericString() |
Validates if the value is a string consisting only of numeric characters (0-9). | |
email() |
Validates if the value is a valid email address. | |
url() |
Validates if the value is a valid URL. | |
pattern(regex) |
Validates if the value matches a specific regular expression. | regex (RegExp): The pattern to test. |
isDate() |
Validates if the value is a valid Date object. | |
pastDate(options?) |
Validates if the value is a past date. | options.inclusive (boolean): Include current time, defaults to false |
futureDate(options?) |
Validates if the value is a future date. | options.inclusive (boolean): Include current time, defaults to false |
dateRange(start, end, options?) |
Validates if the value is within a specified date range. | start (Date): Start dateend (Date): End dateoptions.inclusive (boolean): Include boundaries, defaults to true |
weekday() |
Validates if the value is a weekday (Monday to Friday). | |
weekend() |
Validates if the value is a weekend (Saturday or Sunday). | |
isoDate(options?) |
Validates if the value is a valid date string. | options.strict (boolean): Use strict format check, defaults to true |
minDate(min) |
Validates if the value is not earlier than a specified date. | min (Date): Minimum date |
maxDate(max) |
Validates if the value is not later than a specified date. | max (Date): Maximum date |
sameDay(date) |
Validates if the value is the same day as a specified date. | date (Date): Date to compare with |
age(min, max?) |
Validates if the value represents an age within specified range. | min (number): Minimum agemax (number): Maximum age (optional) |
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! If you'd like to improve Validator-Air, please fork the repository and submit a pull request.