pavi
v2.1.1
Published
## Usage
Readme
Payload Validator
Usage
To start using the validator, you can use
import validator from 'pavi';Parsing and Response
There are two types of parsing - safe and unsafe.
const packageName = new validator.str().equals('pavi');
const unsafeParse = packageName.parse('ivap'); // will throw an error
const safeParse = packageName.safeParse('ivap'); // will return an objectWhen you use the safeParse method, an object will be returned
const failSafeParse = packageName.safeParse('ivap');
/*
{
error: ['EQUALS'],
success: false,
}
*/
const passSafeParse = packageName.safeParse('pavi');
/*
{
data: "pavi",
success: true,
}
*/Methods can also be chained
const password = new validator.str().min(3).max(256);You can also add custom error messages
const username = new validator.str().equals('jeepies', { error_message: 'Incorrect username!' });
const result = username.safeParse('admin');
/*
{
error: ['Incorrect username!'],
success: false,
}
*/Data Types
Integer
Equals
const clientBankPin = new validator.int().equals(1337);Greater than (GT)
const isAbove18 = new validator.int().gt(18);Less than (LT)
const isBelow100 = new validator.int().lt(100);Greater than or equals (GTE)
const is21OrAbove = new validator.int().gte(21);Less than or equals (LTE)
const is100OrBelow = new validator.int().lte(100);Negative
const isNegative = new validator.int().negative();Positive
const isPositive = new validator.int().positive();Finite
const _isFinite = new validator.int().finite();String
Equals
const username = new validator.str().equals('jeepies');Minimum Length
const password = new validator.str().min(3);Maximum Length
const password = new validator.str().max(256);Exact Length
const password = new validator.str().length(8);Includes
const password = new validator.str().includes("123");Starts With
const password = new validator.str().starts_with("P");Ends With
const password = new validator.str().ends_with("123");RegEx
const uuid = new validator.str().regex('/
[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}
/');const isEmail = new validator.str().email();IP Address
const isIP = new validator.str().ip();Roadmap (3.0.0)
- [ ] Objects
