This file contains the full reference documentation (adapters, migrations, CLI, API, and framework examples) moved from the top-level README for easier onboarding.
For the fast-start overview, see ../README.md.
Live Demo - try it in your browser | Blog Post - why this exists
The world's first library that detects confusable characters across non-Latin scripts. Slug claimability, Unicode anti-spoofing, and LLM Denial of Spend defence in one zero-dependency package.
For multi-tenant apps where usernames, organisation slugs, and reserved routes share one URL namespace, and for LLM pipelines where confusable substitution inflates token costs up to 5.2x.
You have a URL structure like yourapp.com/:slug that could be:
- A user profile (
/sarah) - An organization (
/acme-corp) - A reserved route (
/settings,/admin,/api)
When someone signs up or creates an org, you need to check that their chosen slug:
- Isn't already taken by another user
- Isn't already taken by an organization
- Isn't a reserved system route
- Follows your naming rules
- Isn't confusable with protected names (anti-impersonation)
This library handles all of that in one guard call.
npm install namespace-guardimport { createNamespaceGuardWithProfile } from "namespace-guard";
import { createPrismaAdapter } from "namespace-guard/adapters/prisma";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
// One-liner-ready guard with practical defaults
const guard = createNamespaceGuardWithProfile(
"consumer-handle",
{
reserved: ["admin", "api", "settings", "dashboard", "login", "signup"],
sources: [
{ name: "user", column: "handle", scopeKey: "id" },
{ name: "organization", column: "slug", scopeKey: "id" },
],
},
createPrismaAdapter(prisma)
);
// One-liner: format/reserved/taken + anti-spoofing policy
await guard.assertClaimable("acme-corp");
// throws on failure, otherwise safe to create| Feature | namespace-guard | DIY Solution |
|---|---|---|
| Multi-table uniqueness | One call | Multiple queries |
| Reserved name blocking | Built-in with categories | Manual list checking |
| Ownership scoping | No false positives on self-update | Easy to forget |
| Format validation | Configurable regex | Scattered validation |
| Conflict suggestions | Auto-suggest alternatives | Not built |
| Async validators | Custom hooks (profanity, etc.) | Manual wiring |
| Batch checking | checkMany() |
Loop it yourself |
| ORM agnostic | Prisma, Drizzle, Kysely, Knex, TypeORM, MikroORM, Sequelize, Mongoose, raw SQL | Tied to your ORM |
| CLI | check, risk, attack-gen, audit-canonical, calibrate, recommend, drift |
None |
import { PrismaClient } from "@prisma/client";
import { createPrismaAdapter } from "namespace-guard/adapters/prisma";
const prisma = new PrismaClient();
const adapter = createPrismaAdapter(prisma);Note: The Drizzle adapter uses
db.query(the relational query API). Make sure your Drizzle client is set up withdrizzle(client, { schema })so thatdb.query.<tableName>is available.
import { eq } from "drizzle-orm";
import { createDrizzleAdapter } from "namespace-guard/adapters/drizzle";
import { db } from "./db";
import { users, organizations } from "./schema";
// Pass eq directly, or use { eq, ilike } for case-insensitive support
const adapter = createDrizzleAdapter(db, { users, organizations }, eq);import { Kysely, PostgresDialect } from "kysely";
import { createKyselyAdapter } from "namespace-guard/adapters/kysely";
const db = new Kysely<Database>({ dialect: new PostgresDialect({ pool }) });
const adapter = createKyselyAdapter(db);import Knex from "knex";
import { createKnexAdapter } from "namespace-guard/adapters/knex";
const knex = Knex({ client: "pg", connection: process.env.DATABASE_URL });
const adapter = createKnexAdapter(knex);import { DataSource } from "typeorm";
import { createTypeORMAdapter } from "namespace-guard/adapters/typeorm";
import { User, Organization } from "./entities";
const dataSource = new DataSource({ /* ... */ });
const adapter = createTypeORMAdapter(dataSource, { user: User, organization: Organization });import { MikroORM } from "@mikro-orm/core";
import { createMikroORMAdapter } from "namespace-guard/adapters/mikro-orm";
import { User, Organization } from "./entities";
const orm = await MikroORM.init(config);
const adapter = createMikroORMAdapter(orm.em, { user: User, organization: Organization });import { createSequelizeAdapter } from "namespace-guard/adapters/sequelize";
import { User, Organization } from "./models";
const adapter = createSequelizeAdapter({ user: User, organization: Organization });import { createMongooseAdapter } from "namespace-guard/adapters/mongoose";
import { User, Organization } from "./models";
// Note: Mongoose sources typically use idColumn: "_id"
const adapter = createMongooseAdapter({ user: User, organization: Organization });The raw adapter generates PostgreSQL-style SQL ($1 placeholders, double-quoted identifiers). For pg this works directly. For MySQL or SQLite, translate the parameter syntax in your executor wrapper.
import { Pool } from "pg";
import { createRawAdapter } from "namespace-guard/adapters/raw";
const pool = new Pool();
const adapter = createRawAdapter((sql, params) => pool.query(sql, params));MySQL2 wrapper (translates $1 to ? and "col" to `col`):
import mysql from "mysql2/promise";
import { createRawAdapter } from "namespace-guard/adapters/raw";
const pool = mysql.createPool({ uri: process.env.DATABASE_URL });
const adapter = createRawAdapter(async (sql, params) => {
const mysqlSql = sql.replace(/\$\d+/g, "?").replace(/"/g, "`");
const [rows] = await pool.execute(mysqlSql, params);
return { rows: rows as Record<string, unknown>[] };
});better-sqlite3 wrapper (translates $1 to ? and strips identifier quotes):
import Database from "better-sqlite3";
import { createRawAdapter } from "namespace-guard/adapters/raw";
const db = new Database("app.db");
const adapter = createRawAdapter(async (sql, params) => {
const sqliteSql = sql.replace(/\$\d+/g, "?").replace(/"/g, "");
const rows = db.prepare(sqliteSql).all(...params);
return { rows: rows as Record<string, unknown>[] };
});To fully close Unicode/canonicalization edge cases and race windows, enforce uniqueness on a canonical column in your database.
Use this rollout plan:
- Add a canonical column (nullable first).
- Dual-write canonical values on every create/update (
normalize(raw)). - Backfill existing rows in batches.
- Resolve duplicates found after backfill.
- Add a unique index/constraint and make canonical non-null.
- Point
namespace-guardsources[*].columnat the canonical column.
Example dual-write pattern:
import { normalize } from "namespace-guard";
const raw = input.handle;
const canonical = normalize(raw);
await guard.assertClaimable(raw);
await db.user.create({
data: {
handle: raw, // display value
handleCanonical: canonical, // canonical key
},
});After cutover, configure sources to check canonical columns:
sources: [{ name: "user", column: "handleCanonical", scopeKey: "id" }]schema.prisma:
model User {
id String @id @default(cuid())
handle String
handleCanonical String? @unique
}Create/apply migration:
npx prisma migrate dev --name add_handle_canonicalThen backfill via Prisma client in batches, resolve duplicates, and finally make handleCanonical required.
schema.ts (Postgres example):
import { pgTable, text, uniqueIndex } from "drizzle-orm/pg-core";
export const users = pgTable(
"users",
{
id: text("id").primaryKey(),
handle: text("handle").notNull(),
handleCanonical: text("handle_canonical"),
},
(table) => [uniqueIndex("users_handle_canonical_uidx").on(table.handleCanonical)]
);Generate/apply migration:
npx drizzle-kit generate --name add-handle-canonical
npx drizzle-kit migrateMigration (up):
import { Kysely } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable("users")
.addColumn("handle_canonical", "varchar(255)")
.execute();
await db.schema
.createIndex("users_handle_canonical_uidx")
.on("users")
.column("handle_canonical")
.unique()
.execute();
}Backfill in app code, dedupe, then add NOT NULL in a follow-up migration.
Migration:
export async function up(knex) {
await knex.schema.alterTable("users", (table) => {
table.string("handle_canonical", 255);
});
await knex.schema.alterTable("users", (table) => {
table.unique(["handle_canonical"], "users_handle_canonical_uidx");
});
}Entity:
import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm";
@Entity("user")
export class User {
@PrimaryGeneratedColumn("uuid")
id!: string;
@Column({ type: "varchar", length: 255 })
handle!: string;
@Index("IDX_user_handle_canonical_unique", { unique: true })
@Column({ name: "handle_canonical", type: "varchar", length: 255, nullable: true })
handleCanonical!: string | null;
}Migration (up):
import { MigrationInterface, QueryRunner, TableColumn, TableIndex } from "typeorm";
export class AddHandleCanonical1730000000000 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.addColumn(
"user",
new TableColumn({
name: "handle_canonical",
type: "varchar",
length: "255",
isNullable: true,
})
);
await queryRunner.createIndex(
"user",
new TableIndex({
name: "IDX_user_handle_canonical_unique",
columnNames: ["handle_canonical"],
isUnique: true,
})
);
}
}Entity:
import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
@Entity()
export class User {
@PrimaryKey()
id!: string;
@Property()
handle!: string;
@Property({
fieldName: "handle_canonical",
length: 255,
nullable: true,
unique: true,
})
handleCanonical?: string;
}Generate/apply migration:
npx mikro-orm migration:create
npx mikro-orm migration:upMigration:
export async function up(queryInterface, Sequelize) {
await queryInterface.addColumn("Users", "handleCanonical", {
type: Sequelize.DataTypes.STRING(255),
allowNull: true,
});
await queryInterface.addIndex("Users", ["handleCanonical"], {
name: "users_handle_canonical_uidx",
unique: true,
});
}If you use model definitions directly, keep handleCanonical persisted and indexed there too.
Schema:
import { Schema, model } from "mongoose";
const userSchema = new Schema({
handle: { type: String, required: true },
handleCanonical: { type: String, required: true, unique: true, index: true },
});
export const User = model("User", userSchema);Build indexes after deploying the schema change:
await User.init();ALTER TABLE users ADD COLUMN handle_canonical text;
-- backfill from application code using namespace-guard normalize()
CREATE UNIQUE INDEX users_handle_canonical_uidx ON users (handle_canonical);
ALTER TABLE users ALTER COLUMN handle_canonical SET NOT NULL;Before adding UNIQUE/NOT NULL, run a duplicate report:
SELECT handle_canonical, COUNT(*)
FROM users
GROUP BY handle_canonical
HAVING COUNT(*) > 1;Resolve these rows first, then enforce the constraint.
- Prisma schema unique fields: https://www.prisma.io/docs/orm/reference/prisma-schema-reference#unique
- Prisma migrations (
migrate dev): https://www.prisma.io/docs/concepts/components/prisma-migrate/migrate-development-production - Drizzle column/index uniqueness and migrations: https://orm.drizzle.team/docs/indexes-constraints and https://orm.drizzle.team/docs/drizzle-kit-migrate
- Kysely migrations and index builder: https://www.kysely.dev/docs/migrations and https://kysely-org.github.io/kysely-apidoc/classes/CreateIndexBuilder.html
- Knex
table.unique(...): https://knexjs.org/guide/schema-builder.html#unique - TypeORM
@Column({ unique: true })/@Index({ unique: true }): https://typeorm.io/docs/entity/entities and https://typeorm.io/docs/help/decorator-reference/ - MikroORM
@Property({ unique: true })and migrations: https://mikro-orm.io/docs/decorators and https://mikro-orm.io/docs/migrations - Sequelize QueryInterface migrations (
addColumn,addIndex): https://sequelize.org/api/v6/class/src/dialects/abstract/query-interface.js~queryinterface - Mongoose indexes and
unique: https://mongoosejs.com/docs/guide.html#indexes and https://mongoosejs.com/docs/schematypes.html - PostgreSQL unique indexes: https://www.postgresql.org/docs/current/indexes-unique.html
const guard = createNamespaceGuard({
// Reserved names - flat list, Set, or categorized
reserved: new Set([
"admin",
"api",
"settings",
"dashboard",
"login",
"signup",
"help",
"support",
"billing",
]),
// Data sources to check for collisions
// Queries run in parallel for speed
sources: [
{
name: "user", // Prisma model / Drizzle table / SQL table name
column: "handle", // Column containing the slug/handle
idColumn: "id", // Primary key column (default: "id")
scopeKey: "id", // Key for ownership checks (see below)
},
{
name: "organization",
column: "slug",
scopeKey: "id",
},
{
name: "team",
column: "slug",
scopeKey: "id",
},
],
// Validation pattern (default: /^[a-z0-9][a-z0-9-]{1,29}$/)
// This default requires: 2-30 chars, lowercase alphanumeric + hyphens, can't start with hyphen
pattern: /^[a-z0-9][a-z0-9-]{2,39}$/,
// Custom error messages
messages: {
invalid: "Use 3-40 lowercase letters, numbers, or hyphens.",
reserved: "That name is reserved. Please choose another.",
taken: (sourceName) => `That name is already taken.`,
},
}, adapter);Group reserved names by category with different error messages:
const guard = createNamespaceGuard({
reserved: {
system: ["admin", "api", "settings", "dashboard"],
brand: ["oncor", "bandcamp"],
offensive: ["..."],
},
sources: [/* ... */],
messages: {
reserved: {
system: "That's a system route.",
brand: "That's a protected brand name.",
offensive: "That name is not allowed.",
},
},
}, adapter);
const result = await guard.check("admin");
// { available: false, reason: "reserved", category: "system", message: "That's a system route." }You can also use a single string message for all categories, or mix - categories without a specific message fall back to the default.
Add custom async checks that run after format/reserved validation but before database queries:
const guard = createNamespaceGuard({
sources: [/* ... */],
validators: [
async (identifier) => {
if (await isProfane(identifier)) {
return { available: false, message: "That name is not allowed." };
}
return null; // pass
},
async (identifier) => {
if (await isTrademarkViolation(identifier)) {
return { available: false, message: "That name is trademarked." };
}
return null;
},
],
}, adapter);Validators run sequentially and stop at the first rejection. They receive the normalized identifier.
namespace-guard stays zero-dependency and does not bundle external profanity/moderation libraries.
Use createPredicateValidator to wrap any third-party boolean check in one line:
import { createNamespaceGuard, createPredicateValidator } from "namespace-guard";
import { Profanity } from "@2toad/profanity";
const profanity = new Profanity();
const guard = createNamespaceGuard({
sources: [/* ... */],
validators: [
createPredicateValidator((identifier) => profanity.exists(identifier), {
message: "Please choose an appropriate name.",
transform: (identifier) => identifier.normalize("NFKC"),
}),
],
}, adapter);Common optional libraries people pair with namespace-guard:
obscenity(strong normalization/evasion-focused APIs)@2toad/profanity(simple boolean API)bad-words(basic list-based filter)
Use createProfanityValidator for a turnkey profanity filter - supply your own word list:
import { createNamespaceGuard, createProfanityValidator } from "namespace-guard";
const guard = createNamespaceGuard({
sources: [/* ... */],
validators: [
createProfanityValidator(["badword", "offensive", "slur"], {
message: "Please choose an appropriate name.", // optional custom message
checkSubstrings: true, // default: true
mode: "evasion", // default: "evasion"
variantProfile: "balanced", // default: "balanced"
minSubstringLength: 4, // default: 4 (exact matches always checked)
}),
],
}, adapter);mode: "evasion" is designed for profanity bypasses: it folds Unicode confusables plus common ASCII substitutes (for example sh1t, 5h1t, mixed-script variants, and separator insertion like s-h.i_t). Use mode: "basic" if you want strict lowercase matching only.
variantProfile: "balanced" is precision-first and avoids more ambiguous substitutions; switch to "aggressive" only when you need broader catch coverage.
If you want a ready-to-use default list without wiring your own dataset first:
import { createNamespaceGuard } from "namespace-guard";
import { createEnglishProfanityValidator } from "namespace-guard/profanity-en";
const guard = createNamespaceGuard({
sources: [/* ... */],
validators: [
createEnglishProfanityValidator({
mode: "evasion", // default
variantProfile: "balanced", // default
minSubstringLength: 4, // default
}),
],
}, adapter);namespace-guard/profanity-en also exports:
PROFANITY_WORDS_EN(curated word list)PROFANITY_WORDS_EN_COUNTPROFANITY_WORDS_EN_SOURCEPROFANITY_WORDS_EN_LICENSE
Core namespace-guard remains zero-dependency and does not force any external moderation package.
Prevent spoofing attacks where visually similar characters from any Unicode script are substituted for Latin letters (e.g., Cyrillic "а" for Latin "a" in "admin"). Note: with the default ASCII-only pattern ([a-z0-9-]), non-Latin characters are already rejected by the format check. The homoglyph validator is most useful when your pattern allows Unicode characters, or as defense-in-depth alongside rejectMixedScript:
import { createNamespaceGuard, createHomoglyphValidator } from "namespace-guard";
const guard = createNamespaceGuard({
sources: [/* ... */],
validators: [
createHomoglyphValidator(),
],
}, adapter);Options:
createHomoglyphValidator({
message: "Custom rejection message.", // optional
additionalMappings: { "\u0261": "g" }, // extend the built-in map
rejectMixedScript: true, // also reject Latin + non-Latin script mixing
})The built-in CONFUSABLE_MAP contains 613 character pairs generated from Unicode TR39 confusables.txt plus supplemental Latin small capitals. It covers Cyrillic, Greek, Armenian, Cherokee, IPA, Coptic, Lisu, Canadian Syllabics, Georgian, and 20+ other scripts. The map is exported for inspection or extension, and is regenerable for new Unicode versions with npx tsx scripts/generate-confusables.ts.
Reject zero-width/default-ignorable and bidi direction-control characters in the claim path.
Optionally reject combining marks for stricter anti-evasion policies.
import {
createNamespaceGuard,
createInvisibleCharacterValidator,
} from "namespace-guard";
const guard = createNamespaceGuard({
sources: [/* ... */],
validators: [createInvisibleCharacterValidator()],
}, adapter);Options:
createInvisibleCharacterValidator({
message: "That name contains hidden Unicode controls.",
rejectDefaultIgnorables: true, // default
rejectBidiControls: true, // default
rejectCombiningMarks: false, // default (set true for strict anti-evasion mode)
})rejectCombiningMarks is intentionally opt-in because many legitimate identifiers in Unicode-enabled products use combining marks naturally.
For standalone use without NFKC normalization, CONFUSABLE_MAP_FULL (~1,400 entries) includes every single-character-to-Latin mapping from TR39 with no NFKC filtering. This is the right map when your pipeline does not run NFKC before confusable detection, which is the case for most real-world systems: TR39's skeleton algorithm uses NFD, Chromium's IDN spoof checker uses NFD, Rust's confusable_idents lint runs on NFC, and django-registration applies the confusable map to raw input with no normalization at all.
import { CONFUSABLE_MAP_FULL } from "namespace-guard";
// Contains everything in CONFUSABLE_MAP, plus:
// - ~766 entries where NFKC agrees with TR39 (mathematical alphanumerics, fullwidth forms)
// - 31 entries where TR39 and NFKC disagree on the target letter
CONFUSABLE_MAP_FULL["\u017f"]; // "f" (Long S: TR39 visual mapping)
CONFUSABLE_MAP_FULL["\u{1D41A}"]; // "a" (Mathematical Bold Small A)The TR39 Section 4 skeleton algorithm computes a normalized form of a string for confusable comparison. Two strings that look alike will produce the same skeleton. This is the same algorithm used by ICU's SpoofChecker, Chromium's IDN spoof checker, and the Rust compiler's confusable_idents lint.
import { skeleton, areConfusable, CONFUSABLE_MAP } from "namespace-guard";
// Compute skeletons for comparison
skeleton("paypal"); // "paypal"
skeleton("\u0440\u0430ypal"); // "paypal" (Cyrillic р and а)
skeleton("pay\u200Bpal"); // "paypal" (zero-width space stripped)
skeleton("\u017f"); // "f" (Long S via TR39 visual mapping)
// Compare two strings directly
areConfusable("paypal", "\u0440\u0430ypal"); // true
areConfusable("google", "g\u043e\u043egle"); // true (Cyrillic о)
areConfusable("hello", "world"); // false
// Use CONFUSABLE_MAP for NFKC-first pipelines
skeleton("\u017f", { map: CONFUSABLE_MAP }); // "\u017f" (Long S not in filtered map)By default, skeleton() uses CONFUSABLE_MAP_FULL (the complete TR39 map), which matches the NFD-based pipeline specified by TR39. Pass { map: CONFUSABLE_MAP } if your pipeline runs NFKC normalization before calling skeleton().
Most confusable-detection libraries apply a character map in isolation. namespace-guard uses a three-stage pipeline where each stage is aware of the others:
Input → NFKC normalize → Confusable map → Mixed-script reject
(stage 1) (stage 2) (stage 3)
Stage 1: NFKC normalization collapses full-width characters (I → I), ligatures (fi → fi), superscripts, and other Unicode compatibility forms to their canonical equivalents. This runs first, before any confusable check.
Stage 2: Confusable map catches characters that survive NFKC but visually mimic Latin letters - Cyrillic а for a, Greek ο for o, Cherokee Ꭺ for A, and 600+ others from the Unicode Consortium's confusables.txt.
Stage 3: Mixed-script rejection (rejectMixedScript: true) blocks identifiers that mix Latin with non-Latin scripts (Hebrew, Arabic, Devanagari, Thai, Georgian, Ethiopic, etc.) even if the specific characters aren't in the confusable map. This catches novel homoglyphs that the map doesn't cover.
The key insight: TR39's confusables.txt and NFKC normalization sometimes disagree. For example, Unicode says capital I (U+0049) is confusable with lowercase l - visually true in many fonts. But NFKC maps Mathematical Bold 𝐈 (U+1D408) to I, not l. If you naively ship the TR39 mapping (𝐈 → l), the confusable check will never see that character - NFKC already converted it to I in stage 1.
We found 31 entries where this happens:
| Character | TR39 says | NFKC says | Winner |
|---|---|---|---|
ſ Long S (U+017F) |
f |
s |
NFKC (s is correct) |
Ⅰ Roman Numeral I (U+2160) |
l |
i |
NFKC (i is correct) |
I Fullwidth I (U+FF29) |
l |
i |
NFKC (i is correct) |
𝟎 Math Bold 0 (U+1D7CE) |
o |
0 |
NFKC (0 is correct) |
| 11 Mathematical I variants | l |
i |
NFKC |
| 12 Mathematical 0/1 variants | o/l |
0/1 |
NFKC |
These entries are unreachable in any pipeline that runs NFKC first - NFKC has already transformed the character before the confusable map sees it. In a non-NFKC pipeline (which is what TR39 specifies), these entries are correct visual judgments. The generate script (scripts/generate-confusables.ts) produces both CONFUSABLE_MAP (NFKC-filtered) and CONFUSABLE_MAP_FULL (unfiltered) so you can match the map to your normalization strategy.
For reproducible cross-library testing, import the named vector suite directly:
import {
COMPOSABILITY_VECTOR_SUITE,
COMPOSABILITY_VECTORS,
COMPOSABILITY_VECTORS_COUNT,
} from "namespace-guard/composability-vectors";This suite is an alias of NFKC_TR39_DIVERGENCE_VECTORS and represents TR39-full vs NFKC-lowercase divergence vectors.
COMPOSABILITY_VECTOR_SUITE: stable suite id ("nfkc-tr39-divergence-v1")COMPOSABILITY_VECTORS: vector rows ({ char, codePoint, tr39, nfkc })COMPOSABILITY_VECTORS_COUNT: row count
JSON artifact for tooling/pipelines is included at:
docs/data/composability-vectors.json
For cross-tool evaluation and repeatable security testing, use:
docs/data/confusable-bench.v1.json
This dataset includes labeled malicious and benign rows across:
- NFKC/TR39 divergence vectors
- confusable substitutions and mixed-script variants
- default-ignorable and bidi control insertions
- combining-mark evasion rows
- benign controls (including precomposed and decomposed accent forms)
Generation/source notes:
docs/data/confusable-bench.v1.SOURCE.md
Confusable characters are visually identical to Latin letters but encode as multi-byte BPE tokens. A 95-line contract inflates from 881 to 4,567 tokens when flooded with confusables: 5.2x the API bill. We tested this across 4 models, 8 attack types, and 130+ API calls. The model reads through every substitution. The billing attack succeeds. We call it Denial of Spend. Full research: The new DDoS: Unicode confusables can't fool LLMs, but they can 5x your API bill.
Use namespace-guard as a deterministic preprocessing stage before sending text to your model:
Document ingestion
|
v
+----------------+
| namespace- | <-- Detect mixed-script confusable substitution
| guard | <-- Canonicalise to Latin equivalents
| (microseconds) | <-- Flag suspicious patterns for review
+----------------+
|
v
+----------------+
| LLM API | <-- Any model/provider
| (GPT/Claude/ | <-- Receives canonicalised text
| Llama/etc) |
+----------------+
|
v
Analysis output
Rewrites confusable characters to Latin equivalents. By default (strategy: "mixed"), only rewrites characters inside tokens that already contain Latin letters. Standalone non-Latin words are preserved to reduce false positives in multilingual text.
With strategy: "all", rewrites every confusable character regardless of surrounding context. Use this when the document is known to be Latin-script (e.g. an English contract) and you need to catch words where every character was substituted.
import { canonicalise } from "namespace-guard";
canonicalise("The seller аssumes аll liаbility.");
// "The seller assumes all liability."
canonicalise("Москва is the capital");
// "Москва is the capital" (standalone Cyrillic word preserved)
// Strategy "all": rewrites even standalone non-Latin confusable words
canonicalise("поп-refundable", { strategy: "all" });
// "non-refundable" (all Cyrillic п/о/п replaced)
canonicalise("ԝаіⅴеѕ any right", { strategy: "all" });
// "waives any right" (every confusable character replaced)Returns structured findings (codepoint, script, latinEquivalent, visualScore, source, position, word context) plus a summary risk level (none | low | medium | high).
import { scan } from "namespace-guard";
const report = scan("The seller liаbility clause applies.");
// report.hasConfusables === true
// report.summary.riskLevel === "medium" | "high" (context dependent)Fast boolean gate for confusable substitution. Short-circuits on first suspicious match.
With the default strategy: "mixed", only mixed-script confusables fail the gate. With strategy: "all", any confusable character fails the gate.
import { isClean } from "namespace-guard";
isClean("The seller assumes all liability."); // true
isClean("The seller liаbility clause applies."); // false
// Strategy "all": any confusable fails, even standalone non-Latin
isClean("поп-refundable", { strategy: "all" }); // falsestrategy--"mixed"(default) only acts on tokens containing both Latin and non-Latin characters;"all"acts on every confusable character regardless of context. Use"all"for known-Latin documents.threshold-- minimum visual similarity score for replacement/detection (default:0.7)includeNovel-- include confusable-vision novel mappings in addition to TR39 baseline (default:true)scripts-- optional allowlist of source scripts (case-insensitive)riskTerms(scan/isClean) -- optional list of high-value terms used by risk heuristics
By default, normalize() applies NFKC normalization before lowercasing. This collapses full-width characters, ligatures, superscripts, and other Unicode compatibility forms to their canonical equivalents:
normalize("hello"); // "hello" (full-width → ASCII)
normalize("\ufb01nance"); // "finance" (fi ligature → fi)NFKC is a no-op for ASCII input and matches what ENS, GitHub, and Unicode IDNA standards mandate. To opt out:
const guard = createNamespaceGuard({
sources: [/* ... */],
normalizeUnicode: false,
}, adapter);Twitter/X blocks purely numeric handles. Enable this with allowPurelyNumeric: false:
const guard = createNamespaceGuard({
sources: [/* ... */],
allowPurelyNumeric: false,
messages: {
purelyNumeric: "Handles cannot be all numbers.", // optional custom message
},
}, adapter);
await guard.check("123456"); // { available: false, reason: "invalid", message: "Handles cannot be all numbers." }
await guard.check("abc123"); // available (has letters)When a slug is taken, automatically suggest available alternatives using pluggable strategies:
const guard = createNamespaceGuard({
sources: [/* ... */],
suggest: {
// Named strategy (default: ["sequential", "random-digits"])
strategy: "suffix-words",
// Max suggestions to return (default: 3)
max: 3,
},
}, adapter);
const result = await guard.check("acme-corp");
// {
// available: false,
// reason: "taken",
// message: "That name is already in use.",
// source: "organization",
// suggestions: ["acme-corp-dev", "acme-corp-io", "acme-corp-app"]
// }| Strategy | Example Output | Description |
|---|---|---|
"sequential" |
sarah-1, sarah1, sarah-2 |
Hyphenated and compact numeric suffixes |
"random-digits" |
sarah-4821, sarah-1037 |
Random 3-4 digit suffixes |
"suffix-words" |
sarah-dev, sarah-hq, sarah-app |
Common word suffixes |
"short-random" |
sarah-x7k, sarah-m2p |
Short 3-char alphanumeric suffixes |
"scramble" |
asrah, sarha |
Adjacent character transpositions |
"similar" |
sara, darah, thesarah |
Edit-distance-1 mutations (deletions, keyboard-adjacent substitutions, prefix/suffix) |
Combine multiple strategies - candidates are interleaved round-robin:
suggest: {
strategy: ["random-digits", "suffix-words"],
max: 4,
}
// → ["sarah-4821", "sarah-dev", "sarah-1037", "sarah-io"]Pass a function that returns candidate slugs:
suggest: {
strategy: (identifier) => [
`${identifier}-io`,
`${identifier}-app`,
`the-real-${identifier}`,
],
}Suggestions are verified against format, reserved names, validators, and database collisions using a progressive batched pipeline. Only available suggestions are returned.
Check multiple identifiers at once:
const results = await guard.checkMany(["sarah", "admin", "acme-corp"]);
// {
// sarah: { available: true },
// admin: { available: false, reason: "reserved", ... },
// "acme-corp": { available: false, reason: "taken", ... }
// }All checks run in parallel. Accepts an optional scope parameter.
When users update their own slug, you don't want a false "already taken" error:
// User with ID "user_123" wants to change handle from "sarah" to "sarah-dev"
// Without scoping, this would error because "sarah-dev" != their current handle
// Pass their ID to exclude their own record from collision detection
const result = await guard.check("sarah-dev", { id: "user_123" });
// Available (unless another user/org has it)The scope object keys map to scopeKey in your source config. This lets you check multiple ownership types:
// Check if a user OR their org owns this slug
const result = await guard.check("acme", {
userId: currentUser.id,
orgId: currentOrg.id,
});Validate slugs from the command line:
# Format + reserved name checking (no database needed)
npx namespace-guard check acme-corp
# ✓ acme-corp is available
npx namespace-guard check admin
# ✗ admin - That name is reserved. Try another one.
npx namespace-guard check "a"
# ✗ a - Use 2-30 lowercase letters, numbers, or hyphens.
# Risk scoring against protected targets
npx namespace-guard risk paуpal --protect paypal
# ⛔ paуpal — risk 100/100 (block)Create namespace-guard.config.json:
{
"reserved": ["admin", "api", "settings", "dashboard"],
"pattern": "^[a-z0-9][a-z0-9-]{2,39}$",
"sources": [
{ "name": "users", "column": "handle" },
{ "name": "organizations", "column": "slug" }
]
}Or with categorized reserved names:
{
"reserved": {
"system": ["admin", "api", "settings"],
"brand": ["oncor"]
}
}npx namespace-guard check sarah --config ./my-config.jsonnpx namespace-guard check sarah --database-url postgres://localhost/mydbRequires pg to be installed (npm install pg).
Exit code 0 = available, 1 = unavailable.
# Warn/block thresholds (0-100)
npx namespace-guard risk paypa1 --protect paypal --warn-threshold 45 --block-threshold 80
# Fail CI on warn or block (default fail mode is block only)
npx namespace-guard risk paypa1 --protect paypal --fail-on warn
# JSON output for automation
npx namespace-guard risk paуpal --protect paypal --jsonGenerate confusable attack candidates for red-team testing and policy tuning:
# one-liner default: evasion mode (confusables + substitutions)
npx namespace-guard attack-gen paypal
npx namespace-guard attack-gen paypal --json
# Impersonation-focused generation (Unicode/NFKC/NFC confusables)
npx namespace-guard attack-gen paypal --mode impersonation
# Profanity-evasion generation (adds ASCII/lookalike substitutions)
npx namespace-guard attack-gen shit --mode evasion --json
# Explore deeper substitutions and wider candidate pools
npx namespace-guard attack-gen github --max-edits 2 --max-candidates 50Default mode is evasion for practical coverage. Use --mode impersonation when you want Unicode-only spoof analysis without substitution noise.
Useful for finding:
- high-risk variants your policy already blocks
- non-blocking variants (
alloworwarn) that still pass format checks (useful for tightening thresholds/protect lists)
Audit an exported dataset for canonical collisions before enforcing a DB unique constraint:
# Dataset must be a JSON array. Identifier can be in: identifier/raw/handle/slug/username/value
npx namespace-guard audit-canonical ./users-export.json
npx namespace-guard audit-canonical ./users-export.json --jsonAccepted optional stored-canonical fields:
canonicalnormalizedhandleCanonicalslugCanonicalhandle_canonicalslug_canonical
Exit code:
0when no collisions/mismatches are found1when collisions or canonical mismatches are found
Use labeled examples to recommend warn/block thresholds for your namespace:
[
{ "identifier": "paуpal", "label": "malicious", "target": "paypal" },
{ "identifier": "teamspace", "label": "benign", "target": "paypal" }
]npx namespace-guard calibrate ./risk-dataset.json
npx namespace-guard calibrate ./risk-dataset.json --json
# Cost-aware calibration (optimize expected harm, not just F1)
npx namespace-guard calibrate ./risk-dataset.json \
--cost-block-benign 8 \
--cost-warn-benign 1 \
--cost-allow-malicious 12 \
--cost-warn-malicious 3 \
--malicious-prior 0.05Run calibration + drift together and get a ready-to-paste risk config plus CI gate command:
npx namespace-guard recommend ./risk-dataset.json
npx namespace-guard recommend ./risk-dataset.json --jsonThis is the fastest onboarding path when you already have labeled examples.
It calibrates thresholds from your dataset, then derives CI gate budgets from the built-in composability vector suite.
Quantify composability drift between TR39-full mapping (CONFUSABLE_MAP_FULL) and NFKC-filtered mapping (CONFUSABLE_MAP):
# Built-in composability vector suite
npx namespace-guard drift
# Your own dataset (same shape as calibrate)
npx namespace-guard drift ./risk-dataset.json --jsonUse the included drift gate script to fail CI if drift metrics exceed your budget:
# Build first so dist/cli.js exists
npm run build
# Fail when drift exceeds these limits
npm run ci:drift-gate -- \
--max-action-flips 29 \
--max-average-score-delta 95 \
--max-abs-score-delta 100GitHub Actions workflow is included at .github/workflows/drift-gate.yml.
Creates a guard instance with your configuration and database adapter.
Returns: NamespaceGuard instance
Create a guard with practical profile defaults, then apply your explicit config overrides.
Built-in profiles:
consumer-handleorg-slugdeveloper-id
Each profile sets defaults for:
patternnormalizeUnicodeallowPurelyNumericriskthresholds (warnThreshold,blockThreshold, etc.)
Check if an identifier is available.
Parameters:
identifier- The slug/handle to checkscope- Optional ownership scope to exclude own records
Returns:
// Available
{ available: true }
// Not available
{
available: false,
reason: "invalid" | "reserved" | "taken",
message: string,
source?: string, // Which table caused the collision (reason: "taken")
category?: string, // Reserved name category (reason: "reserved")
suggestions?: string[] // Available alternatives (reason: "taken", requires suggest config)
}Check multiple identifiers in parallel. Suggestions are skipped by default for performance.
Parameters:
identifiers- Array of slugs/handles to checkscope- Optional ownership scope applied to all checksoptions- Optional{ skipSuggestions?: boolean }(default:true)
Pass { skipSuggestions: false } to include suggestions for taken identifiers.
Returns: Record<string, CheckResult>
Score spoofing/confusability risk against protected targets using weighted confusable distance + chain depth.
Parameters:
identifier- Candidate slug/handle to assessoptions- Optional:protect?: string[]additional high-value targets to compare againstincludeReserved?: booleaninclude configured reserved names as protected targets (default:true)warnThreshold?: numberthreshold foraction: "warn"(default:45)blockThreshold?: numberthreshold foraction: "block"(default:70)maxMatches?: numbernumber of top matches to return (default:3)map?: Record<string, string>custom confusable map
Returns: { score, level, action, reasons, matches, ... }
Apply a deny policy on top of risk scoring.
Options:
- All
checkRiskoptions failOn?: "block" | "warn"("block"default)messages?: { warn?: string; block?: string }- If
protectis omitted, usesconfig.risk.protect, then falls back toDEFAULT_PROTECTED_TOKENS
Returns: { allowed, action, message?, risk }
Same as check(), but throws an Error if not available.
One-liner guard for production claim checks.
Runs:
check()(format/reserved/validators/database)enforceRisk()(confusable risk policy)
Throws an Error if the identifier should not be claimed.
Race-safe claim helper. Runs claimability checks, then executes your write callback with the normalized identifier.
If a duplicate-key/unique-constraint race happens, it returns an unavailable result instead of throwing.
const result = await guard.claim(input.handle, async (canonical) => {
return prisma.user.create({
data: {
handle: input.handle,
handleCanonical: canonical,
},
});
});
if (!result.claimed) {
// show result.message to user
}Options:
- All
assertClaimableoptions scope?: OwnershipScopeisUniqueViolation?: (error) => booleancustom duplicate detectortakenMessage?: stringcustom duplicate-race message
Validate format, purely-numeric restriction, and reserved name status without querying the database.
Returns: Error message string if invalid or reserved, null if OK.
Validate only the identifier's format and purely-numeric restriction. Does not check reserved names or query the database. Useful for instant client-side feedback on input shape.
Returns: Error message string if the format is invalid, null if OK.
Convenience re-export of the standalone normalize() function. Note: always applies NFKC normalization regardless of the guard's normalizeUnicode setting. Use normalize(id, { unicode: false }) directly if you need to skip NFKC.
Clear the in-memory cache and reset hit/miss counters. No-op if caching is not enabled.
Get cache performance statistics.
Returns: { size: number; hits: number; misses: number }
Utility function to normalize identifiers. Trims whitespace, applies NFKC Unicode normalization (by default), lowercases, and strips leading @ symbols. Pass { unicode: false } to skip NFKC.
import { normalize } from "namespace-guard";
normalize(" @Sarah "); // "sarah"
normalize("ACME-Corp"); // "acme-corp"Best-effort detector for duplicate-key / unique-constraint write errors across common stacks (Postgres 23505, Prisma P2002, MySQL ER_DUP_ENTRY, SQLite constraint errors, Mongo E11000).
Useful when you want custom race handling outside guard.claim().
LLM preprocessing helper that rewrites confusable characters to Latin equivalents in Latin-containing tokens.
Returns: string
Options:
threshold?: number(default0.7)includeNovel?: boolean(defaulttrue)scripts?: string[](optional script allowlist)
LLM preprocessing scanner that returns structured confusable findings and summary risk metadata.
Returns: ScanResult
ScanResult:
hasConfusables: booleancount: numberfindings: ScanFinding[]summary: { distinctChars, wordsAffected, scriptsDetected, riskLevel }
ScanFinding:
char,codepoint,script,latinEquivalentvisualScore,source("tr39" | "novel")index,word,mixedScript
Options:
threshold?: number(default0.7)includeNovel?: boolean(defaulttrue)scripts?: string[](optional script allowlist)riskTerms?: string[](optional targeted-term hints for risk scoring)
Fast boolean gate for mixed-script confusable substitutions. Short-circuits on first suspicious finding.
Returns: boolean
Options: same as scan().
LLM_CONFUSABLE_MAP, LLM_CONFUSABLE_MAP_PAIR_COUNT, LLM_CONFUSABLE_MAP_CHAR_COUNT, LLM_CONFUSABLE_MAP_SOURCE_COUNTS
Static lookup data powering the LLM preprocessing helpers.
LLM_CONFUSABLE_MAP: source char -> candidate Latin mappings with visual similarity score and source metadataLLM_CONFUSABLE_MAP_PAIR_COUNT: total mapping rowsLLM_CONFUSABLE_MAP_CHAR_COUNT: number of source charactersLLM_CONFUSABLE_MAP_SOURCE_COUNTS: split by{ tr39, novel }
Regenerate from source artifacts with:
npm run build:llm-confusable-mapUse these when you need explicit pairwise spoof checks, custom scoring pipelines, or explainable risk details outside guard.check() / guard.checkRisk().
Generate a TR39-style skeleton string for visual-comparison workflows.
- Default map:
CONFUSABLE_MAP_FULL(TR39/raw-input compatible) - Optional map override:
{ map: CONFUSABLE_MAP }for NFKC-first pipelines - Removes default-ignorable characters before map replacement
import { skeleton, CONFUSABLE_MAP } from "namespace-guard";
skeleton("pa\u0443pal"); // "paypal"
skeleton("pay\u200Bpal"); // "paypal"
skeleton("pa\u0443pal", { map: CONFUSABLE_MAP }); // explicit NFKC-first map modeBoolean helper. Without weights, uses skeleton() equality (TR39 coverage only). With weights, also checks character-level visual similarity from confusable-vision's measured data (v2: RaySpace), including cross-script pairs.
import { areConfusable } from "namespace-guard";
import { CONFUSABLE_WEIGHTS } from "namespace-guard/confusable-weights";
areConfusable("paypal", "pa\u0443pal"); // true (skeleton match)
areConfusable("hello", "world"); // false
// With weights: catches cross-script pairs that skeleton() misses
areConfusable("\u1175", "\u4E28", { weights: CONFUSABLE_WEIGHTS }); // true (Hangul/Han)
areConfusable("\u0406", "\u0399", { weights: CONFUSABLE_WEIGHTS }); // true (Cyrillic/Greek)
areConfusable("\u1175", "\u4E28"); // false (no weights = skeleton only)Analyse an identifier for cross-script confusable risk. Returns the scripts present, any confusable pairs found between different scripts, and a risk level.
import { detectCrossScriptRisk } from "namespace-guard";
import { CONFUSABLE_WEIGHTS } from "namespace-guard/confusable-weights";
detectCrossScriptRisk("hello"); // { riskLevel: "none", scripts: ["latin"], crossScriptPairs: [] }
detectCrossScriptRisk("\u1175\u4E28", { weights: CONFUSABLE_WEIGHTS });
// { riskLevel: "high", scripts: ["hangul", "han"], crossScriptPairs: [{ a: { char: "ᅵ", script: "hangul" }, b: { char: "丨", script: "han" }, score: 0.996 }] }Options:
weights-- optional visually-scored weights for cross-script pair lookup
Compute weighted confusable distance between two strings.
Outputs:
distance(lower means closer)similarity(0..1)chainDepth(number of non-trivial edit steps)crossScriptCount,ignorableCount,divergenceCountsteps(explainable shortest-path operations)skeletonEqual/normalizedEqual
import { confusableDistance } from "namespace-guard";
const score = confusableDistance("paypal", "pa\u0443pal");
// score.similarity, score.chainDepth, score.steps, etc.Options:
map-- confusable character map (default:CONFUSABLE_MAP_FULL)weights-- optional measured visual weights from confusable-vision scoring; when provided, TR39 pairs use measured cost instead of hardcoded 0.35, and novel pairs (not in TR39 map) use their visual-weight costcontext-- filter weights by deployment context:'identifier'(XID_Continue only),'domain'(IDNA PVALID only), or'all'(default)
Weighted usage:
import { confusableDistance } from "namespace-guard";
import { CONFUSABLE_WEIGHTS } from "namespace-guard/confusable-weights";
const result = confusableDistance("paypal", "pa\u0443pal", {
weights: CONFUSABLE_WEIGHTS,
context: "identifier",
});
// Steps with reason: "visual-weight" indicate novel pairs scored via RaySpaceCheck whether a domain label is a realistic spoof of a target label. Unlike areConfusable(), this function only flags threats that could produce registrable domain names under ICANN IDN rules — mixed-script labels are excluded because registrars reject them.
import { isDomainSpoof } from "namespace-guard";
import { CONFUSABLE_WEIGHTS } from "namespace-guard/confusable-weights";
// Full-Cyrillic lookalike of "paypal" — realistic, registrable spoof
isDomainSpoof("\u0440\u0430\u0443\u0440\u0430\u04CF", "paypal", { weights: CONFUSABLE_WEIGHTS });
// { spoof: true, script: "cyrillic", danger: 0.91, substitutions: [...] }
// Mixed-script — cannot be registered, not a spoof
isDomainSpoof("\u0440aypal", "paypal", { weights: CONFUSABLE_WEIGHTS });
// { spoof: false }
// Known-legitimate non-Latin domain — skip via allowlist
isDomainSpoof("\u0430\u0441\u0435", "ace", {
weights: CONFUSABLE_WEIGHTS,
allowlist: ["\u0430\u0441\u0435"],
});
// { spoof: false }Options:
map-- confusable character map (default:CONFUSABLE_MAP_FULL)weights-- measured visual weights for similarity scoringminDanger-- minimum average danger forspoofto betrue(default:0.5). Thedangerscore is always returned regardless, so callers can apply their own thresholdallowlist-- known-legitimate non-Latin labels to skip (checked after NFKC + lowercase normalisation)
Returns DomainSpoofResult:
spoof-- opinionated verdict (truewhendanger >= minDanger)script-- script of the spoofing label (always set when a match is found, even below threshold)danger-- average visual similarity across substitutions (0–1)substitutions-- per-character details:{ index, from, to, similarity }
Derive the composability regression corpus: characters where TR39 mapping and NFKC lowercase disagree.
Built-in divergence vectors derived from CONFUSABLE_MAP_FULL, useful for drift and pipeline regression tests.
Named aliases for the same corpus, packaged for cross-library regression imports.
By default, slug lookups are case-sensitive. Enable case-insensitive matching to catch collisions regardless of stored casing:
const guard = createNamespaceGuard({
sources: [/* ... */],
caseInsensitive: true,
}, adapter);Each adapter handles this differently:
- Prisma: Uses
mode: "insensitive"on the where clause - Drizzle: Uses
ilikeinstead ofeq(passiliketo the adapter:createDrizzleAdapter(db, tables, { eq, ilike })) - Kysely: Uses
ilikeoperator - Knex: Uses
LOWER()in a raw where clause - TypeORM: Uses
ILike(pass it to the adapter:createTypeORMAdapter(dataSource, entities, ILike)) - MikroORM: Uses
$ilikeoperator - Sequelize: Uses
LOWER()via Sequelize helpers (pass{ where: Sequelize.where, fn: Sequelize.fn, col: Sequelize.col }) - Mongoose: Uses collation
{ locale: "en", strength: 2 } - Raw SQL: Wraps both sides in
LOWER()
Enable in-memory caching to reduce database calls during rapid checks (e.g., live form validation, suggestion generation):
const guard = createNamespaceGuard({
sources: [/* ... */],
cache: {
ttl: 5000, // milliseconds (default: 5000)
maxSize: 1000, // max cached entries before LRU eviction (default: 1000)
},
}, adapter);
// Manually clear the cache after writes
guard.clearCache();
// Monitor cache performance
const stats = guard.cacheStats();
// { size: 12, hits: 48, misses: 12 }// lib/guard.ts
import { createNamespaceGuard } from "namespace-guard";
import { createPrismaAdapter } from "namespace-guard/adapters/prisma";
import { prisma } from "./db";
export const guard = createNamespaceGuard({
reserved: ["admin", "api", "settings"],
sources: [
{ name: "user", column: "handle", scopeKey: "id" },
{ name: "organization", column: "slug", scopeKey: "id" },
],
suggest: {},
}, createPrismaAdapter(prisma));
// app/signup/actions.ts
"use server";
import { guard } from "@/lib/guard";
export async function checkHandle(handle: string) {
return guard.check(handle);
}
export async function createUser(handle: string, email: string) {
const result = await guard.check(handle);
if (!result.available) return { error: result.message };
const user = await prisma.user.create({
data: { handle: guard.normalize(handle), email },
});
return { user };
}import express from "express";
import { guard } from "./lib/guard";
const app = express();
// Reusable middleware
function validateSlug(req, res, next) {
const slug = req.body.handle || req.body.slug;
if (!slug) return res.status(400).json({ error: "Slug is required" });
guard.check(slug, { id: req.user?.id }).then((result) => {
if (!result.available) return res.status(409).json(result);
req.normalizedSlug = guard.normalize(slug);
next();
});
}
app.post("/api/users", validateSlug, async (req, res) => {
const user = await db.user.create({ handle: req.normalizedSlug, ... });
res.json({ user });
});import { z } from "zod";
import { router, protectedProcedure } from "./trpc";
import { guard } from "./lib/guard";
export const namespaceRouter = router({
check: protectedProcedure
.input(z.object({ slug: z.string() }))
.query(async ({ input, ctx }) => {
return guard.check(input.slug, { id: ctx.user.id });
}),
claim: protectedProcedure
.input(z.object({ slug: z.string() }))
.mutation(async ({ input, ctx }) => {
await guard.assertClaimable(input.slug, { id: ctx.user.id });
return ctx.db.user.update({
where: { id: ctx.user.id },
data: { handle: guard.normalize(input.slug) },
});
}),
});Full TypeScript support with exported types:
import {
createNamespaceGuard,
createNamespaceGuardWithProfile,
createPredicateValidator,
createProfanityValidator,
createHomoglyphValidator,
createInvisibleCharacterValidator,
skeleton,
areConfusable,
confusableDistance,
deriveNfkcTr39DivergenceVectors,
isLikelyUniqueViolationError,
NAMESPACE_PROFILES,
DEFAULT_PROTECTED_TOKENS,
NFKC_TR39_DIVERGENCE_VECTORS,
COMPOSABILITY_VECTOR_SUITE,
COMPOSABILITY_VECTORS,
COMPOSABILITY_VECTORS_COUNT,
CONFUSABLE_MAP,
CONFUSABLE_MAP_FULL,
normalize,
type NamespaceConfig,
type NamespaceSource,
type NamespaceAdapter,
type NamespaceGuard,
type CheckResult,
type FindOneOptions,
type OwnershipScope,
type NamespaceValidator,
type NamespaceValidatorResult,
type PredicateValidatorOptions,
type SuggestStrategyName,
type ProfanityValidationMode,
type ProfanityVariantProfile,
type ProfanityValidatorOptions,
type InvisibleCharacterValidatorOptions,
type SkeletonOptions,
type CheckManyOptions,
type CheckRiskOptions,
type RiskCheckResult,
type AssertClaimableOptions,
type ClaimOptions,
type ClaimResult,
type EnforceRiskOptions,
type EnforceRiskResult,
type UniqueViolationDetector,
type RiskReason,
type RiskMatch,
type RiskLevel,
type RiskAction,
type NamespaceProfileName,
type NamespaceProfilePreset,
type ConfusableDistanceOptions,
type ConfusableDistanceResult,
type ConfusableDistanceStep,
type ConfusableWeight,
type ConfusableWeights,
type NfkcTr39DivergenceVector,
type ComposabilityVector,
} from "namespace-guard";Optional curated profanity subpath export:
import {
createEnglishProfanityValidator,
PROFANITY_WORDS_EN,
PROFANITY_WORDS_EN_COUNT,
PROFANITY_WORDS_EN_SOURCE,
PROFANITY_WORDS_EN_LICENSE,
} from "namespace-guard/profanity-en";Composability vectors subpath export:
import {
COMPOSABILITY_VECTOR_SUITE,
COMPOSABILITY_VECTORS,
COMPOSABILITY_VECTORS_COUNT,
type ComposabilityVector,
} from "namespace-guard/composability-vectors";Confusable weights subpath export:
import { CONFUSABLE_WEIGHTS } from "namespace-guard/confusable-weights";
// 4,174 visually-scored pairs (3,111 TR39 + 1,063 novel discoveries)
// Each pair has a `danger` score (0–1) measuring geometric similarity across 245 fonts.
// The shipped dataset uses a 0.5 floor. For higher precision, filter at danger > 0.7 (574 pairs).
// Pass to confusableDistance() for measured visual costs
const result = confusableDistance("paypal", "pa\u0443pal", {
weights: CONFUSABLE_WEIGHTS,
});If you find this useful, consider supporting the project:
Contributions welcome! Please open an issue first to discuss what you'd like to change.