
Security News
Official Go SDK for MCP in Development, Stable Release Expected in August
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
The tcomb npm package is a library for Node.js and the browser which allows you to check the types of JavaScript values at runtime with a simple and concise syntax. It is useful for enforcing type safety, defining domain models, and creating composable and reusable type checkers.
Type Checking
This feature allows you to define a structure with specific types and create instances that adhere to those types.
const t = require('tcomb');
const Person = t.struct({ name: t.String, age: t.Number });
const person = Person({ name: 'Giulio', age: 43 });
Function Argument Validation
This feature enables you to validate function arguments to ensure they are of the expected type.
const t = require('tcomb');
function sum(a, b) {
t.Number(a);
t.Number(b);
return a + b;
}
sum(1, 2);
Subtyping
This feature allows you to create subtypes based on existing types with additional constraints.
const t = require('tcomb');
const Positive = t.subtype(t.Number, n => n >= 0);
const positiveNumber = Positive(10);
Refinement
This feature is similar to subtyping but is specifically for refining types to a more specific subset.
const t = require('tcomb');
const Integer = t.refinement(t.Number, n => n % 1 === 0);
const integerNumber = Integer(42);
Enums
This feature allows you to define a set of constants and ensure values match one of those constants.
const t = require('tcomb');
const Role = t.enums({ Admin: 'Admin', User: 'User' });
const userRole = Role('User');
Prop-types is a library for React that is used to document the intended types of properties passed to components. It is similar to tcomb in that it provides runtime type checking, but it is specifically tailored for React's component props.
io-ts is a TypeScript runtime type system for IO decoding/encoding. It is similar to tcomb in providing runtime type checking and validation, but it leverages TypeScript's type system for added compile-time safety.
Joi is an object schema description language and validator for JavaScript objects. It provides a powerful and expressive way to define and validate data structures. It is similar to tcomb in terms of validation capabilities but has a different API and is often used for validating API input.
Ajv is a JSON schema validator. It validates data against JSON Schema standards and is similar to tcomb in terms of validation. However, it uses a JSON-based schema definition rather than a programmatic API.
% tcomb
tcomb is a library for Node.js and the browser which allows you to check the types of JavaScript values at runtime with a simple syntax. It's great for Domain Driven Design, for testing and for adding safety to your internal code.
The library provides a built-in assert
function, if an assert fails the debugger kicks in
so you can inspect the stack and quickly find out what's wrong.
You can handle:
JavaScript native types
null
and undefined
type combinators (build new types from those already defined)
Let's build a product model
var Product = struct({
name: Str, // required string
desc: maybe(Str), // optional string, can be null
home: Url, // a subtype of a string
shippings: list(Str), // a list of shipping methods
category: Category, // enum, one of [audio, video]
price: union([Num, Price]), // a price (dollars) OR in another currency
size: tuple([Num, Num]), // width x height
warranty: dict(Num) // a dictionary country -> covered years
});
var Url = subtype(Str, function (s) {
return s.indexOf('http://') === 0;
});
var Category = enums.of('audio video');
var Price = struct({ currency: Str, amount: Num });
// JSON of a product
var json = {
name: 'iPod',
desc: 'Engineered for maximum funness.',
home: 'http://www.apple.com/ipod/',
shippings: ['Same Day', 'Next Businness Day'],
category: 'audio',
price: {currency: 'EUR', amount: 100},
size: [2.4, 4.1],
warranty: {
US: 2,
IT: 1
}
};
// get an immutable instance, `new` is optional
var ipod = Product(json);
You have existing code and you want to add safety
// your code: plain old JavaScript class
function Point (x, y) {
this.x = x;
this.y = y;
}
var p = new Point(1, 'a'); // silent error
in order to "tcombify" your code you can simply add some asserts
function Point (x, y) {
assert(Num.is(x));
assert(Num.is(y));
this.x = x;
this.y = y;
}
var p = new Point(1, 'a'); // => fail! debugger kicks in
Node
npm install tcomb
Browser
bower install tcomb
or download the tcomb.min.js
file.
This library uses a few ES5 methods
Array.forEach()
Array.map()
Array.some()
Array.every()
Object.keys()
Object.freeze()
JSON.stringify()
you can use es5-shim
, es5-sham
and json2
to support old browsers
<!--[if lt IE 9]>
<script src="json2.js"></script>
<script src="es5-shim.min.js"></script>
<script src="es5-sham.min.js"></script>
<![endif]-->
<script type="text/javascript" src="tcomb.js"></script>
<script type="text/javascript">
console.log(t);
</script>
Run mocha
or npm test
in the project root.
What's a type? In tcomb a type is a function T
such that
T
has signature T(value, [mut])
where value
depends on the nature of T
and the optional boolean mut
makes the instance mutable (default false
)T
is idempotent: T(T(value, mut), mut) === T(value, mut)
T
owns a static function T.is(x)
returning true
if x
is an instance of T
Note: 2. implies that T
can be used as a default JSON decoder
In production envs you don't want to leak failures to the user
// override onFail hook
options.onFail = function (message) {
try {
// capture stack trace
throw new Error(message);
} catch (e) {
// use you favourite JavaScript error logging service
console.log(e.stack);
}
};
Adds to structs, tuples, lists and dicts a static method update
that returns a new instance
without modifying the original.
Example
// see http://facebook.github.io/react/docs/update.html
options.update = function (x, updates) {
return React.addons.update(mixin({}, x), updates);
};
var p1 = Point({x: 0, y: 0});
var p2 = Point.update(p1, {x: {$set: 1}}); // => Point({x: 1, y: 0})
assert(guard, [message], [values...]);
If guard !== true
the debugger kicks in.
guard
boolean conditionmessage
optional string useful for debugging, formatted with values like util.format in NodeExample
assert(1 === 2); // throws 'assert(): failed'
assert(1 === 2, 'error!'); // throws 'error!'
assert(1 === 2, 'error: %s !== %s', 1, 2); // throws 'error: 1 !== 2'
To customize failure behaviour, see options.onFail
.
struct(props, [name])
Defines a struct like type.
props
hash name -> typename
optional string useful for debuggingExample
"use strict";
// defines a struct with two numerical props
var Point = struct({
x: Num,
y: Num
});
// methods are defined as usual
Point.prototype.toString = function () {
return '(' + this.x + ', ' + this.y + ')';
};
// costructor usage, p is immutable
var p = Point({x: 1, y: 2});
p.x = 2; // => TypeError
p = Point({x: 1, y: 2}, true); // now p is mutable
p.x = 2; // ok
Returns true
if x
is an instance of the struct.
Point.is(p); // => true
union(types, [name])
Defines a union of types.
types
array of typesname
optional string useful for debuggingExample
var Circle = struct({
center: Point,
radius: Num
});
var Rectangle = struct({
bl: Point, // bottom left vertex
tr: Point // top right vertex
});
var Shape = union([
Circle,
Rectangle
]);
Returns true
if x
belongs to the union.
Shape.is(Circle({center: p, radius: 10})); // => true
maybe(type, [name])
Same as union([Nil, type])
.
// the value of a radio input where null = no selection
var Radio = maybe(Str);
Radio.is('a'); // => true
Radio.is(null); // => true
Radio.is(1); // => false
enums(map, [name])
Defines an enum of strings.
map
hash enum -> valuename
optional string useful for debuggingExample
var Direction = enums({
North: 'North',
East: 'East',
South: 'South',
West: 'West'
});
Returns true
if x
belongs to the enum.
Direction.is('North'); // => true
Returns an enums of an array of keys, useful when you don't mind to define custom values for the enums.
keys
array (or string) of keysname
optional string useful for debuggingExample
// result is the same as the main example
var Direction = enums.of(['North', 'East', 'South', 'West']);
// or..
Direction = enums.of('North East South West');
tuple(types, [name])
Defines a tuple whose coordinates have the specified types.
types
array of coordinates typesname
optional string useful for debuggingExample
var Area = tuple([Num, Num]);
// constructor usage, area is immutable
var area = Area([1, 2]);
Returns true
if x
belongs to the tuple.
Area.is([1, 2]); // => true
Area.is([1, 'a']); // => false, the second element is not a Num
Area.is([1, 2, 3]); // => false, too many elements
subtype(type, predicate, [name])
Defines a subtype of an existing type.
type
the supertypepredicate
a function with signature (x) -> boolean
name
optional string useful for debuggingExample
// points of the first quadrant
var Q1Point = subtype(Point, function (p) {
return p.x >= 0 && p.y >= 0;
});
// costructor usage, p is immutable
var p = Q1Point({x: 1, y: 2});
p = Q1Point({x: -1, y: -2}); // => fail!
Note. You can't add methods to Q1Point
prototype
, add them to the supertype prototype
if needed.
Returns true
if x
belongs to the subtype.
var Int = subtype(Num, function (n) {
return n === parseInt(n, 10);
});
Int.is(2); // => true
Int.is(1.1); // => false
list(type, [name])
Defines an array where all the elements are of type T
.
type
type of all the elementsname
optional string useful for debuggingExample
var Path = list(Point);
// costructor usage, path is immutable
var path = Path([
{x: 0, y: 0},
{x: 1, y: 1}
]);
Returns true
if x
belongs to the list.
var p1 = Point({x: 0, y: 0});
var p2 = Point({x: 1, y: 2});
Path.is([p1, p2]); // => true
dict(type, [name])
Defines a dictionary Str -> type.
type
the type of the valuesname
optional string useful for debuggingExample
// defines a dictionary of numbers
var Tel = dict(Num);
Returns true
if x
is an instance of the dict.
Tel.is({'jack': 4098, 'sape': 4139}); // => true
func(Arguments, f, [Return], [name])
Defines a function where the arguments
and the return value are checked.
Arguments
the type of arguments
(can be a list of types)f
the function to executeReturn
optional, check the type of the return valuename
optional string useful for debuggingExample
var sum = func([Num, Num], function (a, b) {
return a + b;
}, Num);
sum(1, 2); // => 3
sum(1, 'a'); // => fail!
Experimental. I added a tcomb.sjs
file containing some sweet.js macros, here some examples:
// structs
type Point struct {
x: Num,
y: Num
}
// unions
type Shape union {
Circle,
Rectangle
}
// tuples
type Coords tuple {
Num,
Num
}
// enums
type Direction enums {
'North',
'East',
'South',
'West'
}
// enums with specified values
type Direction enums {
'North': 0,
'East': 1,
'South': 2,
'West': 3
}
// subtypes
type Positive subtype<Num> n {
return n > 0;
}
// irriducibles types, like JavaScript primitives
type Irriducible irriducible x {
return typeof x === 'function';
}
// maybe
type Maybe maybe<Str>
// lists
type List list<Str>
// dictionaries
type Dict dict<Str>
// functions
fn add (x: Num, y: Num) {
return x + y;
}
// functions with checked return value
fn add (x: Num, y: Num) -> Num {
return x + y;
}
If you do have a contribution for the package feel free to put up a Pull Request or open an Issue.
The MIT License (MIT)
Copyright (c) 2014 Giulio Canti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Type checking and DDD for JavaScript
The npm package tcomb receives a total of 1,452,011 weekly downloads. As such, tcomb popularity was classified as popular.
We found that tcomb demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
Security News
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.