Skip to content

Commit a95b65a

Browse files
dmealingclaude
andcommitted
refactor(slug): derive the generation alphabet from the codegen'd validator
slug.ts hardcoded the 57-char slug ALPHABET as a literal that "MUST stay consistent with" the shortSlug abstract's validator.regex — a second copy of the same alphabet kept in sync by a comment. share.ts already single-sources its route-param VALIDATOR from Council.id's codegen'd field; this does the same for the GENERATION half. Derive both the alphabet and the length from Council.id.rules.pattern.value (the regex codegen'd from metaobjects/abstracts/meta-short-slug-field.yaml), so the slug shape now has exactly one source of truth: the metadata regex. Change the alphabet there once and both generation and validation track it on the next `npm run gen:db`. New test locks generation is a subset of validation against the REAL codegen'd pattern (not a hand-copied `[a-zA-Z0-9]`): every newSlug() must match Council.id's validator. No metadata, no generated code, no migration. drift/tsc/build/204 green. Part of the reference-impl single-source cleanup pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LPWr1dbYZnmsY6Vm91iWEN
1 parent aa2ceb8 commit a95b65a

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

‎src/db/slug.ts‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
// Slug generator. The alphabet here MUST stay consistent with the abstract
2-
// declared in metaobjects/abstracts/meta-short-slug-field.yaml — that file
3-
// is the source of truth for slug VALIDATION (codegen'd into Council.id's
4-
// Zod validator + the share.ts route-param guard, which now imports it).
5-
// This file remains hand-coded for GENERATION (Web Crypto is a runtime call,
6-
// not codegen).
1+
// URL-safe opaque short-slug generator.
72
//
8-
// Safe alphabet: alphanumerics minus visually-confusable 0/O/I/l/1 (57 chars).
9-
const ALPHABET = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
3+
// SINGLE SOURCE OF TRUTH: the slug SHAPE (alphabet + length) is declared once, as
4+
// the validator.regex on the `shortSlug` abstract in
5+
// metaobjects/abstracts/meta-short-slug-field.yaml. `Council.id extends: shortSlug`
6+
// codegen's that regex into Council.id's validator; we DERIVE the generation alphabet
7+
// and length straight out of that same codegen'd pattern here, so generation can never
8+
// drift from validation. (Web Crypto is a runtime call, so only the random draw stays
9+
// hand-coded — the alphabet itself is no longer restated.)
10+
//
11+
// share.ts derives its route-param VALIDATOR from the same Council.id field; this is
12+
// the generation-side half of that single-sourcing.
13+
import { Council } from "./generated/Council";
14+
15+
// The codegen'd slug pattern is /^[<alphabet>]{<length>}$/ — an explicit character
16+
// class (no ranges), so each character is an equiprobable draw. Pull both out of it.
17+
const { source: SLUG_PATTERN_SOURCE } = Council.id.rules.pattern.value;
18+
const shape = SLUG_PATTERN_SOURCE.match(/\[([^\]]+)\]\{(\d+)\}/);
19+
if (!shape) {
20+
throw new Error(`slug: could not derive alphabet/length from Council.id pattern "${SLUG_PATTERN_SOURCE}"`);
21+
}
22+
const ALPHABET = shape[1];
23+
const SLUG_LENGTH = Number(shape[2]);
1024

11-
export function newSlug(length = 8): string {
25+
export function newSlug(length = SLUG_LENGTH): string {
1226
const bytes = new Uint8Array(length);
1327
crypto.getRandomValues(bytes);
1428
let s = "";

‎test/slug.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from "vitest";
22
import { newSlug } from "../src/db/slug";
3+
import { Council } from "../src/db/generated/Council";
34

45
describe("newSlug", () => {
56
it("produces 8 chars from a safe alphabet (no 0/O/I/l/1)", () => {
@@ -11,6 +12,16 @@ describe("newSlug", () => {
1112
}
1213
});
1314

15+
it("every generated slug satisfies the codegen'd Council.id validator", () => {
16+
// Generation single-sources its alphabet+length from Council.id's regex, so
17+
// every slug it emits must pass that exact validator. Locks generation to
18+
// validation against the real pattern (not a hand-copied `[a-zA-Z0-9]`).
19+
const pattern = Council.id.rules.pattern.value;
20+
for (let i = 0; i < 200; i += 1) {
21+
expect(newSlug()).toMatch(pattern);
22+
}
23+
});
24+
1425
it("returns distinct values across many calls", () => {
1526
const set = new Set(Array.from({ length: 1000 }, () => newSlug()));
1627
expect(set.size).toBeGreaterThan(995);

0 commit comments

Comments
 (0)