Form-ready validators for email, phone, credit card, postcode and password rules. Zero dependencies, under 5 KB, plugs into React Hook Form, TanStack Form and tRPC via Standard Schema.
Form-ready validators for email, phone, credit card, postcode and password rules. One function call each, readable error messages, zero dependencies, under 5 KB.
import { validators } from 'snap-validate';
validators.phone('(555) 123-4567').validate().isValid; // true
validators.creditCard('4111 1111 1111 1111').validate().isValid; // true (Luhn-checked)
validators.zipCode('SW1A 1AA', 'uk').validate().isValid; // true
validators.password('hunter2').validate().errors;
// ['Password must be at least 8 characters',
// 'Password must contain at least one uppercase letter']
snap-validate implements Standard Schema, so it plugs into React Hook Form, TanStack Form, tRPC, Hono and other compatible tools without an adapter:
import { useForm } from 'react-hook-form';
import { standardSchemaResolver } from '@hookform/resolvers/standard-schema';
import { validators, toStandardSchema } from 'snap-validate';
const signup = toStandardSchema({
email: validators.email,
phone: (value) => validators.phone(value),
password: validators.password
});
const form = useForm({ resolver: standardSchemaResolver(signup) });
// errors.email.message -> 'Invalid email format'
Step-by-step recipes, each tested against the framework's current release: React Hook Form, TanStack Form, tRPC, Hono and Express.
safeParse results with errors already grouped by field.When to pick something else: if you want TypeScript types inferred from your schemas, or deeply nested data schemas for an API, Zod or Valibot are a better fit. snap-validate is for validating user input with as little setup as possible.
npm install snap-validate
Runtime: Node.js >= 18 (the published library has zero dependencies and also runs in browsers via a bundler — see Browser Usage).
Both module systems are supported out of the box:
// CommonJS
const { validators, validate } = require('snap-validate');
// ESM
import { validators, validate } from '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
safeParse returns the result shape most JavaScript validation code expects, with errors already grouped by field:
import { validators, BaseValidator, safeParse, safeParseAsync } from 'snap-validate';
const signupSchema = {
email: validators.email,
age: (value) => new BaseValidator(value).between(18, 99, 'Must be between 18 and 99')
};
const ok = safeParse(signupSchema, { email: ' New@Example.com ', age: 30 });
// -> { success: true, data: { email: 'new@example.com', age: 30 } } (transforms applied)
const bad = safeParse(signupSchema, { email: 'nope', age: 12 });
if (!bad.success) {
bad.error.fieldErrors; // { email: ['Invalid email format'], age: ['Must be between 18 and 99'] }
bad.error.formErrors; // messages not tied to a field
bad.error.issues; // [{ message, path: ['email'] }, ...]
}
// Schemas with async rules (customAsync, patternAsync, ...) need safeParseAsync
const result = await safeParseAsync(signupSchema, input);
safeParse accepts a schema object, a single-value factory such as (value) => validators.email(value), or any Standard Schema, including schemas from other libraries. In TypeScript, data is inferred from typed Standard Schemas; for snap-validate schemas, pass the type: safeParse<User>(signupSchema, input).
snap-validate implements [Standard Schema v1](https://standardschema
Use one constant for the 10,000-character input limit
https://github.com/aniru-dh21/snap-validate/issues/51
Labels: good first issue, refactor
Raise test coverage of BaseValidator
https://github.com/aniru-dh21/snap-validate/issues/44
Labels: good first issue, tests
Add more postcode countries: India, Germany, Australia
https://github.com/aniru-dh21/snap-validate/issues/41
Labels: enhancement, good first issue
New recipe: Next.js server actions
https://github.com/aniru-dh21/snap-validate/issues/40
Labels: documentation, good first issue
Unknown country or format codes silently fall back to defaults
https://github.com/aniru-dh21/snap-validate/issues/37
Labels: enhancement, discussion
Cover the remaining untested branches
https://github.com/aniru-dh21/snap-validate/issues/35
Labels: good first issue, tests
We love your input! We want to make contributing to Snap Validate as easy and transparent as possible, whether it's:
We use GitHub to host code, to track issues and feature requests, as well as accept pull requests.
Pull requests are the best way to propose changes to the codebase. We actively welcome your pull requests:
main.# Clone the repository
git clone https://github.com/aniru-dh21/snap-validate.git
cd snap-validate
# Install dependencies
npm install
# Run tests to make sure everything works
npm test
Create a feature branch
git checkout -b feature/your-feature-name
Make your changes
Test your changes
npm test # Run all tests
npm run test:watch # Run tests in watch mode
npm run test:coverage # Run tests with coverage
npm run lint # Check for linting errors
npm run lint:fix # Fix linting errors automatically
Commit your changes
git add .
git commit -m "feat: add new validator for social security numbers"
Push and create PR
git push origin feature/your-feature-name
We use conventional commits for better changelog generation:
feat: - New featuresfix: - Bug fixesdocs: - Documentation changesstyle: - Code style changes (formatting, etc.)refactor: - Code refactoringtest: - Adding or updating testschore: - Maintenance tasksExamples:
feat: add credit card type detection
fix: resolve phone number validation for international formats
docs: update README with new validator examples
test: add edge cases for email validation
We use ESLint and Prettier to maintain consistent code style:
Run npm run format to auto-format your code.
describe blocksdescribe('Validator Name', () => {
describe('valid inputs', () => {
test('should validate correct input', () => {
// Test implementation
});
});
describe('invalid inputs', () => {
test('should reject invalid input', () => {
// Test implementation
});
});
});
When adding a new validator, please:
Add the validator function to src/index.js:
validators.newValidator = (value, options = {}) => {
// Implementation
return new BaseValidator(value)
.required('Field is required')
.pattern(/regex/, 'Invalid format');
};
Write comprehensive tests in tests/validators.test.js:
describe('New Validator', () => {
test('should validate correct input', () => {
const result = validators.newValidator('valid-input').validate();
expect(result.isValid).toBe(true);
});
});
Update documentation in README.md:
Add to TypeScript definitions (if applicable)
We use GitHub issues to track public bugs. Report a bug by opening a new issue.
Great Bug Reports tend to have:
We love feature requests! Please provide:
When contributing code:
Maintainers follow this process for releases:
package.jsonCHANGELOG.mdThis project follows the Contributor Covenant Code of Conduct. By participating, you agree to uphold it. To report a problem privately, see the Enforcement section.
By contributing, you agree that your contributions will be licensed under the MIT License.
Don't hesitate to ask qu