
Product
Introducing Rust Support in Socket
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
snap-validate
Advanced tools
Lightweight validation library for common patterns without heavy dependencies
A lightning-fast, lightweight validation library for common patterns without heavy dependencies. Perfect for client-side and server-side validation with zero external dependencies and built-in protection against ReDoS (Regular Expression Denial of Service) attacks.
when()
and optional()
npm install snap-validate
For TypeScript projects, types are included automatically:
npm install snap-validate
# Types are included - no need for @types/snap-validate
const { validators, validate } = require('snap-validate');
// Single field validation
const emailResult = validators.email('user@example.com').validate();
console.log(emailResult.isValid); // true
// Schema validation
const schema = {
email: validators.email,
phone: (value) => validators.phone(value, 'us'),
password: validators.password
};
const data = {
email: 'user@example.com',
phone: '(555) 123-4567',
password: 'SecurePass123'
};
const result = validate(schema, data);
console.log(result.isValid); // true
Snap Validate includes comprehensive TypeScript definitions for enhanced developer experience:
import { BaseValidator, validators, validate, ValidationResult } from 'snap-validate';
// Full type safety and auto-completion
const validator = new BaseValidator('test-value')
.required('This field is required')
.min(5, 'Must be at least 5 characters')
.pattern(/^[a-zA-Z]+$/, 'Only letters allowed');
// Type-safe result handling
const result: ValidationResult = validator.validate();
// Schema validation with types
interface UserData {
email: string;
phone: string;
password: string;
}
const userData: UserData = {
email: 'john@example.com',
phone: '1234567890',
password: 'StrongPass123'
};
const schema = {
email: validators.email,
phone: (value: string) => validators.phone(value, 'us'),
password: validators.password
};
const result = validate(schema, userData);
Features:
Snap Validate includes built-in protection against Regular Expression Denial of Service (ReDoS) attacks:
// Set custom timeout for regex operations
const validator = new BaseValidator(value)
.setRegexTimeout(2000) // 2 second timeout
.pattern(/your-pattern/, 'Error message');
// Use async pattern validation for complex patterns with timeout protection
const validator = new BaseValidator(value)
.patternAsync(/complex-pattern/, 'Error message');
const result = await validator.validateAsync();
validators.email('user@example.com').validate();
// US format (default)
validators.phone('(555) 123-4567').validate();
// International format
validators.phone('+1234567890', 'international').validate();
// Simple numeric
validators.phone('1234567890', 'simple').validate();
// Uses Luhn algorithm
validators.creditCard('4532015112830366').validate();
validators.url('https://example.com').validate();
// Default: min 8 chars, requires upper, lower, numbers
validators.password('SecurePass123').validate();
// Custom options
validators.password('MyPass123!', {
minLength: 10,
requireUppercase: true,
requireLowercase: true,
requireNumbers: true,
requireSpecialChars: true
}).validate();
validators.alphanumeric('ABC123').validate();
validators.numeric('12345').validate();
// US zip code
validators.zipCode('12345').validate();
validators.zipCode('12345-6789').validate();
// Canadian postal code
validators.zipCode('K1A 0A6', 'ca').validate();
// UK postal code
validators.zipCode('SW1A 1AA', 'uk').validate();
const { BaseValidator } = require('snap-validate');
// Validate only when condition is met
const validator = new BaseValidator(value)
.when(user.isAdmin, validators.required('Admin field required'))
.min(5, 'Must be at least 5 characters');
// Optional validation - skip if empty/null/undefined
const optionalValidator = new BaseValidator(value)
.optional()
.email('Must be a valid email if provided');
// Function-based conditions
const conditionalValidator = new BaseValidator(value)
.when(() => user.role === 'admin', validators.required())
.max(100);
const { BaseValidator } = require('snap-validate');
// Synchronous custom validation
const customValidator = new BaseValidator(value)
.custom((val) => val !== 'forbidden', 'Value cannot be forbidden')
.custom((val) => {
if (val.includes('admin') && !user.isAdmin) {
return 'Only admins can use this value';
}
return true;
});
// Asynchronous custom validation
const asyncValidator = new BaseValidator(email)
.email()
.customAsync(async (email) => {
const exists = await checkEmailExists(email);
return !exists || 'Email already exists';
}, 'Email validation failed');
// Use async validation
const result = await asyncValidator.validateAsync();
// Async validation for single field
const validator = new BaseValidator(username)
.required()
.min(3)
.customAsync(async (username) => {
const available = await checkUsernameAvailable(username);
return available || 'Username is already taken';
});
const result = await validator.validateAsync();
// Async schema validation
const asyncSchema = {
username: (value) => new BaseValidator(value)
.required()
.customAsync(async (val) => {
const available = await checkUsernameAvailable(val);
return available || 'Username taken';
}),
email: (value) => validators.email(value)
.customAsync(async (val) => {
const exists = await checkEmailExists(val);
return !exists || 'Email already registered';
})
};
const asyncResult = await validate.async(asyncSchema, userData);
const { BaseValidator } = require('snap-validate');
// Synchronous pattern validation with built-in safety checks
const validator = new BaseValidator(value)
.pattern(/^[a-zA-Z0-9]+$/, 'Only alphanumeric characters allowed');
// Asynchronous pattern validation with timeout protection
const asyncValidator = new BaseValidator(value)
.patternAsync(/^[a-zA-Z0-9]+$/, 'Only alphanumeric characters allowed')
.setRegexTimeout(5000); // 5 second timeout
const result = await asyncValidator.validateAsync();
const validator = new BaseValidator(value)
.setRegexTimeout(3000) // Set custom timeout (3 seconds)
.pattern(/your-pattern/, 'Error message');
// The library automatically:
// - Detects unsafe regex patterns
// - Limits input length to prevent ReDoS
// - Applies timeout protection for complex patterns
// - Provides clear error messages for security violations
const { BaseValidator } = require('snap-validate');
const customValidator = new BaseValidator('test-value')
.required('This field is required')
.min(5, 'Must be at least 5 characters')
.max(20, 'Must be no more than 20 characters')
.pattern(/^[a-zA-Z]+$/, 'Only letters allowed');
const result = customValidator.validate();
const schema = {
username: (value) => new BaseValidator(value)
.required()
.min(3)
.max(20)
.pattern(/^[a-zA-Z0-9_]+$/, 'Username can only contain letters, numbers, and underscores'),
email: validators.email,
age: (value) => new BaseValidator(value)
.required()
.pattern(/^\d+$/, 'Age must be a number')
.custom((val) => parseInt(val) >= 18, 'Must be 18 or older')
};
const userData = {
username: 'john_doe',
email: 'john@example.com',
age: '25'
};
const result = validate(schema, userData);
const result = validators.email('invalid-email').validate();
if (!result.isValid) {
console.log('Validation errors:', result.errors);
// Output: ['Invalid email format']
}
// For schema validation
const schemaResult = validate(schema, data);
if (!schemaResult.isValid) {
const errors = schemaResult.getErrors();
console.log('Field errors:', errors);
// Output: { email: ['Invalid email format'], password: ['Password too weak'] }
}
// Async error handling
try {
const asyncResult = await validator.validateAsync();
if (!asyncResult.isValid) {
console.log('Async validation errors:', asyncResult.errors);
}
} catch (error) {
console.log('Validation exception:', error.message);
}
// Security-related errors
const unsafeResult = validator.pattern(/potentially-dangerous-pattern/, 'Error').validate();
if (!unsafeResult.isValid) {
console.log('Security errors:', unsafeResult.errors);
// Output: ['Potentially unsafe regex pattern detected']
}
<script src="https://unpkg.com/snap-validate/src/index.js"></script>
<script>
const { validators } = SnapValidate;
const result = validators.email('user@example.com').validate();
console.log(result.isValid);
</script>
isValid: boolean
- Whether validation passederrors: string[]
- Array of error messagesrequired(message?)
- Field is requiredmin(length, message?)
- Minimum length validationmax(length, message?)
- Maximum length validationpattern(regex, message?)
- Pattern matching validation with safety checkspatternAsync(regex, message?)
- Async pattern validation with timeout protectionsetRegexTimeout(timeoutMs)
- Set custom timeout for regex operationswhen(condition, validator)
- Conditional validationoptional()
- Skip validation if empty/null/undefinedcustom(fn, message?)
- Custom synchronous validationcustomAsync(fn, message?)
- Custom asynchronous validationvalidate()
- Execute synchronous validationvalidateAsync()
- Execute asynchronous validationvalidators.email(value)
validators.phone(value, format?)
validators.creditCard(value)
validators.url(value)
validators.password(value, options?)
validators.alphanumeric(value)
validators.numeric(value)
validators.zipCode(value, country?)
ValidationResult
- Interface for validation resultsValidatorFunction
- Type for validator functions used in schemasValidationSchema
- Type for validation schema objectsPasswordOptions
- Interface for password validation configurationBaseValidator<T>
- Generic base validator classvalidate(schema, data)
- Synchronous schema validationvalidate.async(schema, data)
- Asynchronous schema validationisRegexSafe(regex)
- Check if a regex pattern is safe to usesafeRegexText(regex, str, timeoutMs)
- Execute regex with timeout protectionisRegexSafe()
to check custom regex patterns before deploymentWe welcome contributions! Please see our Contributing Guide for details.
git checkout -b feature/amazing-feature
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
# Lint code
npm run lint
# Format code
npm run format
# Security audit
npm audit
# Type checking (for TypeScript users)
npm run type-check
# Validate TypeScript definitions
npm run validate-types
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for a detailed history of changes.
Made with ⚡ by Ramachandra Anirudh Vemulapalli
FAQs
Lightweight validation library for common patterns without heavy dependencies
We found that snap-validate demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.