npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

basic-input-validation

v1.0.0

Published

A simple library for basic input validation

Downloads

5

Readme

Basic Input Validation

This project provides a set of utility functions for validating and sanitizing various data types such as strings, emails, usernames, passwords, and more. The utilities include checks for type validation, length constraints, SQL injection prevention, JavaScript injection prevention, and other security measures.

Functions Overview

validateByStructure

Validates if the given data matches the defined structure.

Parameters:

  • structure: The structure to validate against. It can be a string, array, or object.
  • arg: The data to be validated.
  • path: The current path in the structure for error reporting (default: "").

Returns:

  • Throws an error if validation fails, otherwise returns void.

validateType

Validates the type of a value against a given type.

Parameters:

  • type: The type that the value should match.
  • value: The value whose type is being validated.

Returns:

  • Throws an error if the types do not match.

validateEmail

Validates an email address based on a specific format and length constraints.

Parameters:

  • email: The email address to validate.
  • possibleEmailMinLength: Minimum length of the email (default: 5).
  • possibleEmailLength: Maximum length of the email (default: 64).

Returns:

  • Throws an error if the email format or length is invalid.

validateUserName

Validates a username based on length constraints and allowed characters.

Parameters:

  • userName: The username to validate.
  • possibleUserNameMinLength: Minimum length of the username (default: 5).
  • possibleUserNameLength: Maximum length of the username (default: 32).

Returns:

  • Throws an error if the username format or length is invalid.

validatePassword

Validates a password to ensure it meets strength requirements (length, character types).

Parameters:

  • password: The password to validate.
  • possiblePasswordMinLength: Minimum length of the password (default: 8).
  • possiblePasswordLength: Maximum length of the password (default: 128).

Returns:

  • Throws an error if the password does not meet the required criteria.

isSqlInjection

Prevents SQL injections by checking and escaping dangerous SQL keywords and patterns.

Parameters:

  • input: The string to check for SQL injection.

Returns:

  • The sanitized string with SQL keywords escaped.

isJsScript

Prevents JavaScript injections by checking and sanitizing <script> tags in a string.

Parameters:

  • input: The string to check for JavaScript injections.

Returns:

  • The sanitized string with <script> tags replaced by escaped versions.

Example Usage

import { validateEmail, validateUserName, validatePassword, isSqlInjection, isJsScript } from 'basic-input-validation';

try {
  validateEmail('[email protected]');
  validateUserName('user123');
  validatePassword('StrongP@ssw0rd!');
} catch (error) {
  console.error('Validation error:', error.message);
}

const sanitizedInput = isSqlInjection("SELECT * FROM users WHERE name = 'admin';");
const safeInput = isJsScript("<script>alert('Hello!');</script>");

console.log(sanitizedInput);  // Sanitized SQL input
console.log(safeInput);       // Safe JavaScript input
Installation
You can install the package using npm:

bash
Копировать код
npm install basic-input-validation
License
This project is licensed under the MIT License - see the LICENSE file for details.