node-basic-validator
v1.0.0
Published
A flexible Node.js package for validating emails, passwords, and other input fields with custom options.
Maintainers
Readme
node-basic-validator
A flexible Node.js package for validating emails, passwords, numbers, and other input fields with chainable methods and schema-based validation.
Features
- Email and password validation
- Alpha and alphanumeric validation
- Numeric checks
- Custom validations
- Schema-based validation for objects
- Chainable API
- Lightweight and dependency-free (optional deps allowed)
Installation
npm install node-basic-validatorUsage
1. Chainable Field Validation
const { validate } = require('node-basic-validator');
const result = validate('MyName123', 'username')
.required()
.minLength(5)
.maxLength(15)
.isAlphanumeric();
if (result.hasErrors()) {
console.log(result.getErrors()); // array of error messages
}2. Email and Password Validation
validate('[email protected]', 'email')
.isEmail()
.required();
validate('SecureP@ssw0rd', 'password')
.isPassword({ minLength: 10, hasSpecialChar: true });3. Object Schema Validation
const { validateObject } = require('node-basic-validator');
const userData = {
email: '[email protected]',
password: 'Secure123!',
username: 'john_doe',
age: 20
};
const schema = {
email: { type: 'email', required: true },
password: { type: 'password', options: { minLength: 8 } },
username: { type: 'string', required: true, minLength: 3, maxLength: 20, isAlphanumeric: true },
age: { type: 'number', required: true, custom: val => val >= 18 }
};
const errors = validateObject(userData, schema);
if (Object.keys(errors).length > 0) {
console.log(errors); // { fieldName: [errors] }
}Supported Validations
| Method | Description |
|---------------------|----------------------------------------------|
| .required() | Field must be present and not empty |
| .minLength(n) | Minimum length for string |
| .maxLength(n) | Maximum length for string |
| .isNumeric() | Must be a valid number |
| .isAlpha() | Letters only (a-zA-Z) |
| .isAlphanumeric() | Letters and numbers only |
| .matches(regex) | Must match a regular expression |
| .custom(fn) | Provide a custom validator function |
| .isEmail() | Validates an email address |
| .isPassword() | Validates password with rules |
.isPassword() options
{
minLength: 8,
maxLength: 64,
hasUppercase: true,
hasLowercase: true,
hasNumber: true,
hasSpecialChar: true,
disallowedChars: [' ']
}License
MIT License © Abdullatif Mohammed [email protected]
