3.0.1 â€Ē Published 9 months ago

tiny-guards v3.0.1

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

tiny-guards 💂

A tiny library for advanced typescript guarding

  • ðŸŠķ lightweight
  • 🍃 tree-shakable
  • ðŸ§ą composable
  • ðŸ‘Ū type-safe
  • 🔗 zero dependencies
  • 🌚 dead simple
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")
  }
}
3.0.1

9 months ago

3.0.0

9 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

3 years ago

0.0.0-pre.5

3 years ago

0.0.0-pre.4

3 years ago

0.0.0-pre.3

3 years ago

0.0.0-pre.2

3 years ago

0.0.0-pre.1

3 years ago

0.0.0-pre.0

3 years ago

0.0.0-pre

3 years ago