The case validation system provides types for enforcing consistent text case formatting across different parts of commit messages. This ensures standardized formatting for commit types, scopes, subjects, and other message components.
type TargetCaseType =
| "camel-case"
| "kebab-case"
| "snake-case"
| "pascal-case"
| "start-case"
| "upper-case"
| "uppercase"
| "sentence-case"
| "sentencecase"
| "lower-case"
| "lowercase"
| "lowerCase";Union type defining all supported case formats for text validation:
camelCase - First word lowercase, subsequent words capitalizedkebab-case - Words separated by hyphens, all lowercasesnake_case - Words separated by underscores, all lowercasePascalCase - All words capitalized, no separatorsStart Case - All words capitalized with spacesUPPER CASE - All characters uppercase with spacesUPPERCASE - All characters uppercase, no spacesSentence case - First letter capitalized, rest lowercasesentencecase - Alternative form, all lowercaselower case - All characters lowercase with spaceslowercase - All characters lowercase, no spaceslowerCase - Camel case starting with lowercaseCase validation is primarily used in rule configurations that validate text formatting:
type CaseRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
V,
TargetCaseType | TargetCaseType[]
>;Rule configuration type that accepts either a single case type or an array of acceptable case types.
import { TargetCaseType, RuleConfigSeverity } from "@commitlint/types";
// Enforce lowercase for commit types
const typeRule = [
RuleConfigSeverity.Error,
"always",
"lower-case" as TargetCaseType
];
// Enforce kebab-case for scopes
const scopeRule = [
RuleConfigSeverity.Error,
"always",
"kebab-case" as TargetCaseType
];
// Enforce sentence case for subjects
const subjectRule = [
RuleConfigSeverity.Warning,
"always",
"sentence-case" as TargetCaseType
];import { TargetCaseType, RulesConfig } from "@commitlint/types";
const rules: Partial<RulesConfig> = {
// Allow either lowercase or upper-case for types
"type-case": [2, "always", ["lowercase", "upper-case"] as TargetCaseType[]],
// Allow camel-case or kebab-case for scopes
"scope-case": [2, "always", ["camel-case", "kebab-case"] as TargetCaseType[]],
// Allow sentence-case or lower-case for subjects
"subject-case": [1, "always", ["sentence-case", "lower-case"] as TargetCaseType[]],
// Enforce snake_case for body text
"body-case": [1, "always", "snake-case" as TargetCaseType]
};import { TargetCaseType, RuleConfigSeverity } from "@commitlint/types";
// Configuration for different parts of commit message
const commitCaseRules = {
// Type: must be lowercase (feat, fix, docs)
type: {
case: "lowercase" as TargetCaseType,
severity: RuleConfigSeverity.Error
},
// Scope: can be camelCase or kebab-case
scope: {
case: ["camel-case", "kebab-case"] as TargetCaseType[],
severity: RuleConfigSeverity.Error
},
// Subject: sentence case (first letter capitalized)
subject: {
case: "sentence-case" as TargetCaseType,
severity: RuleConfigSeverity.Warning
},
// Body: any case allowed (no restriction)
body: {
case: undefined, // No case restriction
severity: RuleConfigSeverity.Disabled
}
};Understanding what each case type looks like:
import { TargetCaseType } from "@commitlint/types";
const examples: Record<TargetCaseType, string> = {
"camel-case": "camelCaseExample",
"kebab-case": "kebab-case-example",
"snake-case": "snake_case_example",
"pascal-case": "PascalCaseExample",
"start-case": "Start Case Example",
"upper-case": "UPPER CASE EXAMPLE",
"uppercase": "UPPERCASEEXAMPLE",
"sentence-case": "Sentence case example",
"sentencecase": "sentencecaseexample",
"lower-case": "lower case example",
"lowercase": "lowercaseexample",
"lowerCase": "lowerCaseExample"
};
// Function to validate case format
function validateCase(text: string, expectedCase: TargetCaseType): boolean {
const expected = examples[expectedCase];
// Implementation would transform input to expected case and compare
return transformToCase(text, expectedCase) === text;
}import { TargetCaseType, RulesConfig, RuleConfigSeverity } from "@commitlint/types";
// Advanced case configuration for monorepo
const monorepoRules: Partial<RulesConfig> = {
// Types must be lowercase
"type-case": [RuleConfigSeverity.Error, "always", "lowercase"],
// Scopes can be kebab-case for packages or camelCase for modules
"scope-case": [
RuleConfigSeverity.Error,
"always",
["kebab-case", "camel-case"] as TargetCaseType[]
],
// Subjects should start with lowercase for consistency
"subject-case": [
RuleConfigSeverity.Warning,
"always",
["lowercase", "sentence-case"] as TargetCaseType[]
],
// Headers (type + scope + subject) follow sentence case
"header-case": [RuleConfigSeverity.Warning, "always", "sentence-case"]
};import { TargetCaseType } from "@commitlint/types";
// Utility to check if text matches case requirement
function matchesCase(text: string, caseType: TargetCaseType): boolean {
switch (caseType) {
case "lowercase":
case "lower-case":
return text === text.toLowerCase();
case "uppercase":
case "upper-case":
return text === text.toUpperCase();
case "camel-case":
return /^[a-z][a-zA-Z0-9]*$/.test(text);
case "kebab-case":
return /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(text);
case "snake-case":
return /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/.test(text);
case "pascal-case":
return /^[A-Z][a-zA-Z0-9]*$/.test(text);
case "sentence-case":
return /^[A-Z][a-z0-9\s]*$/.test(text);
default:
return true; // Unknown case types pass validation
}
}
// Utility to validate against multiple allowed cases
function matchesAnyCases(text: string, cases: TargetCaseType[]): boolean {
return cases.some(caseType => matchesCase(text, caseType));
}import { TargetCaseType, CaseRuleConfig, RuleConfigSeverity } from "@commitlint/types";
// Function to create case rule configurations
function createCaseRule(
cases: TargetCaseType | TargetCaseType[],
severity: RuleConfigSeverity = RuleConfigSeverity.Error
): CaseRuleConfig {
return [severity, "always", cases];
}
// Apply to common commit message components
const caseRules = {
typeCase: createCaseRule("lowercase", RuleConfigSeverity.Error),
scopeCase: createCaseRule(["kebab-case", "camel-case"], RuleConfigSeverity.Error),
subjectCase: createCaseRule("sentence-case", RuleConfigSeverity.Warning),
bodyCase: createCaseRule(["sentence-case", "lower-case"], RuleConfigSeverity.Warning)
};