A comprehensive validation library for JavaScript with a user-friendly, chainable API.
npm install validis
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
const { Schema } = require('validis');
// String validation
const stringSchema = Schema.string().min(3).max(10).email();
const result = stringSchema.parse('test@example.com');
// 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: 'john@example.com',
age: 25
});
// Array validation
const arraySchema = Schema.array(Schema.string()).min(1).max(5);
const arrayResult = arraySchema.parse(['apple', 'banana', 'cherry']);
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('example@example.com');
// 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();
const validis = require('validis');
// Basic validations
validis.email('test@example.com'); // 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 OTP
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
]
}
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
]
}
Each validation function returns an object with the following structure:
// Success case
{
valid: true
}
// Error case
{
valid: false,
reason: 'Error message'
}
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
min(value)
- Minimum valuemax(value)
- Maximum valuepositive()
- Positive number validationnegative()
- Negative number validationinteger()
- Integer validationrange(min, max)
- Range validation
true()
- Must be truefalse()
- Must be false
required()
- Required fields validationstrict()
- No additional properties allowed
min(length)
- Minimum array lengthmax(length)
- Maximum array lengthlength(length)
- Exact array lengthunique()
- Unique items validation
email()
- Email format validationphone()
- Phone number format validationchar(limit)
- Character limit validation
num()
- Positive number validationrange(min, max)
- Range validation
pass()
- Password strength validationminLen(length)
- Minimum length validationsetCompareValue(value)
- Set comparison valuematch()
- Password match validation
firstUpper()
- First letter uppercase validationisLower()
- Lowercase validationisUpper()
- Uppercase validation
edgeSpace()
- No leading/trailing whitespace validationnoSpaces()
- No whitespace validationblank()
- Non-blank validation
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
MIT