π What is sd-is
?
sd-is
is a zero-dependency, blazing-fast JavaScript utility library for:
- β Clean runtime type checks
- π§ Smart validation with auto-fix suggestions
- π Schema definition + enforcement
- π Emoji-enhanced developer-friendly errors
Itβs like typeof
, instanceof
, and Array.isArray
β but smarter and cleaner.
β¨ What's New in v2.0+
Feature | Description |
---|---|
β
assertType()
|
Enforce expected type at runtime |
π defineSchema()
|
Declaratively define reusable schemas |
π§ͺ validateAgainst()
|
Validate data against your schema (enums, custom, nested) |
π§ smartCheck
|
Check + explain + suggest fixes in human-readable format |
β Bonus Checks | New utils: isDate , isPromise , isSymbol , isRegExp , listFunctions
|
π¦ Installation
npm install sd-is
Supports CommonJS, ESM, and is fully tree-shakable.
β Type Checking Basics
import {
isString,
isNumber,
isBoolean,
isUndefined,
isNotUndefined,
isEmptyObject,
isEmptyArray,
isPlainObject
} from 'sd-is';
console.log(isString('hello')); // true
console.log(isNumber(123)); // true
console.log(isBoolean(false)); // true
console.log(isEmptyArray([])); // true
console.log(isPlainObject({ a: 1 })); // true
console.log(isUndefined(undefined)); // true
console.log(isNotUndefined(42)); // true
π§ smartCheck: Check + Explain + Fix
import { smartCheck } from 'sd-is';
const result = smartCheck.isEmptyObject({ x: 1 });
console.log(result.ok); // false
console.log(result.verdict); // β Not empty or not a valid object
console.log(result.reason); // Found 1 key(s): x
console.log(result.fix()); // {}
π assertType: Catch Mistakes Early
import assertType from 'sd-is/utils/assertType.js';
assertType('hello', 'string'); // β
assertType(42, 'string'); // β Throws: expected 'string', got 'number'
π defineSchema + validateAgainst
import defineSchema from 'sd-is/utils/defineSchema.js';
import validateAgainst from 'sd-is/utils/validateAgainst.js';
const productSchema = defineSchema({
id: { type: 'string' },
price: { type: 'number', default: 0 },
category: { type: ['string', 'undefined'], optional: true },
tags: { type: 'array', default: [] },
status: { enum: ['active', 'inactive'] },
createdAt: {
type: 'string',
custom: (val) =>
/\d{4}-\d{2}-\d{2}/.test(val) || 'Must be in YYYY-MM-DD format'
}
});
const product = {
id: 'p-001',
price: 89,
tags: ['electronics', 'sale'],
status: 'active',
createdAt: '2025-07-14'
};
const result = validateAgainst(productSchema, product);
console.log(result.ok); // true
console.log(result.errors); // []
π All Available Utilities
Function | Description |
---|---|
isBoolean |
Check for true/false |
isNumber |
Check if value is number |
isString |
Check if value is string |
isUndefined |
Check if value is undefined |
isNotUndefined |
Opposite of isUndefined |
isNull |
Check if value is null |
isFunction |
Check if value is function |
isPlainObject |
Check if object is a plain {} |
isEmptyObject |
Check if object has no keys |
isEmptyArray |
Check if array is empty |
isDate |
Check if value is a valid Date |
isSymbol |
Check if value is a Symbol |
isPromise |
Check if value is a Promise |
isRegExp |
Check if value is a RegExp |
listFunctions |
List all available util functions |
π€ Why Use sd-is?
- π No need to write typeof value === 'string' everywhere
- π§ Helpful validation, with built-in defaults and messages
- β‘ Extremely fast and tiny (~2KB gzipped)
- β Perfect for API layers, CLI tools, or raw object validation
- π¦ No dependencies, supports modern JS without bloat
π Links
π¦ NPM: sd-is
π» GitHub: github.com/sandeepdara-sd/sd-is
π Wrap Up
If you're tired of verbose type checks and want a tiny, reliable, and developer-friendly utility for validating JavaScript structures β sd-is is for you.
Let me know what you think or contribute on GitHub! β
Top comments (0)