Registry /
serialization / defensive-programming-framework
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
cannotBeNull
✓ import { cannotBeNull } from 'defensive-programming-framework';
✗ const cannotBeNull = require('defensive-programming-framework');
Primary utility for unconditional 'cannot be null' checks. Framework is ESM-first, use named imports.
mustBeGreaterThanOrEqualTo
✓ import { mustBeGreaterThanOrEqualTo } from 'defensive-programming-framework';
✗ import mustBeGreaterThanOrEqualTo from 'defensive-programming-framework';
Typical usage is via named imports for specific validation functions.
whenIsNull
✓ import { whenIsNull } from 'defensive-programming-framework';
Used for optional/corrective validation, where input can be modified instead of throwing an error.
ArgumentError
✓ import { ArgumentError } from 'defensive-programming-framework';
✗ new ArgumentError('message')
The custom error class thrown by unconditional validation functions. Must be imported to catch specifically.
Demonstrates both unconditional (error-throwing) and corrective (input-modifying) validation using core framework functions like `cannotBeNull`, `whenIsLessThan`, `whenIsNull`, and `ArgumentError`.
import {
cannotBeNull,
mustBeGreaterThanOrEqualTo,
whenIsNull,
whenIsLessThan,
ArgumentError
} from 'defensive-programming-framework';
/**
* Example function demonstrating defensive programming with the framework.
* Validates and potentially corrects input parameters.
*/
export function write(buffer: number[], startIndex: number, data: number[]): void {
// Unconditional validation - throws ArgumentError on failure
cannotBeNull(buffer); // Throws if buffer is null
cannotBeNull(data); // Throws if data is null
// Corrective validation - attempts to fix input, returns corrected value
// If startIndex is less than 0, it's corrected to 0.
startIndex = whenIsLessThan(startIndex, 0, 0);
// If data is null, it's corrected to an empty array.
data = whenIsNull(data, []);
// Mixed validation - ensuring corrected values meet further conditions
mustBeGreaterThanOrEqualTo(startIndex, 0); // Re-validate startIndex (now guaranteed >= 0 if corrected)
// Custom conditional check (can be wrapped in a framework function if frequently used)
if (data.length >= buffer.length - startIndex) {
throw new ArgumentError(`Length of data cannot be greater than remaining buffer space (${buffer.length - startIndex}).`);
}
console.log('Validation passed. Proceeding with actual write operation...');
// Actual execution code would go here
// For demonstration, let's just log the validated inputs
console.log('Buffer:', buffer);
console.log('StartIndex:', startIndex);
console.log('Data:', data);
}
// Example usage:
try {
console.log('\n--- Valid Case ---');
write([1, 2, 3, 4, 5], 1, [10, 20]); // Valid inputs
console.log('\n--- Corrective Case ---');
write([1, 2, 3, 4, 5], -5, null); // startIndex corrected to 0, data corrected to []
console.log('\n--- Error Case ---');
write([1, 2], 0, [10, 20, 30]); // Data length too large for buffer
} catch (e) {
if (e instanceof ArgumentError) {
console.error('Validation Error (caught):', e.message);
} else {
console.error('Unexpected Error (caught):', e);
}
}
Errors
Common errors & fixes
ReferenceError: ArgumentError is not defined
The `ArgumentError` class was used without being imported from the package.
fixAdd `import { ArgumentError } from 'defensive-programming-framework';` to your file. TypeError: cannotBeNull is not a function
A validation utility function (e.g., `cannotBeNull`, `mustBeEqualTo`) was called without being imported.
fixEnsure you have `import { cannotBeNull } from 'defensive-programming-framework';` (or the specific function) at the top of your file. UnhandledPromiseRejectionWarning: ArgumentError: Value cannot be null.
An unconditional validation function threw an `ArgumentError` which was not caught, typically in an asynchronous context or at the top level of execution.
fixWrap the code that calls the validation function in a `try...catch` block. For asynchronous operations, ensure the `catch` block is properly chained or awaited.
Audit
Dependencies
No dependency data recorded yet.