feat: add JS-009 rule for Utils.showToast() and update naming docs

Architecture rules:
- Add JS-009: Use Utils.showToast() instead of alert() or window.showToast
- Supports inline noqa comments to suppress warnings

Documentation:
- Update naming-conventions.md to emphasize plural table names (industry standard)
- Document that plural table names follow Rails/Django/Laravel conventions

Schema:
- Add from_attributes to VendorUserResponse for ORM compatibility

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-12 22:37:28 +01:00
parent 4e8ea8151c
commit 2b899d5a52
4 changed files with 1261 additions and 41 deletions

View File

@@ -348,13 +348,23 @@ model_rules:
- "from_attributes = True"
- id: "MDL-004"
name: "Database models use singular table names"
name: "Database tables use plural names"
severity: "warning"
description: |
Database table names should be singular lowercase (e.g., 'vendor' not 'vendors').
Database table names should be plural lowercase following industry standard
conventions (Rails, Django, Laravel, most ORMs). A table represents a
collection of entities, so plural names are natural: 'users', 'orders',
'products'. This reads naturally in SQL: SELECT * FROM users WHERE id = 1.
Examples:
- Good: users, vendors, products, orders, order_items, cart_items
- Bad: user, vendor, product, order
Junction/join tables use both entity names in plural:
- Good: vendor_users, order_items, product_translations
pattern:
file_pattern: "models/database/**/*.py"
check: "table_naming"
check: "table_naming_plural"
# ============================================================================
# EXCEPTION HANDLING RULES
@@ -573,6 +583,65 @@ javascript_rules:
loading = false;
}
- id: "JS-008"
name: "Use apiClient for API calls, not raw fetch()"
severity: "error"
description: |
All API calls must use the apiClient helper instead of raw fetch().
The apiClient automatically:
- Adds Authorization header with JWT token from cookies
- Sets Content-Type headers
- Handles error responses consistently
- Provides logging integration
WRONG (raw fetch):
const response = await fetch('/api/v1/admin/products/123');
RIGHT (apiClient):
const response = await apiClient.get('/admin/products/123');
const result = await apiClient.post('/admin/products', data);
await apiClient.delete('/admin/products/123');
pattern:
file_pattern: "static/**/js/**/*.js"
anti_patterns:
- "fetch\\('/api/"
- 'fetch\\("/api/'
- "fetch\\(`/api/"
exceptions:
- "init-api-client.js" # The apiClient implementation itself
- id: "JS-009"
name: "Use Utils.showToast() for notifications, not alert() or window.showToast"
severity: "error"
description: |
All user notifications must use Utils.showToast() from static/shared/js/utils.js.
Never use browser alert() dialogs or undefined window.showToast.
Utils.showToast() provides:
- Consistent styling (Tailwind-based toast in bottom-right corner)
- Automatic fade-out after duration
- Color-coded types (success=green, error=red, warning=yellow, info=blue)
WRONG (browser dialog):
alert('Product saved successfully');
alert(errorMessage);
WRONG (undefined function):
window.showToast('Success', 'success');
if (window.showToast) { window.showToast(...); } else { alert(...); }
RIGHT (Utils helper):
Utils.showToast('Product saved successfully', 'success');
Utils.showToast('Failed to save product', 'error');
Utils.showToast('Please fill all required fields', 'warning');
pattern:
file_pattern: "static/**/js/**/*.js"
anti_patterns:
- "alert\\("
- "window\\.showToast"
exceptions:
- "utils.js" # The Utils implementation itself
# ============================================================================
# TEMPLATE RULES (Jinja2)
# ============================================================================

View File

@@ -285,17 +285,42 @@ frontend/
### Database Naming
**Table Names**: Use singular, lowercase with underscores
**Table Names**: Use plural, lowercase with underscores (industry standard)
Following Rails, Django, Laravel conventions - tables represent collections of entities,
so plural names are natural and read well in SQL queries.
```sql
-- ✅ Correct
inventory
inventory_movements
-- ✅ Correct (plural table names)
users
vendors
products
orders
customers
order_items
cart_items
vendor_users
-- ❌ Incorrect
inventories
inventory_movement
vendorusers
-- ❌ Incorrect (singular table names)
user
vendor
product
order
customer
```
**Rationale**:
- A table holds a *collection* of records (many users, many products)
- SQL reads naturally: `SELECT * FROM users WHERE id = 1`
- Follows conventions of most ORMs (ActiveRecord, Django ORM, Laravel Eloquent)
- Used by ~80% of modern web frameworks
**Junction/Join Tables**: Combine both entity names in plural
```sql
-- ✅ Correct
vendor_users -- Links vendors and users
order_items -- Links orders and products
product_translations -- Translations for products
```
**Column Names**: Use singular, descriptive names
@@ -410,7 +435,7 @@ class Customer:
- [ ] File names follow singular/plural conventions
- [ ] Class names use appropriate terminology (inventory vs stock)
- [ ] API endpoints use plural resource names
- [ ] Database models use singular names
- [ ] Database table names use plural (industry standard)
- [ ] Variables names match their content (singular vs plural)
### Automated Checks
@@ -428,13 +453,13 @@ Consider implementing linting rules or pre-commit hooks to enforce:
| Component | Naming | Example |
|-----------|--------|---------|
| API Endpoint Files | PLURAL | `products.py`, `orders.py` |
| Database Models | SINGULAR | `product.py`, `order.py` |
| Database Models (files) | SINGULAR | `product.py`, `order.py` |
| Schema/Pydantic | SINGULAR | `product.py`, `order.py` |
| Services | SINGULAR + service | `product_service.py` |
| Exceptions | SINGULAR | `product.py`, `order.py` |
| Middleware | SIMPLE NOUN | `auth.py`, `logging.py`, `context.py` |
| Middleware Tests | test_{name}.py | `test_auth.py`, `test_logging.py` |
| Database Tables | SINGULAR | `product`, `inventory` |
| **Database Tables** | **PLURAL** | `users`, `products`, `orders` |
| Database Columns | SINGULAR | `vendor_id`, `created_at` |
| API Endpoints | PLURAL | `/products`, `/orders` |
| Functions (single) | SINGULAR | `create_product()` |

View File

@@ -153,3 +153,5 @@ class VendorUserResponse(BaseModel):
email: str
role: str
is_active: bool
model_config = {"from_attributes": True}

File diff suppressed because it is too large Load Diff