Skip to content

APIs

Full list of available APIs.

UnContract TypeScript only

A type that allows to extract the result type of a Contract.

ts
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.

ts
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.

ts
bool.isData(true) === true;
bool.isData(42) === false;

num

Contract that checks if a value is a number.

ts
num.isData(42) === true;
num.isData("42") === false;

str

Contract that checks if a value is a string.

ts
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.

ts
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.

ts
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.

ts
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.

ts
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.

ts
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.

ts
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.

ts
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.

ts
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.

ts
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.

ts
anything.isData("hello") === true;
anything.isData(42) === true;
anything.isData({}) === true;
anything.isData([]) === true;
anything.isData(null) === true;
anything.isData(undefined) === true;

Released under the MIT License.