
Security News
High Salaries No Longer Enough to Attract Top Cybersecurity Talent
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
@badrap/valita
Advanced tools
A TypeScript library for validating & parsing structured objects. The API is heavily influenced by Zod's excellent API, while the implementation side aims for the impressive performance of simple-runtypes.
We also pay special attention for providing descriptive validation error messages:
const vehicle = v.union(
v.object({ type: v.literal("plane"), airline: v.string() }),
v.object({ type: v.literal("train") }),
v.object({ type: v.literal("automobile"), make: v.string() })
);
vehicle.parse({ type: "bike" });
// ValitaError: invalid_literal at .type (expected "plane", "train" or "automobile")
npm i @badrap/valita
A motivating example in lack of any better documentation:
import * as v from "@badrap/valita";
const Pet = v.object({
type: v.union(v.literal("dog"), v.literal("cat")),
name: v.string(),
});
const Person = v.object({
name: v.string(),
age: v.number(),
pets: v.array(Pet).optional(),
});
Now Person.parse(value)
returns value
if it matches the Person schema - or throws an error otherwise.
const grizzlor = Person.parse({
name: "Grizzlor",
age: 101,
pets: [
{ type: "cat", name: "Mittens" },
{ type: "cat", name: "Parsley" },
{ type: "cat", name: "Lulu" },
{ type: "cat", name: "Thomas Percival Meowther III" },
],
});
The real magic here comes from TypeScript's type inference. The inferred type for grizzlor
is:
const grizzlor: {
name: string;
age: number;
pets?: { type: "dog" | "cat"; name: string }[] | undefined;
};
You can use Infer<T>
to get your mitts on the inferred type in your code:
type PersonType = v.Infer<typeof Person>;
This library is licensed under the MIT license. See LICENSE.
FAQs
A validation & parsing library for TypeScript
The npm package @badrap/valita receives a total of 17,053 weekly downloads. As such, @badrap/valita popularity was classified as popular.
We found that @badrap/valita demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
Product
Socket, the leader in open source security, is now available on Google Cloud Marketplace for simplified procurement and enhanced protection against supply chain attacks.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.