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 rttcVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
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.
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.
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.
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.
No dependency data recorded yet.