or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Aproba

Aproba is a ridiculously light-weight function argument validator designed for asserting function interfaces in library code. It provides a simple single-character type system that enables developers to validate function arguments with concise schema strings, offering comprehensive error handling and browser compatibility with zero dependencies.

Package Information

  • Package Name: aproba
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install aproba

Core Imports

const validate = require("aproba");

Basic Usage

const validate = require("aproba");

function myfunc(a, b, c) {
  // `a` must be a string, `b` a number, `c` a function
  validate('SNF', arguments); // [a,b,c] is also valid
}

myfunc('test', 23, function () {}); // ok
myfunc(123, 23, function () {}); // type error
myfunc('test', 23); // missing arg error
myfunc('test', 23, function () {}, true); // too many args error

Architecture

Aproba is built around a single validation function that uses:

  • Schema-based Validation: Single-character type codes define expected argument patterns
  • Multiple Signature Support: Pipe-separated schemas enable optional arguments
  • Error Type Special Handling: Error arguments make subsequent parameters optional
  • Comprehensive Error Reporting: Specific error codes for different validation failures

Capabilities

Core Validation

Main validation function that checks function arguments against schema patterns.

/**
 * Validates function arguments against a schema string
 * @param {string} rawSchemas - Schema string defining expected types (e.g., 'SNF')
 * @param {Array|Arguments} args - Arguments to validate (typically the arguments object)
 * @throws {TypeError} Validation error with specific code
 */
function validate(rawSchemas, args);

Usage Examples:

const validate = require("aproba");

// Basic type validation
function processUser(name, age, callback) {
  validate('SNF', arguments);
  // name must be string, age number, callback function
}

// Optional arguments using pipe syntax
function createFile(path, options) {
  validate('SO|S', arguments);
  // path string + options object, OR just path string
}

// Error handling with special E type behavior
function handleError(err, data, callback) {
  validate('ESF', arguments);
  // If err is not null, data and callback become optional
  // If err is null, all three arguments required
}

// Multiple signature combinations
function flexibleApi(path, options, callback) {
  validate('SSF|SF|SOF|SO|S', arguments);
  // Many combinations supported
}

Type System

Aproba uses single-character type codes for validation:

// Type codes and their meanings:
// '*' - matches any type
// 'A' - Array.isArray() OR arguments object  
// 'S' - typeof === 'string'
// 'N' - typeof === 'number'
// 'F' - typeof === 'function'
// 'O' - typeof === 'object' && not null && not array && not error
// 'B' - typeof === 'boolean'
// 'E' - instanceof Error OR null (special behavior)
// 'Z' - == null

Schema Features

Multiple Signatures: Use pipe | to separate alternative schemas for optional arguments:

validate('SO|S', arguments); // string + object OR just string

Error Type Special Behavior: The E type has special semantics - if an error argument is present and not null, remaining arguments become optional:

// Schema 'ESF' becomes equivalent to 'E|ESF|ZSF'
validate('ESF', [new Error()]); // valid - only error required
validate('ESF', [new Error(), 'data', callback]); // valid - all args
validate('ESF', [null, 'data', callback]); // valid - all args required
validate('ESF', [null, 'data']); // invalid - missing callback

Error Handling

All validation errors are TypeError instances with specific code properties:

  • 'EMISSINGARG' - Missing required argument
  • 'EINVALIDTYPE' - Argument type doesn't match expected type
  • 'EWRONGARGCOUNT' - Wrong number of arguments provided
  • 'EUNKNOWNTYPE' - Unknown type code used in schema
  • 'ETOOMANYERRORTYPES' - Multiple 'E' types in single schema (not allowed)

Error Handling Example:

const validate = require("aproba");

function safeFunction(name, age) {
  try {
    validate('SN', arguments);
    // Process arguments...
  } catch (err) {
    if (err.code === 'EINVALIDTYPE') {
      console.error('Invalid argument type:', err.message);
    } else if (err.code === 'EMISSINGARG') {
      console.error('Missing required argument:', err.message);
    } else if (err.code === 'EWRONGARGCOUNT') {
      console.error('Wrong number of arguments:', err.message);
    }
    throw err;
  }
}

Types

/**
 * Main validation function (default export)
 * @param {string} rawSchemas - Schema patterns separated by | for alternatives
 * @param {Array|Arguments} args - Arguments array or arguments object to validate
 * @throws {TypeError} With code property indicating specific validation failure
 */
function validate(rawSchemas, args);