Skip to content

Commit

Permalink
lexicon: add support for TID and record-key string formats (bluesky-s…
Browse files Browse the repository at this point in the history
…ocial#2377)

add support for TID and record-key string formats
  • Loading branch information
bnewbold authored Apr 6, 2024
1 parent 7570d31 commit 6b3addd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/lexicon/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const lexStringFormat = z.enum([
'nsid',
'cid',
'language',
'tid',
'record-key',
])
export type LexStringFormat = z.infer<typeof lexStringFormat>

Expand Down
28 changes: 28 additions & 0 deletions packages/lexicon/src/validators/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
ensureValidHandle,
ensureValidNsid,
ensureValidAtUri,
ensureValidTid,
ensureValidRecordKey,
} from '@atproto/syntax'
import { validateLanguage } from '@atproto/common-web'

Expand Down Expand Up @@ -122,3 +124,29 @@ export function language(path: string, value: string): ValidationResult {
),
}
}

export function tid(path: string, value: string): ValidationResult {
try {
ensureValidTid(value)
} catch {
return {
success: false,
error: new ValidationError(
`${path} must be a valid TID (timestamp identifier)`,
),
}
}
return { success: true, value }
}

export function recordKey(path: string, value: string): ValidationResult {
try {
ensureValidRecordKey(value)
} catch {
return {
success: false,
error: new ValidationError(`${path} must be a valid Record Key`),
}
}
return { success: true, value }
}
4 changes: 4 additions & 0 deletions packages/lexicon/src/validators/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ export function string(
return formats.cid(path, value)
case 'language':
return formats.language(path, value)
case 'tid':
return formats.tid(path, value)
case 'record-key':
return formats.recordKey(path, value)
}
}

Expand Down

0 comments on commit 6b3addd

Please sign in to comment.