
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@schoolai/spicedb-zed-schema-parser
Advanced tools
SpiceDB .zed file format parser and analyzer written in Typescript
A TypeScript library for parsing, analyzing, and generating type-safe SDKs from SpiceDB's .zed schema files.
This library provides a complete toolchain for working with SpiceDB schemas, transforming its schema DSL into type-safe TypeScript APIs. It consists of three main components:
.zed files into structured ASTs using ChevrotainAdditionally, it includes a Fluent Builder Library that provides an ergonomic API for SpiceDB operations, serving as a bridge between the verbose @authzed/authzed-node gRPC client and your type-safe generated SDK.
Convert string-based SpiceDB operations into compile-time checked TypeScript:
// β Error-prone: strings everywhere, no compile-time validation
await client.checkPermission({
resource: {
objectType: "document",
objectId: "doc1",
},
permission: "edit", // Could be misspelled
subject: {
object: {
objectType: "user",
objectId: "alice",
},
},
});
// β
Type-safe: generated from your schema
await permissions.document.check.edit("user:alice", "document:doc1").execute();
Catch schema errors early with comprehensive semantic analysis:
Replace verbose gRPC objects with fluent, chainable APIs:
(Note: this layer is not type-safe, but you can drop down to it if the type-safe SDK you generate from your schema.zed file is insufficient to the task).
// β Verbose gRPC style
await client.writeRelationships({
updates: [
{
operation: RelationshipUpdate_Operation.TOUCH,
relationship: {
resource: { objectType: "document", objectId: "doc1" },
relation: "editor",
subject: { object: { objectType: "user", objectId: "alice" } },
},
},
],
});
// β
Fluent builder style
await perms
.grant("editor")
.subject("user:alice")
.resource("document:doc1")
.execute();
Automatically generate SDKs that stay in sync with schema changes, preventing runtime errors when schemas evolve.
pnpm install @schoolai/spicedb-zed-schema-parser
Here's a complete example of parsing a schema and generating a type-safe SDK:
import fs from "node:fs/promises";
import {
parseSpiceDBSchema,
analyzeSpiceDbSchema,
generateSDK,
} from "@schoolai/spicedb-zed-schema-parser";
async function generatePermissionsSDK() {
// 1. Read your schema file
const schemaContent = await fs.readFile("schema.zed", "utf-8");
// 2. Parse the schema
const { ast, errors: parseErrors } = parseSpiceDBSchema(schemaContent);
if (parseErrors.length > 0) {
console.error("Parse errors:", parseErrors);
return;
}
// 3. Analyze the schema
const {
augmentedAst,
errors: analysisErrors,
isValid,
} = analyzeSpiceDbSchema(ast!);
if (!isValid) {
console.error("Analysis errors:", analysisErrors);
return;
}
// 4. Generate TypeScript SDK
const generatedCode = generateSDK(augmentedAst!);
// 5. Write to file
await fs.writeFile("generated/permissions.ts", generatedCode);
console.log("β
Type-safe permissions SDK generated!");
}
definition user {}
definition document {
relation owner: user
relation editor: user
relation viewer: user
permission edit = owner + editor
permission view = owner + editor + viewer
}
definition folder {
relation owner: user
relation editor: user
relation parent: folder
permission edit = owner + editor + parent->edit
permission view = owner + editor + parent->view
}
The generated SDK provides type-safe methods for all your schema operations:
import { permissions } from "./generated/permissions";
// β
Type-safe operations - TypeScript will catch typos and invalid combinations
await permissions.document.grant
.editor("user:alice", "document:doc1")
.execute();
await permissions.document.check.view("user:bob", "document:doc1").execute();
await permissions.folder.find.byOwner("user:alice").execute();
// β TypeScript errors for invalid operations
await permissions.document.grant.invalidRelation("user:alice", "document:doc1"); // Error!
await permissions.document.check.edit("invalid:type", "document:doc1"); // Error!
For cases where you need dynamic operations or are migrating from string-based APIs, use the fluent builder:
import {
createPermissions,
Operations,
} from "@schoolai/spicedb-zed-schema-parser";
const perms = createPermissions(spicedbClient);
// Grant permissions
await perms
.grant("editor")
.subject("user:alice")
.resource("document:doc1")
.execute();
// Check permissions
const hasPermission = await perms
.check("view")
.subject("user:bob")
.resource("document:doc1")
.execute();
// Batch operations
await perms
.batch()
.grant("viewer")
.subject("user:charlie")
.resource("folder:f1")
.and()
.revoke("editor")
.subject("user:alice")
.resource("document:doc1")
.and()
.commit();
// Use static builders for pure operations
const deleteOp = Operations.delete().where({
resourceType: "document",
resourceId: "doc1",
});
await perms.execute(deleteOp);
parseSpiceDBSchema(text: string): ParseResultParses a SpiceDB schema string into an AST.
const { ast, errors } = parseSpiceDBSchema(schemaContent);
analyzeSpiceDbSchema(ast: SchemaAST): SchemaAnalysisResultPerforms semantic analysis on a parsed schema.
const { augmentedAst, errors, isValid } = analyzeSpiceDbSchema(ast);
generateSDK(schema: AugmentedSchemaAST): stringGenerates TypeScript code for a type-safe permissions SDK.
const generatedCode = generateSDK(augmentedAst);
createPermissions(client: SpiceDBClient): PermissionsCreates a permissions instance with bound SpiceDB client.
Operations (Static Builder)Provides static methods for creating pure operations:
Operations.grant(relation: string)Operations.revoke(relation: string)Operations.check(permission: string)Operations.find()Operations.delete()Operations.batch()permission = rel1 + rel2permission = rel1 & rel2permission = rel1 - rel2permission = rel->permissionrelation public: user:*relation editor: user#admin/** documentation */The library provides comprehensive error reporting:
const { ast, errors } = parseSpiceDBSchema(invalidSchema);
if (errors.length > 0) {
errors.forEach((err) => {
console.error(`${err.message} at line ${err.line}, column ${err.column}`);
});
}
const { isValid, errors } = analyzeSpiceDbSchema(ast);
if (!isValid) {
errors.forEach((err) => {
console.error(`${err.code}: ${err.message}`);
});
}
Common error types:
UNDEFINED_TYPE - Referenced type doesn't existCIRCULAR_DEPENDENCY - Circular permission dependenciesDUPLICATE_DEFINITION - Duplicate type namesUNDEFINED_RELATION - Referenced relation doesn't existINVALID_EXPRESSION - Malformed permission expressionβββββββββββββββ ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββ
β .zed Schema βββββΆβ Parser βββββΆβ Semantic βββββΆβ SDK β
β β β (Chevrotain) β β Analyzer β β Generator β
βββββββββββββββ ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββ
β AST β β Augmented AST β β TypeScript β
β β β + Type Info β β SDK Code β
ββββββββββββββββ βββββββββββββββββββ ββββββββββββββββ
β²
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Fluent Builder Library β
β βββββββββββββββ ββββββββββββββββ βββββββββββββββββββ β
β β Operations βββββΆβ Fluent API βββββΆβ SpiceDB Client β β
β β Builder β β (Chainable) β β (gRPC) β β
β βββββββββββββββ ββββββββββββββββ βββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pnpm run build
pnpm test
pnpm run lint
pnpm run lint:fix
git checkout -b feature/amazing-featurepnpm testgit commit -m 'Add amazing feature'git push origin feature/amazing-featureOpen source under the MIT license
.zed parsingFAQs
SpiceDB .zed file format parser and analyzer written in Typescript
We found that @schoolai/spicedb-zed-schema-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 3 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.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authoritiesβ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socketβs new workflow scanning support.