Skip to content

madeofsun/tiny-guards

Repository files navigation

tiny-guards 💂

A tiny library for advanced typescript guarding

  • 🪶 lightweight
  • 🍃 tree-shakable
  • 🧱 composable
  • 👮 type-safe
  • 🔗 zero dependencies
  • 🌚 dead simple

npm package build status coverage

npm install tiny-guards

Usage example

import {
  asserts,
  gt,
  isNumber,
  isString,
  maxLen,
  oneOf,
  optional,
  refine,
  shape,
  Guard,
  tinyGuards,
} from "tiny-guards";

const isNaturalNumber = refine(isNumber, gt(0), Number.isSafeInteger);
const isShortString = refine(isString, maxLen(16));
const isAccountType = oneOf(["reader", "publisher", "moderator"]);

const isUser = shape({
  id: isNaturalNumber,
  username: isShortString,
  accountType: isAccountType,
  firstName: optional(isString),
  lastName: optional(isString),
});

type User = GuardInfer<typeof isUser>;

function doSomething(v: unknown) {
  try {
    asserts(v, isUser);

    v.id; // ✅
    v.username; // ✅
  } catch (error) {
    console.error(error);
    // TinyGuardsError: validation failed
    // [shape]: value at key "id" is blocked by guard "refine"
    // [refine]: value is blocked by refinement "gt" (index "0")
  }
}

function doSomething(v: unknown) {
  if (isUser(v)) {
    v.id; // ✅
    v.username; // ✅
  } else {
    isUser.error;
    // TinyGuardsError: validation failed
    // [shape]: value at key "id" is blocked by guard "refine"
    // [refine]: value is blocked by refinement "gt" (index "0")
  }
}

About

A tiny library for advanced typescript guarding

Topics

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Contributors