CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-interface-checker

Runtime library to validate data against TypeScript interfaces

Pending
Overview
Eval results
Files

data-validation.mddocs/

Data Validation

Core validation functionality with multiple validation modes, detailed error reporting, and TypeScript type guard support.

Capabilities

Basic Validation Methods

Core validation methods for checking data against type definitions.

/**
 * Check that the given value satisfies this checker's type, or throw Error
 * @param value - Value to validate
 * @throws VError if validation fails
 */
check(value: any): void;

/**
 * A fast check for whether or not the given value satisfies this Checker's type.
 * Returns true or false, does not produce an error message, and is fast on both success and failure.
 * @param value - Value to test
 * @returns true if valid, false otherwise
 */
test(value: any): boolean;

/**
 * Returns a non-empty array of error objects describing errors if the given value
 * does not satisfy this Checker's type, or null if it does.
 * @param value - Value to validate
 * @returns Array of error details or null if valid
 */
validate(value: any): IErrorDetail[] | null;

Usage Examples:

import { createCheckers } from "ts-interface-checker";
import userTypes from "./user-ti";

const { User } = createCheckers(userTypes);

// Basic validation - throws on failure
try {
  User.check({ name: "Alice", age: 30 }); // OK
  User.check({ name: "Bob" }); // Throws: "value.age is missing"
} catch (error) {
  console.error(error.message);
}

// Fast boolean test
const data1 = { name: "Charlie", age: 25 };
const data2 = { name: "Dave" };

if (User.test(data1)) {
  console.log("data1 is valid");
}
if (!User.test(data2)) {
  console.log("data2 is invalid");
}

// Detailed error information
const errors = User.validate(data2);
if (errors) {
  errors.forEach(error => {
    console.log(`${error.path}: ${error.message}`);
  });
}

Strict Validation Methods

Strict validation ensures objects and tuples have no extra members, preventing backward compatibility issues.

/**
 * Check that the given value satisfies this checker's type strictly.
 * This checks that objects and tuples have no extra members.
 * @param value - Value to validate
 * @throws VError if validation fails
 */
strictCheck(value: any): void;

/**
 * A fast strict check for whether or not the given value satisfies this Checker's type.
 * @param value - Value to test  
 * @returns true if valid, false otherwise
 */
strictTest(value: any): boolean;

/**
 * Returns error details for strict validation failures
 * @param value - Value to validate
 * @returns Array of error details or null if valid
 */
strictValidate(value: any): IErrorDetail[] | null;

Usage Examples:

const userData = {
  name: "Alice",
  age: 30,
  extra: "not allowed" // Extra property
};

// Regular validation allows extra properties
User.check(userData); // OK

// Strict validation rejects extra properties
try {
  User.strictCheck(userData); // Throws: "value.extra is extraneous"
} catch (error) {
  console.error(error.message);
}

// Strict boolean test
if (!User.strictTest(userData)) {
  console.log("Data has extra properties");
}

Type Guard Support

TypeScript type guard functionality through CheckerT interface.

/**
 * Typed checker interface with type guard functionality
 */
interface CheckerT<T> extends Checker {
  test(value: any): value is T;
  strictTest(value: any): value is T;
}

Usage Examples:

import { createCheckers, CheckerT } from "ts-interface-checker";
import { User } from "./user"; // TypeScript interface
import userTypes from "./user-ti";

const checkers = createCheckers(userTypes) as {
  User: CheckerT<User>
};

const unknownData: unknown = { name: "Alice", age: 30 };

// Type guard narrows unknown to User type
if (checkers.User.test(unknownData)) {
  // TypeScript knows unknownData is User type
  console.log(unknownData.name); // No type error
  console.log(unknownData.age);  // No type error
}

// Strict type guard
if (checkers.User.strictTest(unknownData)) {
  // TypeScript knows unknownData is User type with no extra properties
  console.log(unknownData.name);
}

Path Reporting

Customize error path reporting for better debugging context.

/**
 * Set the path to report in errors, instead of the default "value"
 * @param path - Custom path name for error reporting
 */
setReportedPath(path: string): void;

Usage Examples:

const { User } = createCheckers(userTypes);

// Default error path
try {
  User.check({ name: "Alice" }); // Throws: "value.age is missing"
} catch (error) {
  console.error(error.message);
}

// Custom error path
User.setReportedPath("user");
try {
  User.check({ name: "Bob" }); // Throws: "user.age is missing"
} catch (error) {
  console.error(error.message);
}

Type Information Access

Access the underlying type definition from a Checker instance.

/**
 * Return the type for which this is a checker
 * @returns The underlying TType instance
 */
getType(): TType;

Usage Examples:

const { User } = createCheckers(userTypes);
const userType = User.getType();

// Can be used to introspect type structure
console.log(userType instanceof TIface); // true for interface types

Install with Tessl CLI

npx tessl i tessl/npm-ts-interface-checker

docs

checker-creation.md

data-validation.md

error-handling.md

index.md

interface-method-validation.md

type-definition-system.md

tile.json