@jerome1337/fastify-enforce-routes-pattern

A Fastify plugin that enforces naming pattern for route paths


Keywords
fastify, fastify-plugin, snake_case, kebab-case, camelCase, PascalCase, routes, naming-pattern
License
MIT
Install
npm install @jerome1337/fastify-enforce-routes-pattern@1.0.0

Documentation

@jerome1337/fastify-enforce-routes-pattern

codecov

A Fastify plugin that enforces naming pattern for route paths.

Installation

npm install @jerome1337/fastify-enforce-routes-pattern

Usage

import fastify from 'fastify';
import enforceRoutesPattern from '@jerome1337/fastify-enforce-routes-pattern';

const app = fastify();

// Register with default options
app.register(enforceRoutesPattern);

// These routes will pass validation
app.get('/user-profile', () => {});
app.get('/user-profile/:id/payment-history', () => {});
app.post('/api/v1/create-user', () => {});

// These routes will throw an error
app.get('/userProfile', () => {}); // ❌ camelCase not allowed
app.get('/UserProfile', () => {}); // ❌ PascalCase not allowed
app.get('/user-profile', () => {}); // ❌ kebab-case not allowed

Options

type EnforceRoutesPatternOptions = {
  pattern?: string; // default: 'kebab-case'
  // Whether to throw an error on invalid routes or just log a warning to enforce
  strict?: boolean; // default: true
  // Custom patterns to ignore (e.g., health checks, metrics endpoints)
  ignoredPatterns?: string[]; // default: []
};

Example with options

app.register(enforceRoutesPattern, {
  pattern: 'snake_case', // Will enforce snake_case route names pattern
  strict: false, // Will only log warnings instead of throwing errors
  ignoredPatterns: [
    '^/health$', // Ignore health check endpoint
    '^/metrics', // Ignore metrics endpoints
  ],
});

Contributing

Pull requests are welcome! Please make sure to update tests as appropriate.

License

MIT