validis
v4.0.0
Published
Validation made simple! 🚀 This package gives you all the tools you need to quickly and effortlessly validate emails, phone numbers, passwords, character limits, and more. Whether you’re checking for a positive number, enforcing a specific range, or custo
Downloads
47
Maintainers
Readme
Validis
A comprehensive validation library for JavaScript with a user-friendly, chainable API.
Installation
npm install validisUsage
Validis provides two APIs for validation:
- Schema-based API - chainable API for complex validations
- Legacy Function API - Simple function-based validations
- Validation Schema Classes - Class-based implementations of validation modules
Schema-based API
const { Schema } = require('validis');
// String validation
const stringSchema = Schema.string().min(3).max(10).email();
const result = stringSchema.parse('[email protected]');
// Number validation
const numberSchema = Schema.number().min(5).max(100).positive();
const numResult = numberSchema.parse(42);
// Boolean validation
const boolSchema = Schema.boolean();
const boolResult = boolSchema.parse(true);
// Object validation
const userSchema = Schema.object({
username: Schema.string().min(3).max(20),
email: Schema.string().email(),
age: Schema.number().min(18).optional()
});
const userResult = userSchema.parse({
username: 'johndoe',
email: '[email protected]',
age: 25
});
// Array validation
const arraySchema = Schema.array(Schema.string()).min(1).max(5);
const arrayResult = arraySchema.parse(['apple', 'banana', 'cherry']);Validation Schema Classes
Validis now provides class-based implementations of all validation modules with a chainable API. We've introduced shorter, more user-friendly class names while maintaining backward compatibility with the original names:
// Using the new shorter class names
const {
BasicSchema,
NumSchema,
PassSchema,
TextSchema,
SpaceSchema,
OtpSchema
} = require('validis');
// Basic validations
const basicValidator = new BasicSchema();
basicValidator.email().char(50);
const emailResult = basicValidator.validate('[email protected]');
// The original class names are still supported for backward compatibility
// const {
// BasicValidationsSchema, // Same as BasicSchema
// NumberValidationsSchema, // Same as NumSchema
// PasswordValidationsSchema, // Same as PassSchema
// TextCaseValidationsSchema, // Same as TextSchema
// WhiteSpaceValidationSchema, // Same as SpaceSchema
// OtpGeneratorSchema // Same as OtpSchema
// } = require('validis');
// Number validations
const numberValidator = new NumSchema();
numberValidator.num().range(10, 100);
const numberResult = numberValidator.validate(50);
// Password validations
const passwordValidator = new PassSchema();
passwordValidator.minLen(8).pass();
passwordValidator.setCompareValue('Password123!');
passwordValidator.match();
const passwordResult = passwordValidator.validate('Password123!');
// Text case validations
const textCaseValidator = new TextSchema();
textCaseValidator.firstUpper().isUpper();
const textResult = textCaseValidator.validate('HELLO');
// White space validations
const whiteSpaceValidator = new SpaceSchema();
whiteSpaceValidator.edgeSpace().blank();
const whiteSpaceResult = whiteSpaceValidator.validate('Hello World');
// OTP generator
const otpGenerator = new OtpSchema();
otpGenerator.setLength(8).setType('mixed');
const mixedOtp = otpGenerator.generate();
// Or use shorthand methods
const numOtp = otpGenerator.numOtp();
const alphaOtp = otpGenerator.alphaOtp();Legacy Function API
const validis = require('validis');
// Basic validations
validis.email('[email protected]'); // Returns { valid: true }
validis.phone('1234567890'); // Returns { valid: true }
validis.char('Hello', 10); // Returns { valid: true }
// Password validations
validis.pass('StrongP@ss123'); // Returns { valid: true }
validis.minLen('password', 6); // Returns { valid: true }
validis.match('password', 'password'); // Returns { valid: true }
// Number validations
validis.num(42); // Returns { valid: true }
validis.range(42, 1, 100); // Returns { valid: true }
// Text case validations
validis.firstUpper('Hello'); // Returns { valid: true }
validis.isLower('hello'); // Returns { valid: true }
validis.isUpper('HELLO'); // Returns { valid: true }
// White space validations
validis.edgeSpace('Hello'); // Returns { valid: true }
validis.noSpaces('Hello'); // Returns { valid: false, reason: 'String contains whitespace' }
validis.blank('Hello'); // Returns { valid: true }
// OTP generation
validis.mixOtp(6); // Returns a 6-digit alphanumeric OTP
validis.numOtp(4); // Returns a 4-digit numeric OTP
validis.alphaOtp(8); // Returns an 8-character alphabetic OTPReturn Values
Schema-based API
The parse() method returns an object with the following structure:
// Success case
{
success: true,
data: value // The validated value
}
// Error case
{
success: false,
errors: [
{
code: 'validation.code',
message: 'Error message'
},
// More errors if multiple validations failed
]
}Validation Schema Classes
The validate() method returns an object with the same structure as the Schema-based API:
// Success case
{
success: true,
data: value // The validated value
}
// Error case
{
success: false,
errors: [
{
code: 'validation.code',
message: 'Error message'
},
// More errors if multiple validations failed
]
}Legacy Function API
Each validation function returns an object with the following structure:
// Success case
{
valid: true
}
// Error case
{
valid: false,
reason: 'Error message'
}Available Validations
String Schema
min(length)- Minimum string lengthmax(length)- Maximum string lengthlength(length)- Exact string lengthemail()- Email format validationurl()- URL format validationpattern(regex)- Custom regex pattern validationnoWhitespace()- No whitespace validationnonEmpty()- Non-empty string validation
Number Schema
min(value)- Minimum valuemax(value)- Maximum valuepositive()- Positive number validationnegative()- Negative number validationinteger()- Integer validationrange(min, max)- Range validation
Boolean Schema
true()- Must be truefalse()- Must be false
Object Schema
required()- Required fields validationstrict()- No additional properties allowed
Array Schema
min(length)- Minimum array lengthmax(length)- Maximum array lengthlength(length)- Exact array lengthunique()- Unique items validation
Basic Validations Schema
email()- Email format validationphone()- Phone number format validationchar(limit)- Character limit validation
Number Validations Schema
num()- Positive number validationrange(min, max)- Range validation
Password Validations Schema
pass()- Password strength validationminLen(length)- Minimum length validationsetCompareValue(value)- Set comparison valuematch()- Password match validation
Text Case Validations Schema
firstUpper()- First letter uppercase validationisLower()- Lowercase validationisUpper()- Uppercase validation
Space Schema
edgeSpace()- No leading/trailing whitespace validationnoSpaces()- No whitespace validationblank()- Non-blank validation
OTP Schema
setLength(length)- Set OTP lengthsetType(type)- Set OTP type ('mixed', 'numeric', 'alphabetic')generate()- Generate OTPmixOtp()- Generate mixed alphanumeric OTPnumOtp()- Generate numeric OTPalphaOtp()- Generate alphabetic OTP
License
MIT
