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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vmgware/js-validator

v1.8.0

Published

A versatile and easy-to-use JavaScript validation library for ensuring data integrity with extensive customization options.

Downloads

55

Readme

JS Validator: Comprehensive Guide

Quality Gate Status

JS Validator is a robust JavaScript library designed for validating data inputs across applications. It offers a versatile and effective approach to ensuring data integrity, making it suitable for various scenarios, from form validation to complex rule-based data checks.

Key Features

  • Support for Various Data Types: Validates strings, numbers, booleans, integers, floats, and dates, catering to a wide range of data validation needs.
  • Comprehensive Validation Rules: Equipped with a rich set of rules for checking minimum/maximum values or lengths, matching patterns through regex, and more, ensuring thorough data validation.
  • Asynchronous Validation Capabilities: Facilitates async validations, perfect for use cases requiring database queries or API calls for validation.
  • Customizable Error Messaging: Allows for the definition of custom error messages for different validation scenarios, enhancing clarity and user experience.
  • Strict Mode for Rigorous Validation: Offers an option to enable strict mode, ensuring that only fields defined in the validation rules are accepted, enhancing security and data integrity.
  • Tracking of Validation Outcomes: Provides methods to easily retrieve fields that either passed or failed the validation, aiding in response handling and user feedback.
  • Comprehensive Documentation: Includes detailed documentation for easy integration and use.
  • TypeScript Compatibility: Offers type definitions for seamless integration in TypeScript projects, enhancing code quality and maintainability.
  • Minimalistic Design: Lightweight with no dependencies, ensuring quick load times and efficient performance.
  • Open Source and Community-Driven: Licensed under MIT, encouraging community contributions and widespread use.

Installation

You can easily integrate JS Validator into your project by installing it through npm:

npm install @vmgware/js-validator

Usage Examples

Validating a User Registration Form in JavaScript

This example demonstrates how to validate a simple user registration form:

const Validator = require("@vmgware/js-validator");

// Define validation rules for user data
const rules = {
  username: { type: "string", required: true, min: 3 },
  email: { type: "string", required: true, validate: "email" },
  age: { type: "number", min: 18 },
};

// Define custom error messages for validation failures
const messages = {
  username: {
    required: "Username is required",
    min: "Username must be at least 3 characters long",
  },
  email: {
    required: "Email is required",
    validate: "Please enter a valid email address",
  },
  age: {
    min: "You must be at least 18 years old",
  },
};

// Initialize the validator with the defined rules and messages
const validator = new Validator(rules, messages);

// Sample user data to be validated
const userData = {
  username: "johndoe",
  email: "[email protected]",
  age: 25,
};

// Validate the user data
validator.validate(userData).then(isValid => {
  if (isValid) {
    console.log("User data is valid");
  } else {
    console.error("Validation errors:", validator.getErrors());
  }
});

TypeScript Example: Validating a User Profile

In this TypeScript example, we validate a user profile form, leveraging TypeScript's type safety:

import Validator from "@vmgware/js-validator";

interface UserProfile {
  username: string;
  email: string;
  birthdate: Date;
}

// Validation rules according to the UserProfile interface
const rules = {
  username: { type: "string", required: true, min: 3 },
  email: { type: "string", required: true, validate: "email" },
  birthdate: { type: "date", required: true },
};

// Custom error messages for validation failures
const messages = {
  username: {
    required: "Username is required",
    min: "Username must be at least 3 characters long",
  },
  email: {
    required: "Email is required",
    validate: "Please enter a valid email address",
  },
  birthdate: {
    required: "Birthdate is required",
  },
};

// Initialize the validator with rules and messages tailored for UserProfile
const validator = new Validator<UserProfile>(rules, messages);

// Sample user profile data to be validated
const userProfile: UserProfile = {
  username: "janedoe",
  email: "[email protected]",
  birthdate: new Date("1990-04-15"),
};

// Validate the user profile
validator.validate(userProfile).then(isValid => {
  if (isValid) {
    console.log("User profile is valid");
  } else {
    console.error("Validation errors:", validator.getErrors());
  }
});

Why Custom Error Messages?

Custom error messages provide clear, user-friendly feedback, helping users correct their inputs effectively.

Asynchronous Validation Example

Asynchronous validation is particularly useful for checks that require external data, such as verifying if a username is available:

// Asynchronous function to check if a username is available
async function isUsernameAvailable(username) {
  // Simulate an API call to check username availability
  // Returns true if available
}

// Add asynchronous custom validation to the username field
rules.username.custom = async username => await isUsernameAvailable(username);

// Define a custom error message for the username availability check
messages.username.custom = "This username is already taken";

// Validation process remains the same as shown in previous examples

Using Strict Mode for Enhanced Validation

Strict mode ensures that the input strictly adheres to the defined rules, rejecting any extraneous fields:

// Enable strict mode in the validator options
const options = { strictMode: true };

// Initialize the validator with rules, messages, and options
const validator = new Validator(rules, messages, options);

// With strict mode, any fields not defined in the rules will result in a validation error

Contributing

Contributions are welcome! Please follow the Conventional Commits standard for commit messages.

License

JS Validator is open-sourced under the MIT License. See the LICENSE file for more details.

Who Should Use JS Validator?

JS Validator is designed for developers seeking a straightforward yet versatile validation library for their JavaScript or TypeScript projects. Whether you're validating simple forms or complex data structures, JS Validator provides the tools you need to ensure data integrity with minimal effort.