Skip to content
Projects

About

the logic to sql compiler for authorization

Data & Analytics

How these counts are measured

Measured star growth

in 24h
Collecting history
in 7 days
Collecting history
in 15 days
Collecting history
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README.md

Original

Verdict

Verdict lets you write fine-grained authorization rules that compile to SQL and run in PostgreSQL or SQLite using your existing tables, connections, and transactions.

How it works

Start your .vd policy with @database("postgres") or @database("sqlite") to select the database that supplies facts. Use @table(...) to map tables to facts and @context to declare facts supplied by your application for a request. For example, in a document-sharing app where workspace members and direct share recipients can read documents:

@database("postgres")

@table("documents", id: "text", workspace_id: "text")
declare in_workspace(Document, Workspace);

@table("workspace_members", user_id: "text", workspace_id: "text")
declare member(User, Workspace);

@table("document_shares", document_id: "text", user_id: "text")
declare shared_with(Document, User);

allow(user: User, "read", doc: Document) if
  in_workspace(doc, ws) and
  member(user, ws);

allow(user: User, "read", doc: Document) if
  shared_with(doc, user);

When your application checks whether a user can read a document, Verdict compiles the policy into a single parameterized SQL query that executes directly on your database:

SELECT EXISTS (
  SELECT 1
  FROM documents d
  JOIN workspace_members m ON m.workspace_id = d.workspace_id
  WHERE d.id = $1 AND m.user_id = $2
  UNION
  SELECT 1
  FROM document_shares s
  WHERE s.document_id = $1 AND s.user_id = $2
) AS authorized;

One policy supports role checks, relationships, recursive hierarchies, attributes, and explicit deny overrides.

Quick start (PostgreSQL)

Requires Node.js 24 or later. Install the SDK, PostgreSQL backend, driver, and CLI in your application:

npm install @useverdict/verdict @useverdict/postgres pg
npm install -D @useverdict/cli

Save the document-sharing policy from How it works as policy.vd. Generate a JSON array of the tables and columns declared with @table(...). Pass the array to the PostgreSQL backend as sources.

npx verdict generate catalog policy.vd --output verdict.catalog.json

With profile: "authorization", pass the contents of policy.vd to createClient as program to evaluate its allow and deny rules. The adapter obtains a connection from the pool when a check needs to query the database.

import { readFileSync } from "node:fs";
import { Pool } from "pg";
import { column, createClient, identity } from "@useverdict/verdict";
import {
  createPostgresBackend,
  createNodePostgresAdapter,
  type PostgresFactSource,
} from "@useverdict/postgres";
import generatedSources from "./verdict.catalog.json" with { type: "json" };

const sources = generatedSources as readonly PostgresFactSource[];

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

const client = createClient({
  program: readFileSync("policy.vd", "utf8"),
  profile: "authorization",
  backend: createPostgresBackend({ sources, adapter: createNodePostgresAdapter(pool) }),
});

// Optional startup or test check for the tables and views declared with @table(...).
await client.verifyTables();

const user = identity("User", userId);
const doc = identity("Document", docId);

// Boolean check or throwing guard
const allowed = await client.authorize(user, "read", doc);
await client.enforce(user, "read", doc);

// Or compose the same policy directly into your own SQL list queries:
const auth = client.sql.exists(
  identity("User", userId),
  "read",
  column("documents.id", "Document"),
);
const { rows } = await pool.query(
  `SELECT id, title FROM documents WHERE ${auth.sql} ORDER BY id LIMIT 50`,
  auth.parameters,
);

The optional verifyTables() call checks that the tables, views, and columns declared with @table(...) exist and that the column types match. PostgreSQL also checks that text columns use deterministic collations. Verdict checks read access to the mapped columns with SELECT ... LIMIT 0 and reports mismatches in a TableVerificationError.

To evaluate against uncommitted writes in an active transaction, pass { tx } (or { conn } for a reserved connection) to the client method, such as client.enforce(user, "read", doc, { tx }). Verdict also includes native adapters for postgres (postgres.js), @neondatabase/serverless, and Bun.

See examples/document-sharing/ for a complete runnable PostgreSQL project with schema and seed data, @useverdict/postgres for adapters and transactions, and @useverdict/verdict for the SDK API.

Testing policies locally

To test SQL-backed rules locally, add inline test blocks to the policy and supply sample facts for the rules to query. verdict test runs each block in memory using the supplied facts.

test "membership and direct sharing" {
  in_workspace(Document:handbook, Workspace:acme);
  in_workspace(Document:roadmap, Workspace:acme);
  member(User:alice, Workspace:acme);
  shared_with(Document:handbook, User:bob);

  assert allow(User:alice, "read", Document:roadmap);
  assert allow(User:bob, "read", Document:handbook);
  assert not allow(User:bob, "read", Document:roadmap);
  assert not allow(User:carol, "read", Document:handbook);
}
npx verdict validate policy.vd
npx verdict test policy.vd

The CLI also provides formatting and an editor language server. To install verdict without Node.js (macOS Apple Silicon, Linux x64, and Linux arm64), use Homebrew (brew install useverdict/tap/verdict) or the shell installer (curl -fsSL https://useverdict.dev/install.sh | sh). See @useverdict/cli for all commands and install options.

SQLite support

For local testing or SQLite applications, @useverdict/sqlite evaluates policies against your SQLite tables over Node.js node:sqlite (DatabaseSync) or custom async SQLite adapters.

Verdict for general logic programming

Verdict is a general-purpose, typed logic language whose ergonomics were shaped for authorization. You can also use it for non-authorization workloads:

  • SQL-backed rules: Use the general profile in @useverdict/verdict to compile custom rules to PostgreSQL or SQLite and run checks, queries, and proofs against your existing tables.
  • Logic scripting: Run .vd scripts directly with verdict run or vdrt (@useverdict/runtime) with no database setup required:
depends_on(Service:api, Service:auth);
depends_on(Service:auth, Service:db);

reaches(a, b) if depends_on(a, b);
reaches(a, c) if depends_on(a, b) and reaches(b, c);

reaches(Service:api, ?target)?;
npx verdict run script.vd
# or with @useverdict/runtime:
npx vdrt script.vd

Packages

Package Role
@useverdict/verdict Authorization client (authorize, enforce, enumerate, filter, sql)
@useverdict/postgres PostgreSQL backend and driver adapters (pg, postgres, Neon, Bun)
@useverdict/sqlite SQLite backend and node:sqlite / async adapters
@useverdict/cli verdict CLI (validate, test, run, fmt, lsp, generate catalog)
@useverdict/runtime Program and test execution library with vdrt runner
@useverdict/language Lexer, parser, validator, and table-mapping compiler
@useverdict/engine Declarative logic-to-SQL compiler and evaluation core
@useverdict/fmt Code formatter (verdict-fmt)
@useverdict/lsp Language Server Protocol server and static linter (verdict-lsp)
@useverdict/tree-sitter Tree-sitter grammar and WebAssembly parser
@useverdict/values Signed 64-bit integer normalization and JSON serialization helpers

License

Verdict is licensed under the Apache License, Version 2.0.

Trademarks: Verdict™ and the Verdict logos and wordmarks (in any color or configuration) are product names and trademarks of Marc Porciuncula (trading as Strange Computer). The Apache-2.0 license does not grant permission to use the Verdict name, wordmarks, or logos to endorse, promote, or brand derivative works or third-party services; see NOTICE and Section 6 of the LICENSE.

Explore related projects

Loading…