2.0.0 • Published 3 months ago

@jaybeeuu/is v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

is

npm

A collection of functions which allow you to build up validators, and types for incoming data. Use at the edges of your application, to ensure type safety when deserializing for example.

This is pretty rough and experimental, but it's quick, and small. If you want something more full featured and production read then try runtypes.

Example:

import {
  type CheckedBy,
  assert,
  isTuple,
  is,
  isLiteral,
  isObject,
  isArrayOf,
  isUnionOf,
  TypeAssertion,
} from "@jaybeeuu/is";

const isPosition = isTuple(is("string"), is("string"));

const isRank = isUnionOf(
  isLiteral("captain"),
  isLiteral("first mate"),
  isLiteral("officer"),
  isLiteral("ensign"),
);

const isCrewMember = isObject({
  name: is("string"),
  age: is("number"),
  rank: isRank,
});

const isShip = isObject({
  affiliation: isUnionOf(isLiteral("Royal Navy"), isLiteral("Pirate")),
  position: isPosition,
  guns: is("number"),
  name: is("string"),
  crew: isArrayOf(isCrewMember),
});
type Ship = CheckedBy<typeof isShip>;
const assertIsShip: TypeAssertion<Ship> = assert(isShip);

const thing: unknown = {
  affiliation: "Pirate",
  position: ["13°05′52″N", "59°37′06″W"],
  name: "Flying Dutchman",
  crew: [
    {
      age: 1392,
      name: "Willem van der Decken",
      rank: "captain",
    },
  ],
  guns: 68,
};

assertIsShip(thing);
const ship: Ship = thing;
2.0.0

3 months ago

1.0.0

3 months ago

0.0.0

3 months ago