-
Notifications
You must be signed in to change notification settings - Fork 0
feat: introduce branded user identifiers #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,12 +4,17 @@ | |||||||||||||||||||
| import { z } from 'zod'; | ||||||||||||||||||||
| import { customFetchParsed } from './fetcher'; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export type UserId = string & { readonly brand: 'UserId' }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export interface User { | ||||||||||||||||||||
| id: UserId; | ||||||||||||||||||||
| display_name: string; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+9
to
+13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Document and freeze the public User shape; mark fields readonly Document the export and make fields readonly to discourage mutation of parsed data in consumers. -export interface User {
- id: UserId;
- display_name: string;
-}
+/** Public user shape parsed from the API. Invariant: id is a branded identifier. */
+export interface User {
+ readonly id: UserId;
+ readonly display_name: string;
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| const userSchema = z.object({ | ||||||||||||||||||||
| id: z.string(), | ||||||||||||||||||||
| id: z.string().transform(id => id as UserId), | ||||||||||||||||||||
| display_name: z.string(), | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export type User = z.infer<typeof userSchema>; | ||||||||||||||||||||
| }) satisfies z.ZodType<User>; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export const listUsers = (signal?: AbortSignal) => | ||||||||||||||||||||
| customFetchParsed('/api/users', z.array(userSchema), { signal }); | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Prefer Zod .brand() and derive UserId from a schema; drop the manual intersection brand
Eliminate the hand-rolled brand and derive the type from a dedicated schema to keep runtime validation and types in lockstep and avoid the no-op transform later.
Apply this diff to replace the manual brand with a schema-derived type:
Then, add this schema near the imports (outside this hunk):