Skip to main content

Installation

Get ValidAuth up and running in your project in less than a minute.

Package Installation

1

Install via Package Manager

Choose your preferred package manager to install ValidAuth:
npm install validauth
ValidAuth has zero dependencies, so this is the only package you need to install.
2

Import in Your Code

ValidAuth supports both ES6 modules and CommonJS. Import the validators you need:
// Import specific validators
import { isEmail, isPassword, isUsername } from 'validauth';

// Or import everything
import * as validauth from 'validauth';
ValidAuth is tree-shakeable - when you import specific validators, bundlers like Webpack and Rollup will only include the code you use, keeping your bundle size minimal.
3

Verify Installation

Test that ValidAuth is working correctly:
import { isEmail } from 'validauth';

console.log(isEmail('[email protected]')); // true
console.log(isEmail('invalid-email'));     // false
If you see true and false logged to the console, you’re all set!

Available Exports

ValidAuth exports the following functions:

Validators

FunctionDescriptionSource
isEmail()Validate email addressesemail.js:12
isPassword()Validate password strengthpassword.js:16
getPasswordStrength()Calculate password strength scorepassword.js:98
isPasswordMatch()Compare two passwordspassword.js:227
isUsername()Validate usernamesusername.js:14
validateOTP()Validate OTP codesotp.js:12
isSessionTokenValid()Validate session tokenssessionToken.js
isXSSSafe()Check for XSS vulnerabilitiesxss.js

Generators

FunctionDescriptionSource
generatePassword()Generate secure passwordsgeneratePassword.js:13
generateOTP()Generate OTP codesgenerateOTP.js
generateSessionToken()Generate session tokensgenerateSessionToken.js

Bundle Size Optimization

ValidAuth is designed to be lightweight and efficient:

Full Library

~14KB minified
~5KB gzipped

Email Only

~4.4KB minified
~1.8KB gzipped

Password Only

~6KB minified
~2.3KB gzipped

Tree-Shaking Example

Modern bundlers automatically exclude unused code:
// Only imports the isEmail function and its dependencies
import { isEmail } from 'validauth';

// Final bundle only includes ~4.4KB instead of the full ~14KB
If you’re only using email validation, your users will only download ~1.8KB gzipped - that’s smaller than most images!

Environment Setup

Node.js Projects

For Node.js projects, ensure you’re using version 12 or higher:
package.json
{
  "type": "module",
  "dependencies": {
    "validauth": "^1.3.4"
  }
}
If you’re using CommonJS (no "type": "module" in package.json), use require() instead of import.

React Projects

// In any React component
import { isEmail, isPassword } from 'validauth';

function RegistrationForm() {
  const [email, setEmail] = useState('');
  const [errors, setErrors] = useState({});

  const validateEmail = (value) => {
    const result = isEmail(value, { details: true });
    if (!result.valid) {
      setErrors(prev => ({ ...prev, email: result.errors }));
    }
  };

  // ... rest of component
}

Vue Projects

<script setup>
import { ref } from 'vue';
import { isEmail, isPassword, isUsername } from 'validauth';

const email = ref('');
const password = ref('');
const errors = ref({});

const validateForm = () => {
  const emailResult = isEmail(email.value, { details: true });
  const passwordResult = isPassword(password.value, { details: true });
  
  if (!emailResult.valid) errors.value.email = emailResult.errors;
  if (!passwordResult.valid) errors.value.password = passwordResult.errors;
};
</script>

Express.js Middleware

import express from 'express';
import { isEmail, isPassword, isUsername } from 'validauth';

const app = express();

app.post('/register', (req, res) => {
  const { email, password, username } = req.body;
  
  const emailResult = isEmail(email, { details: true });
  const passwordResult = isPassword(password, { details: true });
  const usernameResult = isUsername(username, { details: true });
  
  if (!emailResult.valid || !passwordResult.valid || !usernameResult.valid) {
    return res.status(400).json({
      errors: {
        email: emailResult.errors,
        password: passwordResult.errors,
        username: usernameResult.errors
      }
    });
  }
  
  // Proceed with registration
  res.json({ success: true });
});

Browser Support

ValidAuth works in all modern browsers and environments:
  • ✅ Chrome 90+
  • ✅ Firefox 88+
  • ✅ Safari 14+
  • ✅ Edge 90+
  • ✅ Opera 76+
ValidAuth uses standard JavaScript features supported by all modern environments. No polyfills or transpilation required for modern browsers.

TypeScript Support

TypeScript definitions are coming in a future release. Currently, you’ll need to add type declarations manually or use // @ts-ignore if needed.
Temporary TypeScript usage:
// Temporary workaround until official types are released
declare module 'validauth' {
  export function isEmail(email: string, options?: any): boolean | any;
  export function isPassword(password: string, options?: any): boolean | any;
  export function isUsername(username: string, options?: any): boolean | any;
  // Add other functions as needed
}

import { isEmail, isPassword } from 'validauth';

Next Steps

Now that ValidAuth is installed, learn how to use it:

Quick Start

Get started with practical examples and common patterns

Email Validator

Learn about email validation options and features

Build docs developers (and LLMs) love