UNPKG

node-easyvalid

Version:

A lightweight, powerful validation library for Node.js backend development. Simplify user input validation, error handling, and data integrity in Express APIs and JavaScript applications with ease.

190 lines (144 loc) 5.89 kB
# Node EasyValid A lightweight, powerful validation library for Node.js backend development. Simplify user input validation, error handling, and data integrity in Express APIs and JavaScript applications with ease. ## Features - Truthy/falsy value checks - Array validation for multiple inputs - Email format validation - String length constraints - Numeric value validation - Custom error messages and HTTP status codes - Seamless integration with Express.js and other Node.js frameworks ## Installation ```bash npm install node-easyvalid ``` ## Quick Start ### Basic usage example: ```javascript const { isTrue, isEmail, minLength } = require("node-easyvalid"); try { isTrue("hello", "Value is required"); // Passes isEmail("user@example.com", "Invalid email"); // Passes minLength("password123", 8, "Password too short"); // Passes isTrue("", "Cannot be empty"); // Throws Error } catch (error) { console.log(error.message); // Custom error message console.log(error.statusCode); // Default 400 or custom } ``` ## 1. isTrue(value, [message], [statusCode]) What it does: Checks if something exists (not empty, not zero, not nothing). Syntax: value: The thing you’re checking (like a name or number). message (optional): What to say if it fails (default: "Validation Failed"). statusCode (optional): A number for errors (default: 400, good for web apps). Returns: true if okay, or throws an error if not. Example ```javascript const { isTrue } = require("node-easyvalid"); try { let name = 'John'; let emptyValue = ""; isTrue(name, "Name is missing!"); // Returns true isTrue(emptyValue, "Name is missing!"); // Throws "Name is missing!" } catch (error) { console.log(error.message); // "Name is missing!" } ``` ## 2. isTrues(array, [message], [statusCode]) What it does: Makes sure every item in a list exists (not empty or nothing). Syntax: array: A list of things (like ["apple", "banana"]). message (optional): Error message if something’s wrong (default: "Validation Failed"). statusCode (optional): Error number (default: 400). Returns: true if all items are okay, or throws an error if not. Example: ```javascript const { isTrues } = require("node-easyvalid"); try { isTrues(["cat", "dog"], "List can’t have empty items!"); // Returns true isTrues(["cat", ""], "List can’t have empty items!"); // Throws error } catch (error) { console.log(error.message); // "List can’t have empty items! at index 1" } ``` ## 3. isEmail(email, [message], [statusCode]) What it does: Checks if something looks like an email (e.g., "name@site.com"). Syntax: email: The email you’re checking. message (optional): Error message if it’s wrong (default: "Invalid email format"). statusCode (optional): Error number (default: 400). Returns: true if it’s an email, or throws an error if not. Example ```javascript const { isEmail } = require("node-easyvalid"); try { isEmail("me@example.com", "That’s not an email!"); // Returns true isEmail("not-an-email", "That’s not an email!"); // Throws "That’s not an email!" } catch (error) { console.log(error.message); // "That’s not an email!" } ``` ## 4. minLength(value, min, [message], [statusCode]) What it does: Makes sure text is long enough (like a password). Syntax: value: The text to check. min: The smallest length allowed (a number). message (optional): Error message if too short (default: "Must be at least {min} characters"). statusCode (optional): Error number (default: 400). Returns: true if long enough, or throws an error if not. Example ```javascript const { minLength } = require("node-easyvalid"); try { minLength("password123", 8, "Password too short!"); // Returns true minLength("pass", 8, "Password too short!"); // Throws "Password too short!" } catch (error) { console.log(error.message); // "Password too short!" } ``` ## 5. isNumber(value, [message], [statusCode]) What it does: Checks if something is a number (like 42 or "42"). Syntax: value: The thing to check. message (optional): Error message if it’s not a number (default: "Must be a number"). statusCode (optional): Error number (default: 400). Returns: true if it’s a number, or throws an error if not. Example ```javascript const { isNumber } = require("node-easyvalid"); try { isNumber(25, "Age must be a number!"); // Returns true isNumber("25", "Age must be a number!"); // Returns true isNumber("twenty", "Age must be a number!"); // Throws "Age must be a number!" } catch (error) { console.log(error.message); // "Age must be a number!" } ``` # Real-World Use Cases ## 1. Express API - User Registration Validate user inputs in an Express API for registration: ```javascript const { isTrue, isEmail, minLength, isNumber } = require("node-easyvalid"); // Registration endpoint app.post("/api/register", (req, res, next) => { try { const { username, email, password, age } = req.body; // Validate inputs isTrue(username, "Username is required",400); minLength(username, 3, "Username must be at least 3 characters"); isEmail(email, "Please provide a valid email"); minLength(password, 8, "Password must be at least 8 characters"); isNumber(age, "Age must be a number"); // Simulate successful registration res.status(201).json({ message: "User registered successfully", user: { username, email, age }, }); } catch (error) { res.status(error.statusCode || 500).json({ message: error.message || "Internal Server Error" }); } }); ``` # Questions? ## If you’re stuck, feel free to ask! You can email ameenmohammedak@gmail.com