sd-is
Version:
Tiny JavaScript type check utility functions with smart developer-friendly feedback.
129 lines (91 loc) β’ 4.58 kB
Markdown
[](https://www.npmjs.com/package/sd-is)
[](https://github.com/sandeepdara-sd/sd-is/blob/main/LICENSE)

# sd-is β‘
A blazing-fast, ultra-light JavaScript utility library to validate, verify, and ensure correctness of your data types and structures.
> "Is it... a number? empty? valid? fixable?" β `sd-is` has your back.
## π¦ Installation
```bash
npm install sd-is
```
## β¨ What's New in v1.0.6
* β
Added `assertType(value, expectedType)` β for strict, developer-friendly runtime type checks
* π§ Added `validateAgainst(schema, data)` β schema-based validation with support for enums, nested fields, and custom logic
* π Added `strictMode` option to reject extra fields in user input
* β Clear, emoji-enhanced error messages
* β More type checkers: `isDate`, `isSymbol`, `isPromise`, `isRegExp`
## π Quick Examples
### β
Type Assertion
```js
import assertType from 'sd-is/assertType.js';
assertType('hello', 'string'); // β
passes
assertType(123, 'string'); // β throws: expected 'string', got 'number'
```
### π Schema Validation
```js
import defineSchema from 'sd-is/defineSchema.js';
import validateAgainst from 'sd-is/validateAgainst.js';
const userSchema = defineSchema({
name: { type: 'string' },
age: { type: 'number', optional: true },
role: { enum: ['admin', 'user'] }
});
const result = validateAgainst(userSchema, { name: 'Tony', role: 'user' });
console.log(result.ok); // true
console.log(result.errors); // []
```
## π¨ API: `validateAgainst(schema, data, path?, options?)`
| Option | Default | Description |
| -------------------- | ------- | ------------------------------------------ |
| `path` | `''` | Internal (for nested use) |
| `options.strictMode` | `false` | Rejects extra fields not defined in schema |
### Schema Field Options
| Key | Description | |
| ---------- | --------------------------------------------------------- | ----------------- |
| `type` | `'string'`, `'number'`, `'array'`, etc. or array of types | |
| `enum` | Array of allowed values | |
| `optional` | Boolean β if field is optional | |
| `custom` | Function \`(value) => true | 'error message'\` |
## π§ Also Included: `smartCheck`
```js
const { smartCheck } = require('sd-is');
const result = smartCheck.isEmptyArray([1, 2]);
console.log(result.ok); // false
console.log(result.reason); // Array contains 2 item(s)
console.log(result.fix()); // []
```
## β
Utility Functions Available
| Function | Description |
| ---------------- | ------------------------------------- |
| `isBoolean` | Checks if value is `true` or `false` |
| `isNumber` | Checks if value is a number |
| `isString` | Checks if value is a string |
| `isUndefined` | Checks if value is undefined |
| `isNotUndefined` | Opposite of `isUndefined` |
| `isEmptyArray` | Checks if value is an empty array |
| `isEmptyObject` | Checks if object has no own keys |
| `isPlainObject` | Checks if it's a plain `{}` object |
| `isFunction` | Checks if value is a function |
| `isNull` | Checks if value is `null` |
| `isPromise` | Checks if value is a Promise |
| `isDate` | Checks if value is a Date object |
| `isSymbol` | Checks if value is a Symbol |
| `isRegExp` | Checks if value is a RegExp |
| `listFunctions` | Lists all available utility functions |
## π€ Why Use `sd-is`?
Because:
* β
Youβre tired of writing `typeof x === 'string'` 20x a day
* π« You want better error messages than "undefined is not a function"
* π§ You care about clean code, smart checks, and data integrity
* π¦ Itβs tiny, tree-shakable, and zero-dependency
## π§βπ» Author
Made with β€οΈ by [Sandeep Dara](https://github.com/sandeepdara-sd)
## π License
MIT