3.0.1 âĒ Published 9 months ago
tiny-guards v3.0.1
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