# Create the validation directory
mkdir -p scripts/validation
# Copy all validation scripts
cp scripts/validation/*.js scripts/validation/
# Copy the git hooks configuration
cp lefthook.yml ./# Install globally (recommended)
npm install -g lefthook
# Or with yarn
yarn global add lefthook
# Or with pnpm
pnpm add -g lefthook# Install lefthook in your repository
lefthook install# Make a test commit to see the guardrails in action
git add .
git commit -m "test: testing guardrails setup"The guardrails will automatically check:
- ✅ Type safety (no unsafe
ascasts) - ✅ Import patterns (no barrel files, proper aliases)
- ✅ File complexity (reasonable file sizes)
- ✅ Documentation (JSDoc on functions)
- ✅ Directory structure (clean architecture)
- ✅ Security (no hardcoded secrets)
The commit will be blocked with helpful error messages:
❌ COMMIT BLOCKED: Found 'as' type cast violations!
📁 src/components/UserProfile.tsx:
Line 15: as any
const user = response.data as any;
💡 Replace 'as' casts with proper type guards or type predicates
Edit scripts/validation/check-file-complexity.js:
const COMPLEXITY_LIMITS = {
lines: 300, // Reduce from 500 if you prefer smaller files
functions: 10, // Reduce from 15 if you prefer fewer functions per file
dependencies: 15, // Reduce from 20 if you prefer fewer imports
}Edit scripts/validation/check-as-casts.js:
const ALLOWED_PATTERNS = [
/\bas\s+const\b/g,
/\.test\.(ts|tsx)$/,
/\.spec\.(ts|tsx)$/,
// Add your own patterns here
/your-special-file\.ts$/,
]# Make sure lefthook is installed globally
npm install -g lefthook
# Or use npx
npx lefthook install# Temporarily bypass checks during development
NODE_ENV=development git commit -m "WIP: fixing violations"Add specific patterns to the validation scripts or use environment variables:
# Skip specific checks
SKIP_COMPLEXITY_CHECK=true git commit -m "feature: new component"- Read the documentation: Check out
WHY-GUARDRAILS.mdandEXAMPLES.md - Customize gradually: Start with the defaults, adjust as needed
- Train your team: Share the guardrails with your team
- Monitor and adjust: Review the guardrails periodically
- Start with all guardrails enabled
- Adjust limits based on your team's preferences
- Document any exceptions you make
- Start with higher limits and gradually reduce them
- Fix violations incrementally
- Use
NODE_ENV=developmentto bypass checks during major refactoring
- Discuss guardrails in team meetings
- Make sure everyone understands the benefits
- Review and adjust guardrails as the team grows
That's it! Your project now has AI guardrails that will help maintain code quality automatically. The guardrails will catch issues early and help your team write better code, whether it's generated by AI or written by humans.