JSPM

  • Created
  • Published
  • Downloads 1393
  • Score
    100M100P100Q107200F
  • License MIT

Fast, zero-dependency TypeScript validators and utilities for runtime checks, parsing, and lightweight object schema validation.

Package Exports

  • jet-validators
  • jet-validators/utils

Readme

jet-validators ✈️

npm downloads types bundle size license

A comprehensive collection of TypeScript validator functions and utilities for common compile and runtime checks.

jet-validator's parseObject function is one of the fastest schema validation tools out there not requiring a compilation step. Check out these benchmarks here.

Table of Contents

Introduction

jet-validators is a large collection of small, composable validator functions commonly used when validating values in TypeScript.

  • Zero dependencies
  • Minimal boilerplate
  • Excellent type narrowing
  • Drop-in validators you can import and use immediately

Quick Glance

import { isOptionalString } from 'jet-validators';

if (isOptionalString(value)) {
  // value is string | undefined
}

Why jet-validators?

  • Covers the vast majority of real-world validation needs
  • No initialization or schema definitions required for basic checks
  • Includes helpers for transforming values before validation
  • Lightweight object schema validation utilities
  • Zero dependencies

Installation

npm install jet-validators

Basic Validators

Basic validators can be imported directly and used without configuration.

All validators follow consistent naming patterns:

  • isX
  • isOptionalX
  • isNullableX
  • isNullishX
  • isXArray (and variants)

Nullables

  • isUndef
  • isNull
  • isNullish (null | undefined)

isBoolean

  • isBoolean
  • isOptionalBoolean
  • isNullableBoolean
  • isNullishBoolean
  • isBooleanArray (+ variants)

isValidBoolean

Valid after running through parseBoolean

  • isValidBoolean
  • isOptionalValidBoolean
  • isNullableValidBoolean
  • isNullishValidBoolean
  • isValidBooleanArray (+ variants)

isNumber

  • isNumber (+ optional / nullable / array variants)

Sub-categories:

  • Positive
  • Negative
  • Unsigned

Each includes the full optional / nullable / array variants.


isInteger

Same structure as isNumber, including:

  • Positive
  • Negative
  • Unsigned

isBigInt

  • isBigInt
  • isOptionalBigInt
  • isNullableBigInt
  • isNullishBigInt
  • isBigIntArray (+ variants)

isValidNumber

Valid after numeric coercion.

  • isValidNumber
  • isOptionalValidNumber
  • isNullableValidNumber
  • isNullishValidNumber
  • isValidNumberArray (+ variants)

isString

  • isString (+ optional / nullable / array variants)

isNonEmptyString

Ensures .length > 0.

  • isNonEmptyString (+ variants)

isSymbol

  • isSymbol (+ variants)

isDate

Checks for a Date instance with a valid timestamp.

!isNaN(date.getTime())
  • isDate (+ variants)

isValidDate

Accepts Date, string, or number and validates via new Date(...).

  • isValidDate (+ variants)

isObject

Is non-nullable object

  • isObject (+ variants)

isPlainObject

Is it an object of type Record<string, unknown> and nothing else (i.e. Date, Array, etc).

  • isPlainObject (+ variants)

isFunction

  • isFunction (+ variants)

Complex Validators

These require an initialization step and return a validator function.


isInArray

const isAllowed = isInArray(['a', 'b', 'c']);
isAllowed('a'); // true

Supports optional / nullable variants.


isInRange

Checks whether a number (or numeric string) falls within a range.

Rules:

  • (min, max) → exclusive
  • [min], [max] → inclusive
  • [] → no bound
  • Reverse bounds → “outside range”
const between0and100 = isInRange([0], [100]);
between0and100(50);   // true
between0and100('100'); // true
const negative = isInRange([], 0);
negative(-1); // true
const outside = isInRange(100, 50);
outside(101); // true
outside(75);  // false

isKeyOf

Checks whether a value is a key of an object.

const obj = { foo: 'bar', baz: 'qux' } as const;
const isKey = isKeyOf(obj);

isKey('foo'); // true

Note: Does not support symbol keys.


isValueOf

Checks whether a value exists in an object.

const obj = { foo: 'bar', baz: 'qux' } as const;
const isValue = isValueOf(obj);

isValue('bar'); // true

Includes the ValueOf<T> utility type.


Utilities

Utilities are imported from:

import { parseObject } from 'jet-validators/utils';

nonNullable

Removes null and undefined from a validator.

const isStrictString = nonNullable(isNullishString);

makeOptional / makeNullable / makeNullish

Wrap custom validators to extend their accepted types.

const isEmail = (arg: unknown): arg is TEmail =>
  isString(arg) && EMAIL_REGEX.test(arg);

const isNullishEmail = makeNullish(isEmail);

transform

Transforms a value before validation.

const isParsedArray = transform(JSON.parse, isNumberArray);

Supports callbacks for accessing transformed values.


parseBoolean

Converts common boolean representations:

  • "true", "false" (case-insensitive)
  • "1", "0"
  • 1, 0
parseBoolean('TrUe'); // true
parseBoolean(0);      // false

parseJson

Safely wraps JSON.parse.

const nums = parseJson<number[]>('[1,2,3]');

Throws if input is not a string.


Object Schema Validation

Lightweight schema validation for objects using validator functions.

These utilities are intentionally simpler than libraries like Zod or AJV.


parseObject

  • Transforms values
  • Removes unknown keys (by default)
  • Returns parsed output or false
  • Optional error callback
const parseUser = parseObject<IUser>({
  id: transform(Number, isNumber),
  name: isString,
});

Supports:

  • optional / nullable
  • arrays
  • nested schemas
  • loose / strict modes

Error handling

You can pass a callback as the second argument to the parseObject function or the function returned from it which will provide an array of errors if there are any. Each error object has the format:

Field Type Description
info string General information about the validation failure.
functionName string Name of the validator function that failed.
value unknown The value that caused the validation failure (optional).
caught string Error message caught from an unsafe validator function, if any.
key string The key at which the failure occurred but only when it happened at the root level.
keyPath string[] Full path to the failing value for anything other than a key at the rror level. If the failure occurs while inside an array variant (e.g. parseObjectArray), the first element represents the array index of the failing item.

Example

const parseUsersArrray([{ name: 'sean'}, {name: 123 }]) = ()
{
  info: 'Validator function returned false.',
  functionName: "isString",
  value: 123,
  keyPath: ["1", "name"]
}

---

### `testObject`

Same behavior as `parseObject`, but returns a **type predicate**.

```ts
if (testUser(user)) {
  // user is IUser
}

Combining parse + test

Nested schemas may use testObject inside parseObject, with caveats around TypeScript inference. Supplying generics restores full type safety.


Custom Validators

Any function of the form:

(arg: unknown) => arg is T

can be used in schemas.


Wrapping parse/test

When wrapping these utilities, ensure your generics extend Schema<T> to preserve type safety.

import { type Schema, ParseError } from 'jet-validators/utils';

const myCustomParse = <T, S extends Schema<T>>(schema: S) => {
  return parseObject(schema, (errors: ParseError[]) => `...do stuff`);
}

Safety Modes

Control how extra object properties are handled:

  • loose – keep extra keys
  • default – remove extra keys (no error)
  • strict – remove extra keys and emit errors
const strictUser = strictParseObject({...});

Nested schemas inherit the parent mode unless overridden.


Summary

jet-validators is ideal when you want:

  • Fast runtime validation
  • Strong type narrowing
  • No heavy schema machinery
  • Simple, composable utilities

Perfect for APIs, config parsing, and runtime safety without dependency bloat.