Skip to content

Validate your model in CI

Catch broken models before they are merged. A CI check has three layers:

  1. Validation: syntax and semantic errors, and outdated manual layouts. Use likec4 validate.
  2. Formatting: source files follow the standard format. Use likec4 format --check.
  3. Your own rules: architecture constraints specific to your team. Write them as tests with Vitest and the model API.

likec4 validate checks the workspace for:

  • syntax and semantic errors
  • layout drift: manual layouts that no longer match the model
Terminal window
# Fail on errors only
npx likec4 validate
# Fail on errors and on any warning
npx likec4 validate --max-warnings 0

The 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.

likec4 format --check reports the files that need formatting. It does not change them.

Terminal window
npx likec4 format --check

The 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.

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 vitest

This test checks that every element of kind app has a technology:

test/metadata.spec.ts
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 separately
test.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 test
test('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.

Tests start faster with a pre-generated model. Generate it once in Vitest global setup:

global-setup.ts
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:

test/metadata.spec.ts
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()
}
})

A test context fixture puts the model into every test:

test/likec4test.ts
import { likec4model } from './likec4-model'
import { test } from 'vitest'
interface LikeC4TestFixtures {
likec4: typeof likec4model
}
// A test function with the model in its context
export const likec4test = test.extend<LikeC4TestFixtures>({
likec4: async ({}, use) => {
await use(likec4model)
},
})

Use it in your tests:

test/metadata.spec.ts
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()
}
})

This GitHub Actions workflow runs all three checks on every pull request. It expects likec4 and vitest in your devDependencies.

.github/workflows/likec4.yml
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 run

Each step fails the job on a non-zero exit code. Other CI systems (GitLab CI, Azure Pipelines, Jenkins) run the same three commands.