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

validata-jsts

v1.2.12

Published

Dynamic, rule-based validation for JS/TS objects with support for strings, numbers, dates, booleans, arrays, and custom conditions.

Downloads

107

Readme

Validata-JSTS

npm version
npm downloads
MIT License

Validata-JSTS is a lightweight rule-based validation library for JavaScript/TypeScript. You can define flexible and powerful validation rules using strings or schema objects, including support for custom rules, nested fields, ObjectIds, arrays, and more.


🚀 Features

  • ✅ Rule-based string validation
  • ✅ Schema-based validation using validateWithSchema
  • ✅ Auto-rule extraction from Mongoose schemas via extractRulesFromSchema
  • ✅ Support for: string, number, boolean, email, pwd, array, date, objectid
  • ✅ Custom error messages per rule
  • ✅ Optional fields
  • ✅ Nested field support (dot notation)
  • ✅ Extendable with custom rules via extend

📦 Installation

npm install validata-jsts

🧠 Quick Example

import { isInValiData } from "validata-jsts";

const rules = [
  "name-string-min3-max20",
  "email-email",
  "age-number-min18-max99",
  "password-pwd-min8",
  "bio-string-optional"
];

const data = {
  name: "Abraham",
  email: "[email protected]",
  age: 25,
  password: "StrongPass123!"
};

const result = isInValiData(rules, data);

if (result) {
  console.log("❌ Validation errors:", result);
} else {
  console.log("✅ Passed validation");
}

📌 Alternate Use Case Example: Signup Form

const rule = {
  name: "string-min3-max30",
  email: "email",
  age: "number-min18",
  password: "pwd-min8",
  bio: "string-optional"
};

const data = {
  name: "Abraham",
  email: "[email protected]",
  age: 25,
  password: "StrongPass123!"
};

const result = isInValiData(rules, data);

if (result) {
  console.log("❌ Validation errors:", result);
} else {
  console.log("✅ Passed validation");
}

🧾 Rule Format

fieldName-type-rule1-rule2-err:Custom error

Supported Types

| Type | Description | |-----------|----------------------------------| | string | Text fields | | number | Numeric fields | | boolean | True/false values | | email | Valid email address | | pwd | Strong password (custom rules) | | date | Date string in YYYY-MM-DD | | objectid | Valid MongoDB ObjectId | | array | Generic array support |


🔍 Optional Fields

Use optional to allow a field to be skipped:

"bio-string-optional"

✏️ Custom Error Messages

Use err: or valerr: to specify your own messages:

"email-email-err:Invalid email address"
"password-pwd-min8-valerr:Password too weak"

🧠 Schema-Based Validation

validateWithSchema(schemaObject, data)

import { validateWithSchema } from "validata-jsts";
import { Schema, validateWithSchema } from "validata-jsts";

const schema = new Schema({
  name: { type: "string", min: 3, max: 30 },
  email: { type: "email" },
  age: { type: "number", min: 18 },
  password: { type: "pwd", min: 8 },
  bio: { type: "string"}
});


const data = {
  name: "John",
  email: "[email protected]",
  age: 22,
  password: "StrongPass1!"
};

const result = validateWithSchema(schema, data);
console.log(result)

//Output
false

extractRulesFromSchema(mongooseSchema)

import mongoose from "mongoose";
import { extractRulesFromSchema } from "validata-jsts";

const schema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String },
  password: { type: String, required: true },
  profile: {
    age: { type: Number, min: 16, required: true },
    bio: { type: String, maxlength: 400 }
  },
  userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
});

const rules = extractRulesFromSchema(schema);

// Output:
[
  "name-string-req",
  "email-email",
  "password-pwd-req",
  "profile.age-number-req-min16",
  "profile.bio-string-max400",
  "userId-objectid-req"
]

🔧 Custom Rules with extend

import { extend, isInValiData } from "validata-jsts";

extend("startsWith", (value, expected) => {
  if (typeof value !== "string") return "Must be a string";
  if (!value.startsWith(expected)) return `Must start with ${expected}`;
  return null;
});

const rules = ["username-string-startsWith:dev"];
const data = { username: "devAbraham" };

const result = isInValiData(rules, data);

Real Example: Nigerian Phone Number

extend("naPhone", (value) => {
  if (!/^(\+234|0)[789][01]\d{8}$/.test(value)) {
    return "Invalid Nigerian phone number";
  }
  return null;
});

const rules = ["phone-naPhone-err:Provide a valid Nigerian number"];
// type can be imported  : ValidatorFn incase

import { extend, ValidatorFn } from "validata-jsts";

// default config from user
const minLengthCheck: ValidatorFn = (value, _, config = { min: 5 }) => {
  if (value.length < config.min) {
    return `{Value} is less than the minimum length of ${config.min}`;
  }
  return null;
};

extend("equal", (value, cond, config) => minLengthCheck(value, cond, { min: 10 }));

const rules = ["phone-equal"];
const value ={ idNumber :"abmercy"};
const result = isInValidata(rules, value)
console.log(result);
 // Output: "phone: value is less than the minimum length of 5


You can go as far as attaching your equating value to the name and then extract it... from the condition props

// if its a number and your fucntion name length equal is of length 4 your substring value will be 4
const condValue =  Number.parseInt(conditions[0].substring(4)) // if condition is one or you map it
// then you can validata your value against the condition,  you should have your extend function like this 
const minLengthCheck: ValidatorFn = (value, conditions, config = { min: 5 }) =>{
  // your validation here logic here
}

Then pass it into your rule and we will take care of that 🌝

const rule = ["name-equalAbraham"]
// we will take it from there.


🧪 Type Rules Overview

string

"name-string-min3-max50"
"key-string-16" //must be exactly 8 character long 

number

"age-number-min18-max60"
"packs-number-8" //must be exactly equal to 8 

boolean

"isAdmin-boolean"

email

"email-email"

pwd

"password-pwd-min8"

Password must include:

  • At least one uppercase letter
  • At least one lowercase letter
  • At least one digit
  • At least one special character

date

"birthdate-date-min1990_01_01-max2020_12_31"

objectid

"userId-objectid"

array

"tags-array"

⚠️ Known Limitations

  • ❌ No strict nested array typing like array<{ name: string }> - coming soon
  • ❌ Conditional rules (if:field=value) - coming soon
  • ❌ Rule negation using ! prefix (for rule strings) - coming soon
  • ❌ File/media validation - coming soon

📌 Use Case Example: Signup Form

const rule = {
  name: "string-min3-max30",
  email: "email",
  password: "pwd-min8",
  confirmPassword: "string-equal:Password123!",
  acceptTerms: "boolean"
};

const input = {
  name: "Jane Doe",
  email: "[email protected]",
  password: "Password123!",
  confirmPassword: "Password123!",
  acceptTerms: true
};

const result = isInValiData(rule, input);

📌 Alternate Use Case Example: Signup Form

const rule = [
	"name-string-min3-max30",
 "email-email",
 "password-pwd-max18",
	"confirmPassword-pwd-max18",
	"acceptTerms-boolean"
		];

const input = {
  name: "Jane Doe",
  email: "[email protected]",
  password: "Password123!",
  confirmPassword: "Password123!",
  acceptTerms: true
};

const result = isInValiData(rule, input);

🙌 Contributing

  1. Fork this repository
  2. Create a branch: git checkout -b feature-name
  3. Make your changes
  4. Push and create a Pull Request

📄 License

This project is licensed under the MIT License


✅ Stable, flexible, lightweight — ready for validation-heavy apps.

Coming soon: conditional rules, nested object arrays, and media validation support.


.