Runtime type validation and structure guard for JSON-compatible objects in TypeScript.
Willowisp is a lightweight and extensible runtime type validation system for TypeScript.
It allows developers to define symbolic schemas for JSON-compatible types and safely assert unknown data at runtime with strong type narrowing.
Built for use in data APIs, compilers, and any system requiring reliable structural integrity checks beyond static typing.
npm install willowispimport { Guard, Structure } from "willowisp";This example demonstrates how to define and validate nested object structures with referenced types using willowisp.
type UserDetail = {
address: string;
job: string;
salary?: number;
};The corresponding structure uses $optional: true to mark optional fields, and $additional: true to allow unspecified properties in the object:
const UserDetailStructure = {
$relation: "AND",
$types: [
{
$additional: true,
$kind: StructureType.Object,
$value: {
salary: {
$optional: true,
$relation: "AND",
$types: [{ $kind: StructureType.Primitive, $value: "number" }],
},
...
},
},
],
} satisfies Structure;Use Guard.set() to register the structure and receive a unique key (UserDetailStructureKey) for referencing it later.
const UserDetailStructureKey = Guard.set("UserDetail", UserDetailStructure);interface User {
id: number;
name: string;
email?: string;
roles: string[];
profile: {
active: boolean;
status: number | string | boolean | null;
tag: null;
};
detail: UserDetail;
}In the UserStructure, the detail field references the previously registered UserDetailStructureKey via $kind: Structure.Reference:
const UserStructure = {
$relation: "AND",
$types: [
{
$kind: Structure.Object,
$value: {
detail: {
$relation: "AND",
$types: [{ $kind: Structure.Reference, $value: UserDetailStructureKey }],
},
...
},
},
],
} satisfies Structure;This allows large structures to be modular, reusable, and easily maintained by separating shared definitions.
All structures must be registered using Guard.set(name, structure) to be available for validation. You can use the registered key to perform runtime validation:
const input: unknown = { ... };
const UserStructureKey = Guard.set("User", UserStructure);
if (Guard.assert<User>(input, UserStructureKey)) {
// `input` is now type-safe as `User`
}