@bicknese/gater
v1.0.0
Published
A lightweight TypeScript validation library. As Deno is a play on Dino, Gater is a play on Gator.
Downloads
6
Maintainers
Readme
Gater
[!IMPORTANT]
This is a tech demo, please use a vetted library like zod for your production applications.
Gater is a simple, modern and secure object validator for JavaScript and TypeScript.
Features
- Validates values against schema's during runtime. Quickly know if external input matches the expected shape.
- Utilizes TypeScript to type check known object shapes against the schema in build time.
- Create re-usable validators, removing the need to pass the schema each time.
Usage
Run a one-off validation to determine whether an object is valid
isValid(
{ prey: "string", eaten: "boolean" },
{ prey: "fish", eaten: false } // Gater doesn't eat friends
);
// ✅ `true`, given value is valid
Create the validator in advance and validate multiple values
const isFood = isValid({ prey: "string", eaten: "boolean" });
isFood({ prey: "frog", eaten: false });
// ✅ `true`, checks out
isFood({ prey: "seaweed", eaten: 5 });
// ❌ `false`, 5 is not a boolean
Use it to early catch issues with user input
const isFood = isValid({ prey: "string" });
function onSubmit(food) {
assert(isFood(food));
// ... safely rely on food being food
}
Node
Install from npmjs
npm i --save @bicknese/gater
And import the isValid
function
import { isValid } from '@bicknese/gater';
Deno
Import directly through npm support
import { isValid } from 'npm:@bicknese/gater';