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.
Loading Rules
Section titled “Loading Rules”There are two ways to load rules into Thymian:
1. Via Configuration File
Section titled “1. Via Configuration File”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': {}2. Via CLI flags
Section titled “2. Via CLI flags”npx thymian lint --rule-set @thymian/rules-rfc-9110Creating Your Own Rule Set
Section titled “Creating Your Own Rule Set”File Structure
Section titled “File Structure”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.jsonPackage Definition
Section titled “Package Definition”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" }}Rule Set Entry Point
Section titled “Rule Set Entry Point”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;Using Your Rule Set
Section titled “Using Your Rule Set”After publishing your rule set to npm, install it in your project:
npm install @mycompany/api-rulesruleSets: - '@mycompany/api-rules'List Loaded Rules
Section titled “List Loaded Rules”See which rules are loaded:
thymian rules listOr with specific rule sources:
thymian rules list --rule-set ./rules/**/*.rule.tsTroubleshooting
Section titled “Troubleshooting”Rules Not Loading
Section titled “Rules Not Loading”Problem: Rules aren’t being loaded
Solutions:
- Check the glob pattern matches your files:
ls -la ./rules/**/*.rule.ts- Verify the rule exports default:
export default httpRule('...'); // ✅ Correctexport const myRule = httpRule('...'); // ❌ Wrong- Check for TypeScript compilation errors:
tsc --noEmitNext Steps
Section titled “Next Steps”- Explore the CLI commands for managing rules
- Learn about creating custom rules
- See combining rule types for hybrid validation