RTTC: Runtime Type-Checking for JavaScript
RTTC (Runtime Type-Checking) is a lightweight, recursive type system for JavaScript designed to provide flexible, performant type guarantees on an as-needed basis, particularly valuable in Node.js environments for preventing common async callback errors. Unlike build-time type checkers, RTTC operates at runtime, validating and coercing data without requiring changes to the development stack or build tools. It is currently at version 10.0.1 and is actively maintained, with major version updates occurring when breaking changes necessitate. Key differentiators include its focus on runtime validation and coercion, deep traversal of data structures, and its integral role in core Node-Machine project utilities, the Sails.js framework, Waterline drivers, and various machinepacks, making it a foundational component for many tools in that ecosystem.
Common errors
-
Error: Invalid value provided: Expecting a 'number' but got a 'string' (e.g. "999").
cause `rttc.validateStrict()` was used with a value that is semantically a number but not strictly of the 'number' JavaScript type (e.g., a string like "999").fixIf strict type matching is desired, ensure the input is exactly the expected type. If lenient coercion is acceptable, use `rttc.validate()` or `rttc.coerce()` instead, which can handle type conversion for such cases. -
Error: Invalid value provided: Expecting a 'number' but got an 'object' (e.g. { x: 32, y: 79 }).cause `rttc.validate()` was used with a value that is fundamentally incompatible with the expected type (e.g., an object when a number is expected).fixEnsure that the input data structure broadly matches the expected type schema. For cases where incompatible types might occur, consider preprocessing the data or using `rttc.coerce()` if a fallback to a base value is acceptable, as `coerce()` never throws. -
TypeError: Cannot read properties of undefined (reading 'someProperty')
cause `rttc.coerce()` returned a base value (e.g., `0`, `''`, `[]`, `{}`) for a complex type due to a major type mismatch, and subsequent code attempted to access properties on this base value, possibly an empty object or array, leading to unexpected `undefined`.fixAfter calling `rttc.coerce()`, always check the structure and content of the returned value if there's a possibility of major type mismatches in the input. Implement defensive coding (e.g., optional chaining `?.`, nullish coalescing `??`) or use `rttc.validate()`/`rttc.validateStrict()` if explicit errors are preferred over implicit base value fallbacks. -
Error: Failed to parse and coerce human value to primitive type: Invalid format.
cause In `rttc@10.0.0` or later, `rttc.parseHuman()` was called with a string that could not be coerced to the specified primitive type (e.g., 'not-a-number' for a 'number' schema).fixHandle the potential validation error from `rttc.parseHuman()` using a `try...catch` block. Validate the input string before passing it, or adjust the schema if more flexible parsing is required. Consider custom parsing logic if `rttc.parseHuman()`'s strictness is not suitable for your use case.
Warnings
- breaking In v10.0.0, `rttc.parseHuman()` now throws a validation error if coercion of a human-entered string to a primitive type schema fails. Previously, it would silently fall back to the base value for the type.
- gotcha `rttc.validateStrict()`, `rttc.validate()`, and `rttc.coerce()` have distinct behaviors regarding error handling and coercion. `validateStrict` throws on *any* type mismatch, `validate` throws on *major* mismatches but coerces minor ones, and `coerce` *never* throws, instead returning base values for major mismatches.
- gotcha Older versions of RTTC (prior to v9.3.4) could exhibit edge cases or false positives with `rttc.isEqual()` or other functions when object key names contained `.` (dots). This was addressed in v9.3.4.
Install
-
npm install rttc -
yarn add rttc -
pnpm add rttc
Imports
- rttc
import rttc from 'rttc'; // OR import { rttc } from 'rttc';const rttc = require('rttc'); - validateStrict
import { validateStrict } from 'rttc';const rttc = require('rttc'); rttc.validateStrict('number', 999); - coerce
import { coerce } from 'rttc';const rttc = require('rttc'); rttc.coerce('number', '999');
Quickstart
const rttc = require('rttc');
const schema = [
{ name: 'string', age: 'number', friends: ['string'] }
];
const data = [
{ name: 'Karl', age: 258 },
{ name: 'Samantha', age: '937' },
{ name: 'Lupé', age: 82, friends: ['Henry', 'Mario', undefined] },
{ name: 'Andres', age: '22' },
{ age: ['nonsense!'] }
];
const coercedData = rttc.coerce(schema, data);
console.log(coercedData);
/*
Output:
[
{ name: 'Karl', age: 258, friends: [] },
{ name: 'Samantha', age: 937, friends: [] },
{ name: 'Lupé', age: 82, friends: ['Henry', 'Mario'] },
{ name: 'Andres', age: 22, friends: [] },
{ name: '', age: 0, friends: [] }
]
*/