Skip to content

How To Use Rules

Once you’ve created HTTP rules, you need to integrate them into your API projects. This guide covers loading rules, configuration, and common usage patterns.

There are two ways to load rules into Thymian:

Create a thymian.config.yaml file in your project root:

ruleSets:
- './rules/**/*.rule.js' # Local rules
- '@thymian/rules-rfc-9110' # Npm package
plugins:
'@thymian/plugin-http-linter': {}
Terminal window
npx thymian lint --rule-set @thymian/rules-rfc-9110

Organize your rule set as an npm package:

my-api-rules/
├── package.json
├── src/
│ ├── index.ts
│ └── rules/
│ ├── authentication/
│ │ └── require-bearer-token.rule.ts
│ ├── headers/
│ │ └── require-correlation-id.rule.ts
│ └── versioning/
│ └── check-api-version.rule.ts
└── tsconfig.json

package.json:

{
"name": "@mycompany/api-rules",
"version": "1.0.0",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": ["dist"],
"peerDependencies": {
"@thymian/plugin-http-linter": "^0.0.1",
"@thymian/core": "^0.0.1"
}
}

src/index.ts:

import type { RuleSet } from '@thymian/core';
const myCompanyRules: RuleSet = {
name: '@mycompany/api-rules',
url: 'https://api-guidelines.mycompany.com',
pattern: 'rules/**/*.rule.js', // Glob pattern for built rules
};
export default myCompanyRules;

After publishing your rule set to npm, install it in your project:

Terminal window
npm install @mycompany/api-rules
ruleSets:
- '@mycompany/api-rules'

See which rules are loaded:

Terminal window
thymian rules list

Or with specific rule sources:

Terminal window
thymian rules list --rule-set ./rules/**/*.rule.ts

Problem: Rules aren’t being loaded

Solutions:

  1. Check the glob pattern matches your files:
Terminal window
ls -la ./rules/**/*.rule.ts
  1. Verify the rule exports default:
export default httpRule('...'); // ✅ Correct
export const myRule = httpRule('...'); // ❌ Wrong
  1. Check for TypeScript compilation errors:
Terminal window
tsc --noEmit