CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-interface-checker

Runtime library to validate data against TypeScript interfaces

Pending
Overview
Eval results
Files

interface-method-validation.mddocs/

Interface and Method Validation

Specialized validation for interface properties and method calls, including argument and return value validation.

Capabilities

Property Validation

Validate individual properties of interface types.

/**
 * If this checker is for an interface, returns a Checker for the type required
 * for the given property of this interface
 * @param prop - Property name
 * @returns Checker for the property type
 * @throws Error if property doesn't exist
 */
getProp(prop: string): Checker;

Usage Examples:

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

const { User } = createCheckers(userTypes);

// Get checker for specific property
const nameChecker = User.getProp("name");
const ageChecker = User.getProp("age");

// Validate individual properties
nameChecker.check("Alice"); // OK
nameChecker.check(123); // Throws: "value is not a string"

ageChecker.check(30); // OK
ageChecker.check("30"); // Throws: "value is not a number"

// Use property checkers for partial validation
const userUpdate = { name: "Bob" };
nameChecker.check(userUpdate.name); // Validate just the name

Method Argument Validation

Validate method call arguments for interface methods.

/**
 * If this checker is for an interface, returns a Checker for the argument-list
 * required to call the given method of this interface
 * @param methodName - Name of the method
 * @returns Checker for the method's argument list
 * @throws Error if method doesn't exist or is not a function
 */
methodArgs(methodName: string): Checker;

Usage Examples:

// For interface with methods:
// interface Calculator {
//   add(a: number, b: number): number;
//   divide(dividend: number, divisor: number): number;
// }

const { Calculator } = createCheckers(calculatorTypes);

// Validate method arguments
const addArgsChecker = Calculator.methodArgs("add");
const divideArgsChecker = Calculator.methodArgs("divide");

// Check arguments as arrays
addArgsChecker.check([10, 20]); // OK
addArgsChecker.check([10]); // Throws: argument missing
addArgsChecker.check(["10", 20]); // Throws: first argument not a number

divideArgsChecker.check([100, 5]); // OK
divideArgsChecker.check([100, 0]); // OK (type check passes, divide by zero is runtime issue)
divideArgsChecker.check([100]); // Throws: divisor missing

// Validate actual function calls
function safeAdd(calc: any, a: number, b: number) {
  Calculator.methodArgs("add").check([a, b]);
  return calc.add(a, b);
}

Method Return Value Validation

Validate method return values for interface methods.

/**
 * If this checker is for an interface, returns a Checker for the return value
 * of the given method of this interface
 * @param methodName - Name of the method
 * @returns Checker for the method's return type
 * @throws Error if method doesn't exist or is not a function
 */
methodResult(methodName: string): Checker;

Usage Examples:

const { Calculator } = createCheckers(calculatorTypes);

// Validate method return values
const addResultChecker = Calculator.methodResult("add");
const divideResultChecker = Calculator.methodResult("divide");

// Check return values
addResultChecker.check(30); // OK
addResultChecker.check("30"); // Throws: result not a number

// Validate function implementations
function validateCalculatorImplementation(calc: any) {
  const result1 = calc.add(10, 20);
  addResultChecker.check(result1); // Ensure return type is correct
  
  const result2 = calc.divide(100, 5);
  divideResultChecker.check(result2); // Ensure return type is correct
}

Function Type Validation

For function types (not interface methods), validate arguments and return values.

/**
 * If this checker is for a function, returns a Checker for its argument-list
 * @returns Checker for the function's arguments
 * @throws Error if not applied to a function type
 */
getArgs(): Checker;

/**
 * If this checker is for a function, returns a Checker for its result
 * @returns Checker for the function's return type  
 * @throws Error if not applied to a function type
 */
getResult(): Checker;

Usage Examples:

// For function type:
// type MathFunction = (x: number, y: number) => number;

const { MathFunction } = createCheckers(mathTypes);

// Validate function arguments and results
const argsChecker = MathFunction.getArgs();
const resultChecker = MathFunction.getResult();

// Check function signature compliance
function validateMathFunction(fn: any) {
  // Test with sample arguments
  argsChecker.check([5, 10]); // Arguments are valid
  
  const result = fn(5, 10);
  resultChecker.check(result); // Result type is valid
}

// Usage with actual functions
const multiply = (x: number, y: number) => x * y;
const invalidFn = (x: string, y: string) => x + y;

validateMathFunction(multiply); // OK
// validateMathFunction(invalidFn); // Would fail arg validation

Method Validation Patterns

Common patterns for validating methods and functions in practice.

Usage Examples:

// Interface with optional parameters:
// interface DataProcessor {
//   process(data: string[], options?: ProcessOptions): ProcessResult;
// }

const { DataProcessor } = createCheckers(dataProcessorTypes);

// Validate method with optional parameters
const processArgsChecker = DataProcessor.methodArgs("process");

processArgsChecker.check([["data1", "data2"]]); // OK - options omitted
processArgsChecker.check([["data1", "data2"], { sort: true }]); // OK - options provided
processArgsChecker.check([]); // Throws: data parameter missing

// Validate complex return types
const processResultChecker = DataProcessor.methodResult("process");

const result = {
  processedData: ["data1", "data2"],
  metadata: { count: 2, timestamp: Date.now() }
};

processResultChecker.check(result); // Validates entire result structure

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