CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ow

Function argument validation for humans with expressive, chainable API

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

builtin-types.mddocs/

Built-in Type Validation

Validation for JavaScript built-in types including Date, Error, RegExp, Promise, Function, and Buffer with specialized validation methods.

Capabilities

Date Validation

Validation for Date objects with temporal comparison methods.

/** Date validation predicate */
interface DatePredicate extends BasePredicate<Date> {
  /** Date must be before the specified date */
  before(date: Date): DatePredicate;
  
  /** Date must be after the specified date */
  after(date: Date): DatePredicate;
}

Usage Examples:

import ow from 'ow';

const now = new Date();
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);
const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000);

// Basic date validation
ow(now, ow.date);

// Temporal comparison
ow(yesterday, ow.date.before(now));
ow(tomorrow, ow.date.after(now));

// Combined validation
ow(now, ow.date.after(yesterday).before(tomorrow));

Error Validation

Comprehensive validation for Error objects with property checking and built-in error type validation.

/** Error validation predicate */
interface ErrorPredicate extends BasePredicate<Error> {
  // Property validation
  name(expected: string): ErrorPredicate;
  message(expected: string): ErrorPredicate;
  messageIncludes(message: string): ErrorPredicate;
  hasKeys(...keys: string[]): ErrorPredicate;
  instanceOf(instance: Function): ErrorPredicate;
  
  // Built-in error types
  readonly typeError: ErrorPredicate;
  readonly evalError: ErrorPredicate;
  readonly rangeError: ErrorPredicate;
  readonly referenceError: ErrorPredicate;
  readonly syntaxError: ErrorPredicate;
  readonly uriError: ErrorPredicate;
}

Property Validation Examples:

import ow from 'ow';

const error = new Error('Something went wrong');
error.code = 'ERR_CUSTOM';
error.statusCode = 500;

// Property validation
ow(error, ow.error.name('Error'));
ow(error, ow.error.message('Something went wrong'));
ow(error, ow.error.messageIncludes('went wrong'));
ow(error, ow.error.hasKeys('code', 'statusCode'));

// Custom error classes
class CustomError extends Error {
  constructor(message: string, public code: string) {
    super(message);
    this.name = 'CustomError';
  }
}

const customError = new CustomError('Custom error', 'ERR_001');
ow(customError, ow.error.instanceOf(CustomError));
ow(customError, ow.error.name('CustomError'));

Built-in Error Type Examples:

import ow from 'ow';

// Built-in error types
ow(new TypeError('Type error'), ow.error.typeError);
ow(new EvalError('Eval error'), ow.error.evalError);
ow(new RangeError('Range error'), ow.error.rangeError);
ow(new ReferenceError('Reference error'), ow.error.referenceError);
ow(new SyntaxError('Syntax error'), ow.error.syntaxError);
ow(new URIError('URI error'), ow.error.uriError);

// Combined validation
ow(new TypeError('Invalid type'), ow.error.typeError.messageIncludes('type'));

RegExp Validation

Validation for regular expression objects.

/** RegExp validation predicate */
const regExp: Predicate<RegExp>;

Usage Examples:

import ow from 'ow';

// Basic RegExp validation
ow(/pattern/, ow.regExp);
ow(new RegExp('pattern', 'gi'), ow.regExp);

// With custom validation
ow(/^[a-z]+$/i, ow.regExp.is(regex => regex.test('hello')));

Promise Validation

Validation for Promise objects.

/** Promise validation predicate */
const promise: Predicate<Promise<unknown>>;

Usage Examples:

import ow from 'ow';

// Basic Promise validation
ow(Promise.resolve(42), ow.promise);
ow(fetch('/api/data'), ow.promise);
ow(new Promise(() => {}), ow.promise);

// Async function return values
async function fetchData() {
  return { data: 'result' };
}

ow(fetchData(), ow.promise);

Function Validation

Validation for function objects.

/** Function validation predicate */
const function: Predicate<Function>;

Usage Examples:

import ow from 'ow';

// Function validation
ow(() => {}, ow.function);
ow(function() {}, ow.function);
ow(async () => {}, ow.function);
ow(Math.max, ow.function);

// Method validation
const obj = {
  method() { return 'result'; }
};

ow(obj.method, ow.function);

// Constructor validation
ow(Array, ow.function);
ow(Date, ow.function);

Buffer Validation

Validation for Node.js Buffer objects.

/** Buffer validation predicate */
const buffer: Predicate<Buffer>;

Usage Examples:

import ow from 'ow';

// Buffer validation (Node.js only)
ow(Buffer.from('hello'), ow.buffer);
ow(Buffer.alloc(10), ow.buffer);
ow(Buffer.from([1, 2, 3, 4]), ow.buffer);

// With custom validation
ow(Buffer.from('hello'), ow.buffer.is(buf => buf.length === 5));

Advanced Usage Examples

Error Handling Patterns

import ow from 'ow';

// Validate error objects in catch blocks
function processData(data: unknown) {
  try {
    ow(data, ow.object.exactShape({
      id: ow.number.positive,
      name: ow.string.nonEmpty
    }));
    
    // Process valid data
    return { success: true, data };
  } catch (error) {
    // Validate error type and extract information
    if (ow.isValid(error, ow.error.name('ArgumentError'))) {
      return { success: false, error: error.message };
    }
    
    // Re-throw unknown errors
    throw error;
  }
}

Date Range Validation

import ow from 'ow';

// Validate date ranges
function validateDateRange(startDate: unknown, endDate: unknown) {
  ow(startDate, 'startDate', ow.date);
  ow(endDate, 'endDate', ow.date);
  
  // Ensure end date is after start date
  ow(endDate, 'endDate', ow.date.after(startDate as Date));
}

// Usage
const start = new Date('2023-01-01');
const end = new Date('2023-12-31');
validateDateRange(start, end);

Function Parameter Validation

import ow from 'ow';

// Validate function parameters
function createValidator(validatorFn: unknown, options: unknown) {
  ow(validatorFn, 'validatorFn', ow.function);
  ow(options, 'options', ow.object.partialShape({
    async: ow.optional.boolean,
    timeout: ow.optional.number.positive
  }));
  
  return {
    validate: validatorFn as Function,
    options: options as { async?: boolean; timeout?: number }
  };
}

Promise Chain Validation

import ow from 'ow';

// Validate promise chains
async function processAsyncData(fetchPromise: unknown) {
  ow(fetchPromise, 'fetchPromise', ow.promise);
  
  const result = await (fetchPromise as Promise<unknown>);
  
  ow(result, 'fetchResult', ow.object.hasKeys('data', 'status'));
  
  return result;
}

Install with Tessl CLI

npx tessl i tessl/npm-ow

docs

advanced-features.md

builtin-types.md

collection-types.md

index.md

object-array-validation.md

primitive-types.md

typed-arrays.md

tile.json