APIs
Full list of available APIs.
UnContract
TypeScript only
A type that allows to extract the result type of a Contract.
const User = obj({ name: str, age: num });
type User = UnContract<typeof User>; // { name: string, age: number }
Contract
TypeScript only
A Contract is a type that allows to check if a value is conform to a given structure.
function age(min, max): Contract<unknown, number> {
return {
isData: (data) => typeof data === "number" && data >= min && data <= max,
getErrorMessages: (data) =>
`Expected a number between ${min} and ${max}, but got ${data}`,
};
}
bool
Contract that checks if a value is a boolean.
bool.isData(true) === true;
bool.isData(42) === false;
num
Contract that checks if a value is a number.
num.isData(42) === true;
num.isData("42") === false;
str
Contract that checks if a value is a string.
str.isData("hello") === true;
str.isData(42) === false;
val
Function that creates a Contract that checks if a value is equal to a given value.
const only42 = val(42);
only42.isData(42) === true;
only42.isData(43) === false;
or
Function that creates a Contract that checks if a value is conform to one of the given Contracts.
const stringOrNumber = or(str, num);
stringOrNumber.isData("hello") === true;
stringOrNumber.isData(42) === true;
stringOrNumber.isData(true) === false;
and
Is has multiple overloads 👇
and(objectA, objectB)
Function that merges two Contracts of objects into one.
const User = obj({ name: str });
const Admin = and(User, obj({ permitted: bool }));
and(first, ...rest)
Function that creates a Contract that checks if a value is conform to all of the given Contracts.
function age(min, max): Contract<number, number> {
return {
isData: (data) => data >= min && data <= max,
getErrorMessages: (data) =>
`Expected a number between ${min} and ${max}, but got ${data}`,
};
}
const User = obj({ name: str, age: and(num, age(18, 100)) });
obj
Is has multiple overloads 👇
obj(str, contract)
Function that creates a Contract that checks if a value is object and every property is conform to the given Contract.
const Ids = obj(str, num);
Ids.isData({ id1: 1, id2: 2 }) === true;
Ids.isData({ id1: 1, id2: "2" }) === false;
obj(shape)
Function that creates a Contract that checks if a value is conform to an object with the given Contracts as properties.
const User = obj({ name: str, age: num });
User.isData({ name: "Alice", age: 42 }) === true;
User.isData({ name: "Alice" }) === false;
arr
Function that creates a Contract that checks if a value is conform to an array of the given Contracts.
const arrayOfStrings = arr(str);
arrayOfStrings.isData(["hello", "world"]) === true;
arrayOfStrings.isData(["hello", 42]) === false;
tuple
Function that creates a Contract that checks if a value is conform to a tuple of the given Contracts.
const userAges = tuple(str, num);
userAges.isData(["Alice", 42]) === true;
userAges.isData(["Alice", "what"]) === false;
nothing
since v1.1.0
Contract checking if a value is null or undefined. In case of usage as field in obj
Contract, it will allow to omit the field.
const User = obj({ name: str, age: or(num, nothing) });
User.isData({ name: "Alice", age: 42 }) === true;
User.isData({ name: "Alice" }) === true;
User.isData({ name: "Alice", age: null }) === true;
User.isData({ name: "Alice", age: undefined }) === true;
User.isData({ name: "Alice", age: "four two" }) === false;
anything
since v1.1.0
Contract that allows any value, basically a no-op.
anything.isData("hello") === true;
anything.isData(42) === true;
anything.isData({}) === true;
anything.isData([]) === true;
anything.isData(null) === true;
anything.isData(undefined) === true;