{"success":true,"name":"rules-enforced-in-two-layers-must-share-one-definition-front","description":"The same rule (max length, who can edit, price logic) exists in frontend AND backend","category":"ai-brimind","path":"ai-brimind/rules-enforced-in-two-layers-must-share-one-definition-front/SKILL.md","file":"SKILL.md","content":"---\nname: rules-enforced-in-two-layers-must-share-one-definition-front\ndescription: \"The same rule (max length, who can edit, price logic) exists in frontend AND backend\"\n---\n\n# Rules Enforced in Two Layers Must Share One Definition: Frontend Mirrors, Backend Decides\n\n## When to use\nA business rule is implemented twice — once client-side for UX, once server-side as the law — and the two copies are hand-written separately, free to drift apart.\n\n## Example request\nFrontend allows 100-character names, backend validation caps at 50 → the form accepts, the server rejects, the user is punished by a rule they were told they passed; accept-then-reject is the signature of drifted twin rules. An 'Edit' button shown by frontend role logic while the backend middleware checks a slightly different condition → users see buttons that always fail, or worse, the backend copy is looser; the two copies answer the same question differently. In a Next.js monorepo the rule can literally be ONE exported function or schema (shared validation constants, a canEditMember(user, member) helper) imported by both the form and the endpoint → drift becomes impossible instead of undetected; your shared-vocabulary discipline, upgraded from words to rules. A rule only the server can evaluate (needs secret data) → the frontend shows optimistic UI and handles rejection gracefully; mirroring has honest limits, and knowing them is part of the pattern.\n\n## Instructions\nAct as a developer who knows that any rule living in two layers is one rushed edit away from becoming two rules — and the user is the one who discovers the divergence, by being told 'valid!' by the form and 'invalid!' by the server, or by clicking a button the backend was always going to refuse; twin rules drift because they are typed twice, so the fix is structural: type them once. For every rule enforced on both sides, do this: (1) Restate the hierarchy first, out loud, per your backend-is-law discipline: the server copy is the LAW (security, integrity), the client copy is a COURTESY (instant feedback, hidden buttons) — which means drift where the client is stricter annoys users, and drift where the client is looser exposes them to accept-then-reject; neither is acceptable, but never 'fix' drift by weakening the server. (2) Extract the rule to one shared definition wherever your stack allows it — and Next.js monorepos almost always allow it: validation limits and shapes as one shared schema or constants object (maxNameLength: 50 imported by BOTH the form's maxLength and the border validation), permission logic as one pure function (canCancelBooking(user, booking) — pure, no req/res, per your services discipline — imported by the component that shows the button AND the middleware that guards the endpoint). One definition, two importers, zero drift: your shared-vocabulary discipline applied to logic, not just words. (3) Where true sharing is impossible (the rule needs server-only data or secrets), mirror the CONTRACT instead of the logic: the frontend implements a best-effort version, and — the critical part — always handles the server's rejection gracefully per your form-errors discipline, because the mirror is allowed to be wrong and the UX must survive it. Mark such mirrors with a comment naming the authoritative server location, so editors know there are two sites. (4) When the rule changes, the shared definition changes — and if a mirror exists, your blast-radius habit adds it to the checklist; a rule change that touches one layer of a two-layer rule is a drift being manufactured. (5) Verify with the disagreement test: submit input that sits exactly at the boundary (50 chars, 51 chars; the permission edge case) through the UI — form verdict and server verdict must match on both sides of the line. Only call the rule implemented when there is either one shared definition or one law plus a graceful, labeled mirror — two independently maintained copies of a rule is not defense in depth, it is a scheduled disagreement.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #251\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}