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

validator-utils-js

v1.9.1

Published

validator-utils-js is a flexible and lightweight JavaScript validation library for handling common data validation tasks such as validating strings, numbers, dates, and booleans. It provides a simple chainable methods to validate and customize various inp

Downloads

675

Readme

Validator Utils JS

validator-utils-js is a flexible and lightweight JavaScript validation library for handling common data validation tasks such as validating strings, numbers, dates, and booleans. It provides simple chainable methods to validate and customize various input types efficiently, now with localization support and custom error key identification.

Features

  • Localization Support: Translate validation error messages into multiple languages, with current support for en (English).
  • String Validation: Validate strings, emails, URLs, JSON, and more.
  • Number Validation: Check for positive, negative, even, odd numbers, multiples, etc.
  • Date Validation: Validate date formats, check if a date is in the past or future, within a range, etc.
  • Boolean Validation: Validate if a value is a boolean.
  • Custom Key Names: Use keyName as an optional parameter in the validate function to indicate the input field name in returned error messages.
  • Chainable validation methods for custom validation pipelines.

Installation

To install the package, run the following command:

npm install validator-utils-js

Localization

The package supports localization for error messages. You can specify a language when instantiating the validator, and all error messages will be returned in that language. Currently supported languages are:

  • en for English (default)
  • ar for Arabic (soon)

Example: Setting the Language

import { BaseValidation } from 'validator-utils-js';

// Set the language to Arabic for string validation
const stringValidator = BaseValidation.isString({ language: 'en' })
  .isEmail()
  .validate("[email protected]", "email");

console.log(stringValidator); 
// Output (if invalid): { valid: false, message: "The input email is not a valid email address" }

Usage

Below are examples of how to use the validation utilities provided by validator-utils-js. Each validate function now accepts an optional keyName parameter, which specifies the name of the input field to include in the returned error message.

Importing the Validators

import {
  DateValidation,
  NumberValidation,
  StringValidation,
  BoolValidation,
  BaseValidation
} from 'validator-utils-js';

String Validation

You can chain string validation methods to check for conditions like email format, minimum/maximum length, and more.

const stringValidator = BaseValidation.isString()
  .min(5)
  .isEmail()
  .validate("[email protected]", "email");

console.log(stringValidator); // { valid: true }

Number Validation

Number validation supports checks for positive, negative, even, odd numbers, multiples, and more.

const numberValidator = BaseValidation.isNumber()
  .isPositive()
  .isEven()
  .isMultipleOf(5)
  .validate(10, "quantity");

console.log(numberValidator); // { valid: true }

Date Validation

Date validation allows you to check if the date is valid, in a specific format, and compare dates (e.g., is in the past, is within a range).

const dateValidator = BaseValidation.isDate()
  .isValidFormat()  // Check if date format is YYYY-MM-DD
  .isInThePast()    // Check if date is in the past
  .validate("2020-05-15", "start_date");

console.log(dateValidator); // { valid: true }

Boolean Validation

Boolean validation ensures that the input is a boolean value.

const boolValidator = BaseValidation.isBoolean()
  .validate(true, "isActive");

console.log(boolValidator); // { valid: true }

Example: Validate a Date within a Range

const dateRangeValidator = BaseValidation.isDate()
  .isWithinRange("2020-01-01", "2020-12-31")
  .validate("2020-06-15", "appointment_date");

console.log(dateRangeValidator); // { valid: true }

API Documentation

String Validation

  • isEmail(): Validates if the string is a valid email format.
  • isPhoneNumber(): Validates if the string is a valid E.164 phone number format.
  • isUrl(): Validates if the string is a valid URL format.
  • min(length: number): Validates if the string has a minimum length.
  • max(length: number): Validates if the string has a maximum length.
  • isJson(): Validates if the string is a valid JSON format.
  • isUuid(): Validates if the string is a valid UUID.
  • isFilePath(): Validates if the string is a valid file path.
  • isProgrammingLanguage(): Validates if the string matches a known programming language.
  • isDay(): Validates if the string is a valid day of the week.
  • isMonth(): Validates if the string is a valid month.
  • isCreditCard(): Validates if the string is a valid credit card number using the Luhn algorithm.
  • isAlphanum(): Validates if the string contains only alphanumeric characters.

Number Validation

  • isPositive(): Validates if the number is positive.
  • isNegative(): Validates if the number is negative.
  • isZero(): Validates if the number is zero.
  • isOdd(): Validates if the number is odd.
  • isEven(): Validates if the number is even.
  • isLess(value: number): Validates if the number is less than the specified value.
  • isGreater(value: number): Validates if the number is greater than the specified value.
  • isEqual(value: number): Validates if the number is equal to the specified value.
  • isMultipleOf(divisor: number): Validates if the number is a multiple of the specified divisor.

Date Validation

  • isValidFormat(): Validates if the date string is in the format YYYY-MM-DD.
  • isValidDate(): Validates if the date is a valid calendar date.
  • isInThePast(): Validates if the date is in the past.
  • isInTheFuture(): Validates if the date is in the future.
  • isWithinRange(startDate: string, endDate: string): Validates if the date is within a given range.
  • isWeekend(): Validates if the date falls on a weekend.

Boolean Validation

  • BaseValidation.isBoolean(): Checks if the value is a boolean.

License

This project is licensed under the ISC License.