{"success":true,"name":"normalize-identifiers-before-storing-or-comparing-trim-lower","description":"Saving or looking up emails, usernames, codes, or phone numbers","category":"ai-brimind","path":"ai-brimind/normalize-identifiers-before-storing-or-comparing-trim-lower/SKILL.md","file":"SKILL.md","content":"---\nname: normalize-identifiers-before-storing-or-comparing-trim-lower\ndescription: \"Saving or looking up emails, usernames, codes, or phone numbers\"\n---\n\n# Normalize Identifiers Before Storing or Comparing: Trim, Lowercase, One Canonical Form\n\n## When to use\nYou are writing code that stores or matches identifier-like strings — emails, usernames, coupon codes, phone numbers, slugs — and the raw user-typed value goes into the DB or the query as-is, without an agreed canonical form.\n\n## Example request\nUser signs up as '[EMAIL] ' (capital J, trailing space) and later logs in typing '[EMAIL]' → no account found, or worse, a duplicate account gets created; emails needed lowercasing and trimming ONCE at the border. Coupon code saved as 'SUMMER10' but users type 'summer10' → valid coupons rejected at checkout; pick one canonical case for codes and apply it on both store and lookup. A unique index on email exists, yet '[EMAIL]' and '[EMAIL]' both got in → the DB compared them as different strings; uniqueness only protects the CANONICAL form, so normalization must happen before the insert. Phone numbers stored as '+[PHONE]', '[PHONE]', '0022...' for the same person → matching and deduplication become impossible; one storage format (digits + country code) is the fix, formatting is for display.\n\n## Instructions\nAct as a developer who knows that to a database, '[EMAIL]' and '[EMAIL] ' are two total strangers — string comparison is blind, exact, and merciless, so any identifier humans type will arrive in five spellings unless YOU collapse them into one. For every identifier field you store or match, do this: (1) Decide the canonical form and write it down where the field is defined: emails → trimmed and lowercased; usernames and slugs → pick a case policy and allowed characters deliberately; codes (coupons, invites) → one case, no surrounding whitespace; phone numbers → digits in one international format, with pretty formatting applied only at display time. (2) Apply the normalization at ONE border — the moment the value enters the system (the validation layer of the endpoint, or the schema level: Mongoose's lowercase/trim options on the field do this declaratively, which beats remembering it in every route) — so the DB only ever sees the canonical form. Normalizing 'sometimes, in some routes' is how the duplicates sneak in. (3) Apply the SAME normalization on every lookup and comparison: login, 'does this email exist', coupon checks — the query value gets the identical trim/lowercase treatment, ideally through the same shared function, so store-side and search-side can never drift apart. (4) Only then does a unique index mean what you think: add it on the field to enforce uniqueness at the DB level, and remember it guards the canonical form — normalization plus the index together close the duplicate-account door; either alone leaves it ajar. (5) If real data already contains mixed forms, that is a data migration to canonical form BEFORE tightening the rules — check for collisions ('[EMAIL]' vs '[EMAIL]' may be two real accounts to merge or flag) instead of letting the new unique index explode on old data. Only mark the field done once you can state its canonical form, point at the single place that enforces it on write AND on read, and explain why a user typing their email with creative capitalization tomorrow still lands on the same account.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #215\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}