Validate your model in CI
Catch broken models before they are merged. A CI check has three layers:
- Validation: syntax and semantic errors, and outdated manual layouts. Use
likec4 validate. - Formatting: source files follow the standard format. Use
likec4 format --check. - Your own rules: architecture constraints specific to your team. Write them as tests with Vitest and the model API.
Validate the model
Section titled “Validate the model”likec4 validate checks the workspace for:
- syntax and semantic errors
- layout drift: manual layouts that no longer match the model
# Fail on errors onlynpx likec4 validate
# Fail on errors and on any warningnpx likec4 validate --max-warnings 0The command exits with code 0 when the model is valid.
It exits with code 1 when there are errors, or more warnings than --max-warnings allows.
Without --max-warnings, warnings are printed but do not fail the check.
Useful options:
| Option | What it does |
|---|---|
--max-warnings N |
Fail when there are more than N warnings. 0 fails on any warning |
--no-layout |
Skip the layout drift check |
--file, -f |
Only report errors and warnings from these files (repeatable) |
--project, -p |
Check layout drift of one project in a multi-project workspace |
--json |
Print the result as JSON, for other tools to read |
The JSON output has valid, errors, warnings, maxWarnings and stats.
Each error and warning has a message, a file, a line and a range.
See the CLI reference for all options.
Check formatting
Section titled “Check formatting”likec4 format --check reports the files that need formatting.
It does not change them.
npx likec4 format --checkThe command exits with code 1 if any file needs formatting.
Run npx likec4 format locally to fix them.
Use --project or --files to check only part of the workspace.
See the CLI reference.
Write your own rules
Section titled “Write your own rules”Some rules are specific to your architecture.
For example, “every app has a technology”, or “every relationship has an owner”.
Write them as tests with Vitest and the model API.
npm i -D likec4 vitestpnpm add -D likec4 vitestyarn add -D likec4 vitestbun add -d likec4 vitestExample
Section titled “Example”This test checks that every element of kind app has a technology:
import { LikeC4 } from 'likec4'import { test } from 'vitest'
// Load the workspace and compute the model.// The path is resolved from the folder where you run vitest.const likec4 = await LikeC4.fromWorkspace('.')const model = await likec4.computedModel()
// `test.for` generates a test per element,// so each failure is reported separatelytest.for( model // Select elements of kind `app` .elementsWhere({ kind: 'app' }) // Map to [id, element] tuples, the id is used in the test name .map(e => [e.id, e] as const) .toArray(),)('app "%s" has technology', ([, e], { expect }) => { expect(e.technology).toBeTruthy()})
// Or use `expect.soft` to collect all failures in one testtest('elements of kind `app` have technology', ({ expect }) => { expect.hasAssertions() for (const app of model.elementsWhere({ kind: 'app' })) { expect.soft(app.technology, `app ${app.id} has no technology`).toBeTruthy() }})elementsWhere returns an iterator.
Use .toArray() when you need an array, or iterate it with for...of.
See the model API reference for all query methods.
Pre-generate the model
Section titled “Pre-generate the model”Tests start faster with a pre-generated model. Generate it once in Vitest global setup:
import { execSync } from 'node:child_process'
export default function() { execSync('npx likec4 gen model -o ./test/likec4-model.ts', { stdio: 'inherit', })}The generated model is typed. You get type checking and autocompletion in tests:
import { likec4model } from './likec4-model'import { test } from 'vitest'
test('relationships have metadata', ({ expect }) => { expect.hasAssertions() for (const r of likec4model.relationships()) { expect.soft( r.getMetadata('key'), // type-checked key `Relationship ${r.source.id} -> ${r.target.id} has no metadata`, ).toBeDefined() }})Share the model with a test fixture
Section titled “Share the model with a test fixture”A test context fixture puts the model into every test:
import { likec4model } from './likec4-model'import { test } from 'vitest'
interface LikeC4TestFixtures { likec4: typeof likec4model}
// A test function with the model in its contextexport const likec4test = test.extend<LikeC4TestFixtures>({ likec4: async ({}, use) => { await use(likec4model) },})Use it in your tests:
import { likec4test } from './likec4test'
likec4test('relationships have metadata', ({ expect, likec4 }) => { expect.hasAssertions() for (const r of likec4.relationships()) { expect.soft( r.getMetadata('key'), `Relationship ${r.source.id} -> ${r.target.id} has no metadata`, ).toBeDefined() }})Run the checks on pull requests
Section titled “Run the checks on pull requests”This GitHub Actions workflow runs all three checks on every pull request.
It expects likec4 and vitest in your devDependencies.
name: LikeC4
on: pull_request:
jobs: check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7
- uses: actions/setup-node@v7 with: node-version: 22 # likec4 requires Node.js 22.23.3 or newer cache: npm
- run: npm ci
- name: Validate run: npx likec4 validate --max-warnings 0
- name: Check formatting run: npx likec4 format --check
- name: Architecture rules run: npx vitest runEach step fails the job on a non-zero exit code. Other CI systems (GitLab CI, Azure Pipelines, Jenkins) run the same three commands.