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 🙏

© 2025 – Pkg Stats / Ryan Hefner

lightjx

v1.1.4

Published

A javascript validation framework

Readme

LightJx - A Validation Framework

A javascript validation framework ported and adapted from LightVx

Documentation

📚 AI Developer Guide - Comprehensive guide with all validators, patterns, and examples

Usage

npm i lightjx --save
import { Validate } from 'lightjx';

let validator = Validate.field("MyField","Display Name").required().asPhoneNumber();
validator.validate("This is the user input");
if(!validator.isValid) {
    console.error(validator.errorMessage);
}

A note on security & validation

Remember that validation in the browser is purely asthetic for user convenience. It offers no security and is easily bypassed. All systems should appropriately validate in the backend as part of a trusted and secure process.

Examples

Storing all validation rules in an object using the name

{
    businessName: Validate.field("businessName", "Business Name").required().asAlphaNumericHyphenText(),
    email: Validate.field("email", "E-Mail").required().asEmail(),
    phone: Validate.field("phone", "Phone").asPhoneNumber(),
    fax: Validate.field("fax", "Fax").asPhoneNumber()
}

Using a validator directly

import { Validate } from 'lightjx';

let emailValidator = Validate.field("email", "E-Mail").required().asEmail().validate("yourEmail@address");
if(!emailValidator.isValid) {
    console.error(emailValidator.errorMessage);
}

Using a custom regular expression

This validator is not using the required validator, so if there is no input the isValid property will be true. Most validators will succeed if no user input unless .required() is used.

import { Validate } from 'lightjx';

let validator = Validate.field("MyField","Display Name").withExpression(/^[a-zA-Z0-9]{1,}$/);
validator.validate("This is the user input");
if(!validator.isValid) {
    console.error(validator.errorMessage);
}

Validating exact lengths

LightJx provides a hasLength() method for validating exact string or number lengths, useful for postal codes, IDs, phone numbers, etc.

import { Validate } from 'lightjx';

// Validate US zip code (exactly 5 digits)
let zipValidator = Validate.field("zipCode","Zip Code").required().hasLength(5);
zipValidator.validate("12345"); // Valid
zipValidator.validate("1234");  // Invalid - too short
zipValidator.validate("123456"); // Invalid - too long

// Validate product ID (exactly 8 characters)
let idValidator = Validate.field("productId","Product ID").required().hasLength(8).asAlphaNumericText();
idValidator.validate("ABC12345"); // Valid
idValidator.validate("ABC123");   // Invalid - too short

Using a function to defer validtaion

Sometimes, you might want to validate using information from another source, such as a different field in the form. Some validators allow a function to be used instead of the comparison value, these are:

  • MinValidator
  • MaxValidator
  • MinDateValidator
  • BetweenDateValidator
  • ContainsTextValidator
  • NotContainsTextValidator
  • LengthValidator (hasMinLength, hasMaxLength, hasLengthRange, hasLength) Passing in a function will allow more advanced validation that adjusts to changing form data.
import { Validate } from 'lightjx';

let validator = Validate.field("MyField","Display Name").min(() => yourFormState.values.minCapacity);
validator.validate(100);
if(!validator.isValid) {
    console.error(validator.errorMessage);
}

Validate.field and Validate.define

In general, if you want to create user friendly validation messages, use the Validate.field function as this allows you to pass in control and display name values. If you want to just create validation rules and ignore field names, you can use Validate.define

//Using field
let validator = Validate.field("username", "Your username").required().asAlphaText().hasMaxLength(5);
//Using Define
let validator = Validate.define().required().asAlphaText().hasMaxLength(5);

Custom Error Messages

All validator methods support custom error messages through an optional errorMessage parameter. When provided, your custom message replaces the default validator error message.

// Basic custom error message
let validator = Validate.field("email", "Email Address")
    .required("Please enter your email address")
    .asEmail("Enter a valid email address");

// Multiple validators with custom messages
let passwordValidator = Validate.field("password", "Password")
    .required("Password is required")
    .hasMinLength(8, "Password must be at least 8 characters")
    .containsText("A", false, "Password must contain an uppercase letter");

// Mix custom and default messages
let usernameValidator = Validate.field("username", "Username")
    .required("Username is required")
    .asAlphaNumericText() // uses default message
    .hasMaxLength(20, "Username cannot exceed 20 characters");

Available commands

All methods accept an optional errorMessage parameter for custom error messages:

  • with(validator:Validator)
  • withExpression(expression:string | RegExp)
  • required(errorMessage?: string)
  • asAlphaText(errorMessage?: string)
  • asAlphaNumericText(errorMessage?: string)
  • asAlphaNumericHyphenText(errorMessage?: string)
  • asName(errorMessage?: string)
  • asPhoneNumber(errorMessage?: string)
  • asEmail(errorMessage?: string)
  • asDate(errorMessage?: string)
  • isDateOnOrAfter(minDate:Date, errorMessage?: string)
  • isDateOnOrBefore(maxDate:Date, errorMessage?: string)
  • isDateBetween(minDate:Date, maxDate:Date, errorMessage?: string)
  • asBoolean(errorMessage?: string)
  • containsText(searchText:string, ignoreCase:boolean = false, errorMessage?: string)
  • doesNotContainText(searchText:string, ignoreCase:boolean = false, errorMessage?: string)
  • asInt(errorMessage?: string)
  • asFloat(errorMessage?: string)
  • asNumber(errorMessage?: string)
  • asGuid(errorMessage?: string)
  • asHexColor(errorMessage?: string)
  • asUrl(errorMessage?: string)
  • asSecureUrl(errorMessage?: string)
  • in(items:Array, errorMessage?: string)
  • notIn(items:Array, errorMessage?: string)
  • isNull(errorMessage?: string)
  • isEmptyString(errorMessage?: string)
  • is(value:any, errorMessage?: string)
  • isNot(value:any, errorMessage?: string)
  • hasLengthRange(min?:number, max?:number, errorMessage?: string)
  • hasMinLength(minLength:number | Function, errorMessage?: string)
  • hasMaxLength(maxLength:number | Function, errorMessage?: string)
  • hasLength(length:number | Function, errorMessage?: string)
  • hasNoBrackets()
  • min(min:number, errorMessage?: string)
  • max(max:number, errorMessage?: string)