Registry / testing / rttc
library1.0.6jsnpmunverified

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.

npm install rttc
INSTALL
IMPORT
SIG · RTTC
R
rttc
testingjavascriptv1.0.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rttc
const rttc = require('rttc');
import rttc from 'rttc'; // OR import { rttc } from 'rttc';
RTTC primarily uses CommonJS module exports. Direct ESM `import` statements may not work as expected without specific Node.js configuration (like `"type": "module"` in package.json or transpilation) or the use of an `esm` loader, as the package does not explicitly define `exports` for ESM.
validateStrict
const rttc = require('rttc'); rttc.validateStrict('number', 999);
import { validateStrict } from 'rttc';
Methods like `validateStrict` are properties of the main `rttc` module object, not named exports. Access them via the imported `rttc` object.
coerce
const rttc = require('rttc'); rttc.coerce('number', '999');
import { coerce } from 'rttc';
Similar to `validateStrict`, `coerce` is a method on the `rttc` object. The library exports a single module object containing all its utilities.

Demonstrates `rttc.coerce` applying a complex recursive schema to an array of objects, coercing values to their base types when possible and filling in missing required fields with base values, never throwing errors.

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: [] } ] */
Debug
Known issues
breakingIn 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.
fix
Review calls to `rttc.parseHuman()` with primitive type schemas. Implement `try...catch` blocks or use `rttc.coerce()` directly if you prefer non-throwing behavior that returns base values on failure.
affects: >=10.0.0
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.
fix
Carefully choose the appropriate method based on desired strictness. Use `validateStrict()` for exact type adherence, `validate()` for lenient validation with minor coercion, and `coerce()` when guaranteed non-throwing behavior and fallback to base values are preferred.
affects: >=0.10.0
gotchaOlder 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.
fix
Upgrade to `rttc@9.3.4` or newer to ensure correct behavior when working with object keys that include periods. If upgrading is not possible, avoid using dots in key names where RTTC functions are applied.
affects: <9.3.4
Errors
Common errors & fixes
Error: Invalid value provided: Expecting a 'number' but got a 'string' (e.g. "999").
`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").
fix
If 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 }).
`rttc.validate()` was used with a value that is fundamentally incompatible with the expected type (e.g., an object when a number is expected).
fix
Ensure 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')
`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`.
fix
After 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.
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).
fix
Handle 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.
Upgrade
Version history
1.0.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
rttc — npm install rttc · libregistry