@atproto/lex-schema 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/core/$type.d.ts +2 -2
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +1 -1
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/schema.d.ts +3 -2
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +1 -1
- package/dist/core/schema.js.map +1 -1
- package/dist/core/standard-schema.d.ts +2 -2
- package/dist/core/standard-schema.d.ts.map +1 -1
- package/dist/core/standard-schema.js.map +1 -1
- package/dist/core/string-format.d.ts +2 -2
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/validation-error.d.ts +1 -1
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +1 -1
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validator.d.ts +1 -1
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +1 -1
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +2 -2
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +2 -2
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +1 -1
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +1 -1
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +1 -1
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +2 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.js +1 -1
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.js +1 -1
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +1 -1
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +3 -3
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.js +1 -1
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +1 -1
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +1 -1
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +1 -1
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +2 -1
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.js +1 -1
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.js +1 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +1 -1
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +3 -1
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/lex-map.d.ts +1 -1
- package/dist/schema/lex-map.d.ts.map +1 -1
- package/dist/schema/lex-map.js +1 -1
- package/dist/schema/lex-map.js.map +1 -1
- package/dist/schema/lex-value.d.ts +1 -1
- package/dist/schema/lex-value.d.ts.map +1 -1
- package/dist/schema/lex-value.js +1 -1
- package/dist/schema/lex-value.js.map +1 -1
- package/dist/schema/literal.js +1 -1
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.js +1 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.js +1 -1
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +1 -1
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +1 -1
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +2 -1
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +1 -1
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +2 -1
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +2 -1
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +1 -1
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +1 -1
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +3 -2
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +2 -1
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +1 -1
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +1 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +1 -1
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +1 -1
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +2 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +1 -1
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +2 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +2 -2
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +1 -1
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +1 -1
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +1 -1
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +2 -2
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.js +1 -1
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +2 -2
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +1 -1
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +3 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +2 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +1 -1
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +1 -1
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +2 -2
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +1 -1
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +1 -1
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +1 -1
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +1 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +3 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +1 -1
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +1 -1
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.js +1 -1
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +1 -1
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +1 -1
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +6 -10
- package/src/core/$type.test.ts +0 -24
- package/src/core/$type.ts +0 -199
- package/src/core/record-key.ts +0 -85
- package/src/core/result.ts +0 -15
- package/src/core/schema.ts +0 -412
- package/src/core/standard-schema.test.ts +0 -124
- package/src/core/standard-schema.ts +0 -31
- package/src/core/string-format.ts +0 -411
- package/src/core/types.ts +0 -120
- package/src/core/validation-error.ts +0 -134
- package/src/core/validation-issue.ts +0 -340
- package/src/core/validator.ts +0 -636
- package/src/core.ts +0 -9
- package/src/external.ts +0 -3
- package/src/helpers.test.ts +0 -694
- package/src/helpers.ts +0 -222
- package/src/index.ts +0 -3
- package/src/schema/array.test.ts +0 -251
- package/src/schema/array.ts +0 -126
- package/src/schema/blob.test.ts +0 -733
- package/src/schema/blob.ts +0 -150
- package/src/schema/boolean.test.ts +0 -118
- package/src/schema/boolean.ts +0 -46
- package/src/schema/bytes.test.ts +0 -227
- package/src/schema/bytes.ts +0 -81
- package/src/schema/cid.test.ts +0 -125
- package/src/schema/cid.ts +0 -69
- package/src/schema/custom.test.ts +0 -414
- package/src/schema/custom.ts +0 -106
- package/src/schema/dict.test.ts +0 -181
- package/src/schema/dict.ts +0 -122
- package/src/schema/discriminated-union.test.ts +0 -676
- package/src/schema/discriminated-union.ts +0 -196
- package/src/schema/enum.test.ts +0 -398
- package/src/schema/enum.ts +0 -77
- package/src/schema/integer.test.ts +0 -314
- package/src/schema/integer.ts +0 -86
- package/src/schema/intersection.test.ts +0 -33
- package/src/schema/intersection.ts +0 -113
- package/src/schema/lex-map.test.ts +0 -593
- package/src/schema/lex-map.ts +0 -63
- package/src/schema/lex-value.test.ts +0 -81
- package/src/schema/lex-value.ts +0 -86
- package/src/schema/literal.test.ts +0 -533
- package/src/schema/literal.ts +0 -70
- package/src/schema/never.test.ts +0 -175
- package/src/schema/never.ts +0 -56
- package/src/schema/null.test.ts +0 -80
- package/src/schema/null.ts +0 -49
- package/src/schema/nullable.test.ts +0 -470
- package/src/schema/nullable.ts +0 -74
- package/src/schema/object.test.ts +0 -69
- package/src/schema/object.ts +0 -136
- package/src/schema/optional.test.ts +0 -479
- package/src/schema/optional.ts +0 -92
- package/src/schema/params.test.ts +0 -1118
- package/src/schema/params.ts +0 -371
- package/src/schema/payload.test.ts +0 -340
- package/src/schema/payload.ts +0 -204
- package/src/schema/permission-set.test.ts +0 -613
- package/src/schema/permission-set.ts +0 -86
- package/src/schema/permission.test.ts +0 -537
- package/src/schema/permission.ts +0 -63
- package/src/schema/procedure.test.ts +0 -324
- package/src/schema/procedure.ts +0 -98
- package/src/schema/query.test.ts +0 -348
- package/src/schema/query.ts +0 -86
- package/src/schema/record.test.ts +0 -812
- package/src/schema/record.ts +0 -217
- package/src/schema/ref.test.ts +0 -349
- package/src/schema/ref.ts +0 -103
- package/src/schema/refine.test.ts +0 -579
- package/src/schema/refine.ts +0 -153
- package/src/schema/regexp.test.ts +0 -577
- package/src/schema/regexp.ts +0 -82
- package/src/schema/string.test.ts +0 -773
- package/src/schema/string.ts +0 -229
- package/src/schema/subscription.test.ts +0 -499
- package/src/schema/subscription.ts +0 -108
- package/src/schema/token.test.ts +0 -152
- package/src/schema/token.ts +0 -103
- package/src/schema/typed-object.test.ts +0 -745
- package/src/schema/typed-object.ts +0 -181
- package/src/schema/typed-ref.test.ts +0 -796
- package/src/schema/typed-ref.ts +0 -126
- package/src/schema/typed-union.test.ts +0 -355
- package/src/schema/typed-union.ts +0 -130
- package/src/schema/union.test.ts +0 -191
- package/src/schema/union.ts +0 -89
- package/src/schema/unknown.test.ts +0 -313
- package/src/schema/unknown.ts +0 -47
- package/src/schema/with-default.ts +0 -81
- package/src/schema.ts +0 -43
- package/src/util/array-agg.test.ts +0 -42
- package/src/util/array-agg.ts +0 -44
- package/src/util/assertion-util.ts +0 -1
- package/src/util/if-any.ts +0 -3
- package/src/util/lazy-property.ts +0 -14
- package/src/util/memoize.ts +0 -37
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -8
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
import { validateCidString } from '@atproto/lex-data'
|
|
2
|
-
import {
|
|
3
|
-
AtIdentifierString,
|
|
4
|
-
AtUriString,
|
|
5
|
-
DatetimeString,
|
|
6
|
-
DidString,
|
|
7
|
-
HandleString,
|
|
8
|
-
NsidString,
|
|
9
|
-
RecordKeyString,
|
|
10
|
-
TidString,
|
|
11
|
-
UriString,
|
|
12
|
-
isAtIdentifierString,
|
|
13
|
-
isAtUriString,
|
|
14
|
-
isDatetimeString,
|
|
15
|
-
isDatetimeStringLenient,
|
|
16
|
-
isValidDid,
|
|
17
|
-
isValidHandle,
|
|
18
|
-
isValidLanguage,
|
|
19
|
-
isValidNsid,
|
|
20
|
-
isValidRecordKey,
|
|
21
|
-
isValidTid,
|
|
22
|
-
isValidUri,
|
|
23
|
-
} from '@atproto/syntax'
|
|
24
|
-
import { CheckFn } from '../util/assertion-util.js'
|
|
25
|
-
|
|
26
|
-
// -----------------------------------------------------------------------------
|
|
27
|
-
// Individual string format types and type guards
|
|
28
|
-
// -----------------------------------------------------------------------------
|
|
29
|
-
|
|
30
|
-
// Re-exporting from @atproto/syntax without modification to preserve types and
|
|
31
|
-
// documentation for types and utilities that are already well-defined there.
|
|
32
|
-
// @TODO rework other string formats in @atproto/syntax to follow this pattern
|
|
33
|
-
// and re-export here, e.g. language tags, NSIDs, record keys, etc.
|
|
34
|
-
|
|
35
|
-
export {
|
|
36
|
-
type AtIdentifierString,
|
|
37
|
-
asAtIdentifierString,
|
|
38
|
-
assertAtIdentifierString,
|
|
39
|
-
ifAtIdentifierString,
|
|
40
|
-
isAtIdentifierString,
|
|
41
|
-
} from '@atproto/syntax'
|
|
42
|
-
|
|
43
|
-
// AtIdentifierString utilities
|
|
44
|
-
export { isDidIdentifier, isHandleIdentifier } from '@atproto/syntax'
|
|
45
|
-
|
|
46
|
-
export {
|
|
47
|
-
type DatetimeString,
|
|
48
|
-
asDatetimeString,
|
|
49
|
-
assertDatetimeString,
|
|
50
|
-
ifDatetimeString,
|
|
51
|
-
isDatetimeString,
|
|
52
|
-
isDatetimeStringLenient,
|
|
53
|
-
} from '@atproto/syntax'
|
|
54
|
-
|
|
55
|
-
// DatetimeString utilities
|
|
56
|
-
export { currentDatetimeString, toDatetimeString } from '@atproto/syntax'
|
|
57
|
-
|
|
58
|
-
export {
|
|
59
|
-
type AtUriString,
|
|
60
|
-
asAtUriString,
|
|
61
|
-
assertAtUriString,
|
|
62
|
-
ifAtUriString,
|
|
63
|
-
isAtUriString,
|
|
64
|
-
} from '@atproto/syntax'
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Lenient version of {@link isAtUriString} that does not enforce the validity
|
|
68
|
-
* of the record key (rkey) path component (if present).
|
|
69
|
-
*
|
|
70
|
-
* @see {@link isAtUriString}
|
|
71
|
-
*/
|
|
72
|
-
export function isAtUriStringLenient<I>(input: I): input is I & AtUriString {
|
|
73
|
-
return isAtUriString(input, { strict: false })
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Type guard that checks if a value is a valid CID string.
|
|
78
|
-
*
|
|
79
|
-
* @param value - The value to check
|
|
80
|
-
* @returns `true` if the value is a valid CID string
|
|
81
|
-
*/
|
|
82
|
-
export const isCidString = ((v) => validateCidString(v)) as CheckFn<CidString>
|
|
83
|
-
/**
|
|
84
|
-
* A Content Identifier (CID) string.
|
|
85
|
-
*
|
|
86
|
-
* CIDs are self-describing content addresses used to identify data by its hash.
|
|
87
|
-
*
|
|
88
|
-
* @example `"bafyreig..."`
|
|
89
|
-
*/
|
|
90
|
-
export type CidString = string
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Type guard that checks if a value is a valid DID string.
|
|
94
|
-
*
|
|
95
|
-
* @param value - The value to check
|
|
96
|
-
* @returns `true` if the value is a valid DID string
|
|
97
|
-
*/
|
|
98
|
-
export const isDidString: CheckFn<DidString> = isValidDid
|
|
99
|
-
export type {
|
|
100
|
-
/**
|
|
101
|
-
* A Decentralized Identifier (DID) string.
|
|
102
|
-
*
|
|
103
|
-
* DIDs are globally unique identifiers that don't require a central authority.
|
|
104
|
-
*
|
|
105
|
-
* @example `"did:plc:1234abcd..."` or `"did:web:example.com"`
|
|
106
|
-
*/
|
|
107
|
-
DidString,
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Type guard that checks if a value is a valid handle string.
|
|
112
|
-
*
|
|
113
|
-
* @param value - The value to check
|
|
114
|
-
* @returns `true` if the value is a valid handle string
|
|
115
|
-
*/
|
|
116
|
-
export const isHandleString: CheckFn<HandleString> = isValidHandle
|
|
117
|
-
export type {
|
|
118
|
-
/**
|
|
119
|
-
* A handle string - a human-readable identifier for users.
|
|
120
|
-
*
|
|
121
|
-
* @example `"alice.bsky.social"` or `"bob.example.com"`
|
|
122
|
-
*/
|
|
123
|
-
HandleString,
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Type guard that checks if a value is a valid BCP-47 language tag.
|
|
128
|
-
*
|
|
129
|
-
* @param value - The value to check
|
|
130
|
-
* @returns `true` if the value is a valid language string
|
|
131
|
-
*/
|
|
132
|
-
export const isLanguageString = isValidLanguage as CheckFn<LanguageString>
|
|
133
|
-
/**
|
|
134
|
-
* A BCP-47 language tag string.
|
|
135
|
-
*
|
|
136
|
-
* @example `"en"`, `"en-US"`, `"zh-Hans"`
|
|
137
|
-
*/
|
|
138
|
-
export type LanguageString = string
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Type guard that checks if a value is a valid NSID string.
|
|
142
|
-
*
|
|
143
|
-
* @param value - The value to check
|
|
144
|
-
* @returns `true` if the value is a valid NSID string
|
|
145
|
-
*/
|
|
146
|
-
export const isNsidString: CheckFn<NsidString> = isValidNsid
|
|
147
|
-
export type {
|
|
148
|
-
/**
|
|
149
|
-
* A Namespaced Identifier (NSID) string identifying a lexicon.
|
|
150
|
-
*
|
|
151
|
-
* NSIDs use reverse-domain notation to identify schemas.
|
|
152
|
-
*
|
|
153
|
-
* @example `"app.bsky.feed.post"`, `"com.atproto.repo.createRecord"`
|
|
154
|
-
*/
|
|
155
|
-
NsidString,
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Type guard that checks if a value is a valid record key string.
|
|
160
|
-
*
|
|
161
|
-
* @param value - The value to check
|
|
162
|
-
* @returns `true` if the value is a valid record key string
|
|
163
|
-
*/
|
|
164
|
-
export const isRecordKeyString: CheckFn<RecordKeyString> = isValidRecordKey
|
|
165
|
-
export type {
|
|
166
|
-
/**
|
|
167
|
-
* A record key string identifying a record within a collection.
|
|
168
|
-
*
|
|
169
|
-
* @example `"3k2..."` (TID format) or `"self"` (literal key)
|
|
170
|
-
*/
|
|
171
|
-
RecordKeyString,
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Type guard that checks if a value is a valid TID string.
|
|
176
|
-
*
|
|
177
|
-
* @param value - The value to check
|
|
178
|
-
* @returns `true` if the value is a valid TID string
|
|
179
|
-
*/
|
|
180
|
-
export const isTidString: CheckFn<TidString> = isValidTid
|
|
181
|
-
export type {
|
|
182
|
-
/**
|
|
183
|
-
* A Timestamp Identifier (TID) string.
|
|
184
|
-
*
|
|
185
|
-
* TIDs are time-based identifiers used for record keys.
|
|
186
|
-
*
|
|
187
|
-
* @example `"3k2..."`
|
|
188
|
-
*/
|
|
189
|
-
TidString,
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Type guard that checks if a value is a valid URI string.
|
|
194
|
-
*
|
|
195
|
-
* @param value - The value to check
|
|
196
|
-
* @returns `true` if the value is a valid URI string
|
|
197
|
-
*/
|
|
198
|
-
export const isUriString: CheckFn<UriString> = isValidUri
|
|
199
|
-
export type {
|
|
200
|
-
/**
|
|
201
|
-
* A standard URI string.
|
|
202
|
-
*
|
|
203
|
-
* @example `"https://example.com/path"`
|
|
204
|
-
*/
|
|
205
|
-
UriString,
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// -----------------------------------------------------------------------------
|
|
209
|
-
// String format registry
|
|
210
|
-
// -----------------------------------------------------------------------------
|
|
211
|
-
|
|
212
|
-
type StringFormats = {
|
|
213
|
-
'at-identifier': AtIdentifierString
|
|
214
|
-
'at-uri': AtUriString
|
|
215
|
-
cid: CidString
|
|
216
|
-
datetime: DatetimeString
|
|
217
|
-
did: DidString
|
|
218
|
-
handle: HandleString
|
|
219
|
-
language: LanguageString
|
|
220
|
-
nsid: NsidString
|
|
221
|
-
'record-key': RecordKeyString
|
|
222
|
-
tid: TidString
|
|
223
|
-
uri: UriString
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Union type of all valid string format names.
|
|
228
|
-
*/
|
|
229
|
-
export type StringFormat = Extract<keyof StringFormats, string>
|
|
230
|
-
|
|
231
|
-
const stringFormatVerifiers: {
|
|
232
|
-
readonly [K in StringFormat]: readonly [
|
|
233
|
-
strict: CheckFn<StringFormats[K]>,
|
|
234
|
-
lenient?: CheckFn<StringFormats[K]>,
|
|
235
|
-
]
|
|
236
|
-
} = /*#__PURE__*/ Object.freeze({
|
|
237
|
-
__proto__: null,
|
|
238
|
-
|
|
239
|
-
'at-identifier': [isAtIdentifierString],
|
|
240
|
-
'at-uri': [isAtUriString, isAtUriStringLenient],
|
|
241
|
-
cid: [isCidString],
|
|
242
|
-
datetime: [isDatetimeString, isDatetimeStringLenient],
|
|
243
|
-
did: [isDidString],
|
|
244
|
-
handle: [isHandleString],
|
|
245
|
-
language: [isLanguageString],
|
|
246
|
-
nsid: [isNsidString],
|
|
247
|
-
'record-key': [isRecordKeyString],
|
|
248
|
-
tid: [isTidString],
|
|
249
|
-
uri: [isUriString],
|
|
250
|
-
})
|
|
251
|
-
|
|
252
|
-
export type StringFormatValidationOptions = {
|
|
253
|
-
/**
|
|
254
|
-
* Allows to be more lenient in validation by using a "lenient" verification
|
|
255
|
-
* function, if available. The behavior of the lenient verifier depends on the
|
|
256
|
-
* specific format, but generally it may allow for a wider range of valid
|
|
257
|
-
* inputs, including values that are not compliant with the AT Protocol
|
|
258
|
-
* specification.
|
|
259
|
-
*
|
|
260
|
-
* @default true
|
|
261
|
-
*/
|
|
262
|
-
strict?: boolean
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Infers the string type for a given format name.
|
|
267
|
-
*
|
|
268
|
-
* @typeParam F - The format name
|
|
269
|
-
*
|
|
270
|
-
* @example
|
|
271
|
-
* ```typescript
|
|
272
|
-
* type Did = InferStringFormat<'did'>
|
|
273
|
-
* // Result: DidString
|
|
274
|
-
* ```
|
|
275
|
-
*/
|
|
276
|
-
export type InferStringFormat<F extends StringFormat> = F extends StringFormat
|
|
277
|
-
? StringFormats[F]
|
|
278
|
-
: never
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Type guard that checks if a string matches a specific format.
|
|
282
|
-
*
|
|
283
|
-
* @typeParam I - The input string type
|
|
284
|
-
* @typeParam F - The format to check
|
|
285
|
-
* @param input - The string to validate
|
|
286
|
-
* @param format - The format name to validate against
|
|
287
|
-
* @returns `true` if the string matches the format
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* ```typescript
|
|
291
|
-
* const value: string = 'did:plc:1234...'
|
|
292
|
-
* if (isStringFormat(value, 'did')) {
|
|
293
|
-
* // value is typed as DidString
|
|
294
|
-
* console.log('Valid DID:', value)
|
|
295
|
-
* }
|
|
296
|
-
* ```
|
|
297
|
-
*/
|
|
298
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
299
|
-
export function isStringFormat<I extends string, F extends StringFormat>(
|
|
300
|
-
input: I,
|
|
301
|
-
format: F,
|
|
302
|
-
options?: StringFormatValidationOptions,
|
|
303
|
-
): input is I & StringFormats[F] {
|
|
304
|
-
const formatVerifier = stringFormatVerifiers[format]
|
|
305
|
-
// Fool-proof
|
|
306
|
-
if (!formatVerifier) throw new TypeError(`Unknown string format: ${format}`)
|
|
307
|
-
|
|
308
|
-
const check: CheckFn<StringFormats[F]> =
|
|
309
|
-
options?.strict === false && formatVerifier.length > 1
|
|
310
|
-
? formatVerifier[1]!
|
|
311
|
-
: formatVerifier[0]
|
|
312
|
-
|
|
313
|
-
return check(input)
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Asserts that a string matches a specific format, throwing if invalid.
|
|
318
|
-
*
|
|
319
|
-
* @typeParam I - The input string type
|
|
320
|
-
* @typeParam F - The format to check
|
|
321
|
-
* @param input - The string to validate
|
|
322
|
-
* @param format - The format name to validate against
|
|
323
|
-
* @throws {TypeError} If the string doesn't match the format
|
|
324
|
-
*
|
|
325
|
-
* @example
|
|
326
|
-
* ```typescript
|
|
327
|
-
* assertStringFormat(value, 'handle')
|
|
328
|
-
* // value is now typed as HandleString
|
|
329
|
-
* ```
|
|
330
|
-
*/
|
|
331
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
332
|
-
export function assertStringFormat<I extends string, F extends StringFormat>(
|
|
333
|
-
input: I,
|
|
334
|
-
format: F,
|
|
335
|
-
options?: StringFormatValidationOptions,
|
|
336
|
-
): asserts input is I & StringFormats[F] {
|
|
337
|
-
if (!isStringFormat(input, format, options)) {
|
|
338
|
-
throw new TypeError(`Invalid string format (${format}): ${input}`)
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Validates and returns a string as the specified format type, throwing if invalid.
|
|
344
|
-
*
|
|
345
|
-
* This is useful when you need to convert a string to a format type in an expression.
|
|
346
|
-
*
|
|
347
|
-
* @typeParam I - The input string type
|
|
348
|
-
* @typeParam F - The format to validate against
|
|
349
|
-
* @param input - The string to validate
|
|
350
|
-
* @param format - The format name to validate against
|
|
351
|
-
* @returns The input typed as the format type
|
|
352
|
-
* @throws {TypeError} If the string doesn't match the format
|
|
353
|
-
*
|
|
354
|
-
* @example
|
|
355
|
-
* ```typescript
|
|
356
|
-
* const did = asStringFormat(userInput, 'did')
|
|
357
|
-
* // did is typed as DidString
|
|
358
|
-
* ```
|
|
359
|
-
*/
|
|
360
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
361
|
-
export function asStringFormat<I extends string, F extends StringFormat>(
|
|
362
|
-
input: I,
|
|
363
|
-
format: F,
|
|
364
|
-
options?: StringFormatValidationOptions,
|
|
365
|
-
): I & StringFormats[F] {
|
|
366
|
-
assertStringFormat(input, format, options)
|
|
367
|
-
return input
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* Returns the string as the format type if valid, otherwise returns `undefined`.
|
|
372
|
-
*
|
|
373
|
-
* This is useful for optional validation where you want to handle invalid values
|
|
374
|
-
* without throwing.
|
|
375
|
-
*
|
|
376
|
-
* @typeParam I - The input string type
|
|
377
|
-
* @typeParam F - The format to validate against
|
|
378
|
-
* @param input - The string to validate
|
|
379
|
-
* @param format - The format name to validate against
|
|
380
|
-
* @returns The typed string if valid, otherwise `undefined`
|
|
381
|
-
*
|
|
382
|
-
* @example
|
|
383
|
-
* ```typescript
|
|
384
|
-
* const did = ifStringFormat(maybeInvalid, 'did')
|
|
385
|
-
* if (did) {
|
|
386
|
-
* // did is typed as DidString
|
|
387
|
-
* }
|
|
388
|
-
* ```
|
|
389
|
-
*/
|
|
390
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
391
|
-
export function ifStringFormat<I extends string, F extends StringFormat>(
|
|
392
|
-
input: I,
|
|
393
|
-
format: F,
|
|
394
|
-
options?: StringFormatValidationOptions,
|
|
395
|
-
): undefined | (I & StringFormats[F]) {
|
|
396
|
-
return isStringFormat(input, format, options) ? input : undefined
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Array of all valid string format names.
|
|
401
|
-
*
|
|
402
|
-
* @example
|
|
403
|
-
* ```typescript
|
|
404
|
-
* for (const format of STRING_FORMATS) {
|
|
405
|
-
* console.log(format) // 'at-identifier', 'at-uri', 'cid', ...
|
|
406
|
-
* }
|
|
407
|
-
* ```
|
|
408
|
-
*/
|
|
409
|
-
export const STRING_FORMATS = /*#__PURE__*/ Object.freeze(
|
|
410
|
-
/*#__PURE__*/ Object.keys(stringFormatVerifiers),
|
|
411
|
-
) as readonly StringFormat[]
|
package/src/core/types.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Same as `string` but prevents TypeScript from allowing union types to
|
|
3
|
-
* be widened to `string` in IDEs.
|
|
4
|
-
*
|
|
5
|
-
* This is useful when you want autocompletion for known string values
|
|
6
|
-
* while still allowing arbitrary strings.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* // With plain string, union is widened:
|
|
11
|
-
* type Status1 = 'active' | 'inactive' | string // just becomes "string"
|
|
12
|
-
*
|
|
13
|
-
* // With UnknownString, union is preserved:
|
|
14
|
-
* type Status2 = 'active' | 'inactive' | UnknownString
|
|
15
|
-
* // Autocomplete will suggest 'active' and 'inactive'
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export type UnknownString = string & NonNullable<unknown>
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Simplifies a type by expanding intersections and mapped types.
|
|
22
|
-
*
|
|
23
|
-
* This improves IDE tooltips by showing the actual shape of a type
|
|
24
|
-
* rather than complex intersections.
|
|
25
|
-
*
|
|
26
|
-
* @typeParam T - The type to simplify
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* type Complex = { a: string } & { b: number }
|
|
31
|
-
* type Simple = Simplify<Complex>
|
|
32
|
-
* // Hover shows: { a: string; b: number }
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export type Simplify<T> = { [K in keyof T]: T[K] } & NonNullable<unknown>
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Internal symbol for branding restricted types.
|
|
39
|
-
* @internal
|
|
40
|
-
*/
|
|
41
|
-
declare const __restricted: unique symbol
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* A type that represents a value that cannot be used, with a custom
|
|
45
|
-
* message explaining the restriction.
|
|
46
|
-
*
|
|
47
|
-
* This is useful for creating "never use this" types that provide
|
|
48
|
-
* helpful error messages when someone tries to use them.
|
|
49
|
-
*
|
|
50
|
-
* @typeParam Message - A string literal type containing the error message
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```typescript
|
|
54
|
-
* type DeprecatedField = Restricted<'This field has been deprecated. Use newField instead.'>
|
|
55
|
-
*
|
|
56
|
-
* interface MyType {
|
|
57
|
-
* oldField?: DeprecatedField
|
|
58
|
-
* newField: string
|
|
59
|
-
* }
|
|
60
|
-
*
|
|
61
|
-
* const obj: MyType = {
|
|
62
|
-
* oldField: 'value', // Error: Type 'string' is not assignable to type 'Restricted<...>'
|
|
63
|
-
* newField: 'value'
|
|
64
|
-
* }
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export type Restricted<Message extends string> = typeof __restricted & {
|
|
68
|
-
[__restricted]: Message
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Converts all properties of `P` that may be `undefined` into actual
|
|
73
|
-
* optional properties on the resulting type.
|
|
74
|
-
*
|
|
75
|
-
* This is useful when working with types where some properties are typed as
|
|
76
|
-
* `T | undefined` but should really be optional (`T?`).
|
|
77
|
-
*
|
|
78
|
-
* @typeParam P - The object type to transform
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```typescript
|
|
82
|
-
* type Input = {
|
|
83
|
-
* required: string
|
|
84
|
-
* optional: string | undefined
|
|
85
|
-
* }
|
|
86
|
-
*
|
|
87
|
-
* type Output = WithOptionalProperties<Input>
|
|
88
|
-
* // Result: {
|
|
89
|
-
* // required: string
|
|
90
|
-
* // optional?: string | undefined
|
|
91
|
-
* // }
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
export type WithOptionalProperties<P> = Simplify<
|
|
95
|
-
{
|
|
96
|
-
-readonly [K in keyof P as undefined extends P[K] ? never : K]-?: P[K]
|
|
97
|
-
} & {
|
|
98
|
-
-readonly [K in keyof P as undefined extends P[K] ? K : never]?: P[K]
|
|
99
|
-
}
|
|
100
|
-
>
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Creates a type by omitting a specific key from an object type.
|
|
104
|
-
*
|
|
105
|
-
* Similar to TypeScript's built-in `Omit`, but preserves the type structure
|
|
106
|
-
* more accurately in some edge cases.
|
|
107
|
-
*
|
|
108
|
-
* @typeParam T - The object type to transform
|
|
109
|
-
* @typeParam K - The key to omit (must be a key of T)
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```typescript
|
|
113
|
-
* type Person = { name: string; age: number; email: string }
|
|
114
|
-
* type PersonWithoutEmail = OmitKey<Person, 'email'>
|
|
115
|
-
* // Result: { name: string; age: number }
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
export type OmitKey<T, K extends keyof T> = {
|
|
119
|
-
[K2 in keyof T as K2 extends K ? never : K2]: T[K2]
|
|
120
|
-
}
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { LexError } from '@atproto/lex-data'
|
|
2
|
-
import { arrayAgg } from '../util/array-agg.js'
|
|
3
|
-
import { ResultFailure } from './result.js'
|
|
4
|
-
import {
|
|
5
|
-
Issue,
|
|
6
|
-
IssueInvalidType,
|
|
7
|
-
IssueInvalidValue,
|
|
8
|
-
} from './validation-issue.js'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Error thrown when validation fails.
|
|
12
|
-
*
|
|
13
|
-
* Contains detailed information about all validation issues encountered,
|
|
14
|
-
* including the path to each invalid value and descriptions of what was
|
|
15
|
-
* expected vs what was received.
|
|
16
|
-
*
|
|
17
|
-
* Extends {@link LexError} with the error name "InvalidRequest" for
|
|
18
|
-
* consistency with the AT Protocol error handling conventions.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* const error = new LexValidationError([
|
|
23
|
-
* new IssueInvalidType(['user', 'age'], 'hello', ['number'])
|
|
24
|
-
* ])
|
|
25
|
-
* console.log(error.message)
|
|
26
|
-
* // "Expected integer value type (got "some-string") at $.user.age"
|
|
27
|
-
*
|
|
28
|
-
* console.log(error.issues.length) // 1
|
|
29
|
-
* console.log(error.toJSON())
|
|
30
|
-
* // { error: 'InvalidRequest', message: '...', issues: [...] }
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @note this class implements {@link ResultFailure} to allow it to be used
|
|
34
|
-
* directly as a failure reason in validation results, avoiding the need for
|
|
35
|
-
* wrapping it in an additional object.
|
|
36
|
-
*/
|
|
37
|
-
export class LexValidationError
|
|
38
|
-
extends LexError<'InvalidRequest'>
|
|
39
|
-
implements ResultFailure<LexValidationError>
|
|
40
|
-
{
|
|
41
|
-
name = 'LexValidationError'
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* The list of validation issues that caused this error.
|
|
45
|
-
*
|
|
46
|
-
* Issues are aggregated when possible (e.g., multiple invalid type issues
|
|
47
|
-
* at the same path are combined into a single issue listing all expected types).
|
|
48
|
-
*/
|
|
49
|
-
readonly issues: readonly Issue[]
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Creates a new validation error from a list of issues.
|
|
53
|
-
*
|
|
54
|
-
* Issues are automatically aggregated to combine related issues at the same
|
|
55
|
-
* path (e.g., multiple type expectations from a union schema).
|
|
56
|
-
*
|
|
57
|
-
* @param issues - The validation issues that caused this error
|
|
58
|
-
* @param options - Standard Error options (e.g., `cause`)
|
|
59
|
-
*/
|
|
60
|
-
constructor(issues: Issue[], options?: ErrorOptions) {
|
|
61
|
-
const issuesAgg = aggregateIssues(issues)
|
|
62
|
-
super('InvalidRequest', issuesAgg.join(', '), options)
|
|
63
|
-
this.issues = issuesAgg
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** @see {ResultFailure.success} */
|
|
67
|
-
readonly success = false as const
|
|
68
|
-
|
|
69
|
-
/** @see {ResultFailure.reason} */
|
|
70
|
-
get reason() {
|
|
71
|
-
return this
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Converts the error to a JSON-serializable object.
|
|
76
|
-
*
|
|
77
|
-
* @returns An object containing the error details and issues details
|
|
78
|
-
*/
|
|
79
|
-
override toJSON() {
|
|
80
|
-
return {
|
|
81
|
-
...super.toJSON(),
|
|
82
|
-
issues: this.issues.map((issue) => issue.toJSON()),
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function aggregateIssues(issues: Issue[]): Issue[] {
|
|
88
|
-
// Quick path for common cases
|
|
89
|
-
if (issues.length <= 1) return issues
|
|
90
|
-
if (issues.length === 2 && issues[0].code !== issues[1].code) return issues
|
|
91
|
-
|
|
92
|
-
return [
|
|
93
|
-
// Aggregate invalid_type with identical paths
|
|
94
|
-
...arrayAgg(
|
|
95
|
-
issues.filter((issue) => issue instanceof IssueInvalidType),
|
|
96
|
-
(a, b) => comparePropertyPaths(a.path, b.path),
|
|
97
|
-
(issues) =>
|
|
98
|
-
new IssueInvalidType(
|
|
99
|
-
issues[0].path,
|
|
100
|
-
issues[0].input,
|
|
101
|
-
Array.from(new Set(issues.flatMap((iss) => iss.expected))),
|
|
102
|
-
),
|
|
103
|
-
),
|
|
104
|
-
// Aggregate invalid_value with identical paths
|
|
105
|
-
...arrayAgg(
|
|
106
|
-
issues.filter((issue) => issue instanceof IssueInvalidValue),
|
|
107
|
-
(a, b) => comparePropertyPaths(a.path, b.path),
|
|
108
|
-
(issues) =>
|
|
109
|
-
new IssueInvalidValue(
|
|
110
|
-
issues[0].path,
|
|
111
|
-
issues[0].input,
|
|
112
|
-
Array.from(new Set(issues.flatMap((iss) => iss.values))),
|
|
113
|
-
),
|
|
114
|
-
),
|
|
115
|
-
// Pass through other issues
|
|
116
|
-
...issues.filter(
|
|
117
|
-
(issue) =>
|
|
118
|
-
!(issue instanceof IssueInvalidType) &&
|
|
119
|
-
!(issue instanceof IssueInvalidValue),
|
|
120
|
-
),
|
|
121
|
-
]
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
125
|
-
function comparePropertyPaths(
|
|
126
|
-
a: readonly PropertyKey[],
|
|
127
|
-
b: readonly PropertyKey[],
|
|
128
|
-
) {
|
|
129
|
-
if (a.length !== b.length) return false
|
|
130
|
-
for (let i = 0; i < a.length; i++) {
|
|
131
|
-
if (a[i] !== b[i]) return false
|
|
132
|
-
}
|
|
133
|
-
return true
|
|
134
|
-
}
|