Hi everyone, this is my first post on Dev.to and I'm very excited to hear from you guys.
For the last couple of weeks I've been working on a validation library and yesterday I've published the second major version of it on npm. This package was my first npm package.
See it on npm: handy-types
GitHub Repository: handy-types
It's a lite weight validation library that I've developed as a side project to reduce validation boilerplate in my other validation library that I've been working on for the past few months 😅. But I've decided to publish on npm in case others find it useful.
Example Usages (TypeScript):
import { is, assert } from "handy-types";
let value: unknown;
is("integer", value); // false
is("positive_number", value); // false
is("8bit_unsigned_integer", value); // false
is("non_null_object | plain_object", value); // false
if (is<string | string[]>("non_empty_string | non_empty_string[]", value)) {
value; // here the type of value is: string | string[]
}
// we can use caching for improved performance
if ( is.cache<string | string[]>("non_empty_string | non_empty_string[]", value)) {
value; // here the type of value is: string | string[]
}
assert("integer", value);
// throws error: `Value must be of type Integer`
assert("integer", value, {
name: "Age",
code: "INVALID_AGE",
});
// throws error: `Age must be of type Integer`, with code: "INVALID_AGE"
assert("non_empty_string", value, {
message: "Invalid path",
otherInfo: {
path: value,
errorId: -3,
},
});
// throws error: `Invalid path` , with properties path: undefined, errorId: -3
// use caching for improved performance
assert.cache<string | string[]>(
"non_empty_string | non_empty_string[]",
value,
{ name: "hobbies" }
); // throws error: "hobbies must of type: Non-Empty String or Non-Empty String Array"
Please refer to the github README for detailed documentation.
Kindly let me know if you think I can improve something. Thanks in advance 💝.
Top comments (0)