RTTC: Runtime Type-Checking for JavaScript

10.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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: [] }
]
*/

view raw JSON →