{"id":11967,"library":"rttc","title":"RTTC: Runtime Type-Checking for JavaScript","description":"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.","status":"active","version":"10.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/node-machine/rttc","tags":["javascript","type-checking","validation","static-analysis","typed-javascript"],"install":[{"cmd":"npm install rttc","lang":"bash","label":"npm"},{"cmd":"yarn add rttc","lang":"bash","label":"yarn"},{"cmd":"pnpm add rttc","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import rttc from 'rttc';\n// OR\nimport { rttc } from 'rttc';","symbol":"rttc","correct":"const rttc = require('rttc');"},{"note":"Methods like `validateStrict` are properties of the main `rttc` module object, not named exports. Access them via the imported `rttc` object.","wrong":"import { validateStrict } from 'rttc';","symbol":"validateStrict","correct":"const rttc = require('rttc');\nrttc.validateStrict('number', 999);"},{"note":"Similar to `validateStrict`, `coerce` is a method on the `rttc` object. The library exports a single module object containing all its utilities.","wrong":"import { coerce } from 'rttc';","symbol":"coerce","correct":"const rttc = require('rttc');\nrttc.coerce('number', '999');"}],"quickstart":{"code":"const rttc = require('rttc');\n\nconst schema = [\n  { name: 'string', age: 'number', friends: ['string'] }\n];\n\nconst data = [\n  { name: 'Karl', age: 258 },\n  { name: 'Samantha', age: '937' },\n  { name: 'Lupé', age: 82, friends: ['Henry', 'Mario', undefined] },\n  { name: 'Andres', age: '22' },\n  { age: ['nonsense!'] }\n];\n\nconst coercedData = rttc.coerce(schema, data);\n\nconsole.log(coercedData);\n/*\nOutput:\n[\n  { name: 'Karl', age: 258, friends: [] },\n  { name: 'Samantha', age: 937, friends: [] },\n  { name: 'Lupé', age: 82, friends: ['Henry', 'Mario'] },\n  { name: 'Andres', age: 22, friends: [] },\n  { name: '', age: 0, friends: [] }\n]\n*/","lang":"javascript","description":"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."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":">=10.0.0"},{"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.","message":"`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.","severity":"gotcha","affected_versions":">=0.10.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":"<9.3.4"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","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\").","error":"Error: Invalid value provided: Expecting a 'number' but got a 'string' (e.g. \"999\")."},{"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.","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).","error":"Error: Invalid value provided: Expecting a 'number' but got an 'object' (e.g. { x: 32, y: 79 })."},{"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.","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`.","error":"TypeError: Cannot read properties of undefined (reading 'someProperty')"},{"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.","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).","error":"Error: Failed to parse and coerce human value to primitive type: Invalid format."}],"ecosystem":"npm"}