Define contracts once. Generate everything.
Forge is a contract-first language for defining data contracts and generating code artifacts from a single source of truth.
Forge is currently in v0.1.0.
Implemented:
- Contract-first DSL
- Namespace support
- Contracts
- Optional fields
- Invariants
- Langium-based parser
- Semantic Model architecture
- TypeScript generator
- JSON Schema Draft 2020-12 generator
- Zod generator
- CLI tooling
Planned:
- OpenAPI generator
- Improved diagnostics
- VS Code extension
pnpm install pnpm build
The generated Zod artifacts import zod, which is included as a project dependency.
Initialize a Forge project:
pnpm forge init
Create contracts under:
contracts/**/*.forge
Compile all contracts:
pnpm forge compile
Generated files are written to:
generated/
|-- typescript/
|-- json-schema/
`-- zod/
namespace financeiro
contract Usuario {
id: uuid
nome: string
email?: string
invariant id != ""
}
export interface Usuario { id: string; nome: string; email?: string; }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Usuario",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"nome": {
"type": "string"
},
"email": {
"type": "string"
}
},
"required": [
"id",
"nome"
]
}import { z } from "zod"; export const UsuarioSchema = z.object({ id: z.string(), nome: z.string(), email: z.string().optional(), }); export type Usuario = z.infer<typeof UsuarioSchema>;
namespace financeiro.pagamentos
contract PedidoPago {
pedidoId: uuid
valor: decimal
}
cupom?: string
invariant valor > 0
Invariants are parsed and kept in the Semantic Model. Zod invariant validation is not generated yet.
| Forge Type | TypeScript | JSON Schema | Zod |
|---|---|---|---|
| string | string | string | z.string() |
| int | number | number | z.number().int() |
| float | number | number | z.number() |
| decimal | number | number | z.number() |
| boolean | boolean | boolean | z.boolean() |
| uuid | string | string | z.string() |
| datetime | string | string | z.string() |
| date | string | string | z.string() |
Forge follows a layered architecture:
Forge Source
|
Parser
|
AST
|
Semantic Model
|
Generators
|
Artifacts
Current generators:
- TypeScript
- JSON Schema
- Zod
Generators consume only the Semantic Model, making it possible to add new targets without changing the parser or language definition.
packages/
|-- cli/
|-- generators/
`-- language/
examples/
Contains:
- Langium grammar
- Parser
- AST access
- Semantic Model
- Validations
- createContractId
Contains:
- TypeScript generator
- JSON Schema generator
- Zod generator
Contains:
- forge init
- forge compile
Run all tests:
pnpm testBuild all packages:
pnpm build
- Better diagnostics
- OpenAPI generation
- Enhanced validation support
- Additional language targets