-
Notifications
You must be signed in to change notification settings - Fork 403
/
formats.ts
124 lines (115 loc) · 3.01 KB
/
formats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { isValidISODateString } from 'iso-datestring-validator'
import { CID } from 'multiformats/cid'
import { ValidationResult, ValidationError } from '../types'
import {
ensureValidDid,
ensureValidHandle,
ensureValidNsid,
ensureValidAtUri,
} from '@atproto/syntax'
import { validateLanguage } from '@atproto/common-web'
export function datetime(path: string, value: string): ValidationResult {
try {
if (!isValidISODateString(value)) {
throw new Error()
}
} catch {
return {
success: false,
error: new ValidationError(
`${path} must be an valid atproto datetime (both RFC-3339 and ISO-8601)`,
),
}
}
return { success: true, value }
}
export function uri(path: string, value: string): ValidationResult {
const isUri = value.match(/^\w+:(?:\/\/)?[^\s/][^\s]*$/) !== null
if (!isUri) {
return {
success: false,
error: new ValidationError(`${path} must be a uri`),
}
}
return { success: true, value }
}
export function atUri(path: string, value: string): ValidationResult {
try {
ensureValidAtUri(value)
} catch {
return {
success: false,
error: new ValidationError(`${path} must be a valid at-uri`),
}
}
return { success: true, value }
}
export function did(path: string, value: string): ValidationResult {
try {
ensureValidDid(value)
} catch {
return {
success: false,
error: new ValidationError(`${path} must be a valid did`),
}
}
return { success: true, value }
}
export function handle(path: string, value: string): ValidationResult {
try {
ensureValidHandle(value)
} catch {
return {
success: false,
error: new ValidationError(`${path} must be a valid handle`),
}
}
return { success: true, value }
}
export function atIdentifier(path: string, value: string): ValidationResult {
const isDid = did(path, value)
if (!isDid.success) {
const isHandle = handle(path, value)
if (!isHandle.success) {
return {
success: false,
error: new ValidationError(`${path} must be a valid did or a handle`),
}
}
}
return { success: true, value }
}
export function nsid(path: string, value: string): ValidationResult {
try {
ensureValidNsid(value)
} catch {
return {
success: false,
error: new ValidationError(`${path} must be a valid nsid`),
}
}
return { success: true, value }
}
export function cid(path: string, value: string): ValidationResult {
try {
CID.parse(value)
} catch {
return {
success: false,
error: new ValidationError(`${path} must be a cid string`),
}
}
return { success: true, value }
}
// The language format validates well-formed BCP 47 language tags: https://www.rfc-editor.org/info/bcp47
export function language(path: string, value: string): ValidationResult {
if (validateLanguage(value)) {
return { success: true, value }
}
return {
success: false,
error: new ValidationError(
`${path} must be a well-formed BCP 47 language tag`,
),
}
}