-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathtypes.ts
437 lines (386 loc) · 10.5 KB
/
types.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import { z } from 'zod'
import { NSID } from '@atproto/syntax'
import { requiredPropertiesRefinement } from './util'
// primitives
// =
export const lexBoolean = z
.object({
type: z.literal('boolean'),
description: z.string().optional(),
default: z.boolean().optional(),
const: z.boolean().optional(),
})
.strict()
export type LexBoolean = z.infer<typeof lexBoolean>
export const lexInteger = z
.object({
type: z.literal('integer'),
description: z.string().optional(),
default: z.number().int().optional(),
minimum: z.number().int().optional(),
maximum: z.number().int().optional(),
enum: z.number().int().array().optional(),
const: z.number().int().optional(),
})
.strict()
export type LexInteger = z.infer<typeof lexInteger>
export const lexStringFormat = z.enum([
'datetime',
'uri',
'at-uri',
'did',
'handle',
'at-identifier',
'nsid',
'cid',
'language',
'tid',
'record-key',
])
export type LexStringFormat = z.infer<typeof lexStringFormat>
export const lexString = z
.object({
type: z.literal('string'),
format: lexStringFormat.optional(),
description: z.string().optional(),
default: z.string().optional(),
minLength: z.number().int().optional(),
maxLength: z.number().int().optional(),
minGraphemes: z.number().int().optional(),
maxGraphemes: z.number().int().optional(),
enum: z.string().array().optional(),
const: z.string().optional(),
knownValues: z.string().array().optional(),
})
.strict()
export type LexString = z.infer<typeof lexString>
export const lexUnknown = z
.object({
type: z.literal('unknown'),
description: z.string().optional(),
})
.strict()
export type LexUnknown = z.infer<typeof lexUnknown>
export const lexPrimitive = z.discriminatedUnion('type', [
lexBoolean,
lexInteger,
lexString,
lexUnknown,
])
export type LexPrimitive = z.infer<typeof lexPrimitive>
// ipld types
// =
export const lexBytes = z
.object({
type: z.literal('bytes'),
description: z.string().optional(),
maxLength: z.number().optional(),
minLength: z.number().optional(),
})
.strict()
export type LexBytes = z.infer<typeof lexBytes>
export const lexCidLink = z
.object({
type: z.literal('cid-link'),
description: z.string().optional(),
})
.strict()
export type LexCidLink = z.infer<typeof lexCidLink>
export const lexIpldType = z.discriminatedUnion('type', [lexBytes, lexCidLink])
export type LexIpldType = z.infer<typeof lexIpldType>
// references
// =
export const lexRef = z
.object({
type: z.literal('ref'),
description: z.string().optional(),
ref: z.string(),
})
.strict()
export type LexRef = z.infer<typeof lexRef>
export const lexRefUnion = z
.object({
type: z.literal('union'),
description: z.string().optional(),
refs: z.string().array(),
closed: z.boolean().optional(),
})
.strict()
export type LexRefUnion = z.infer<typeof lexRefUnion>
export const lexRefVariant = z.discriminatedUnion('type', [lexRef, lexRefUnion])
export type LexRefVariant = z.infer<typeof lexRefVariant>
// blobs
// =
export const lexBlob = z
.object({
type: z.literal('blob'),
description: z.string().optional(),
accept: z.string().array().optional(),
maxSize: z.number().optional(),
})
.strict()
export type LexBlob = z.infer<typeof lexBlob>
// complex types
// =
export const lexArray = z
.object({
type: z.literal('array'),
description: z.string().optional(),
items: z.union([lexPrimitive, lexIpldType, lexBlob, lexRefVariant]),
minLength: z.number().int().optional(),
maxLength: z.number().int().optional(),
})
.strict()
export type LexArray = z.infer<typeof lexArray>
export const lexPrimitiveArray = lexArray.merge(
z
.object({
items: lexPrimitive,
})
.strict(),
)
export type LexPrimitiveArray = z.infer<typeof lexPrimitiveArray>
export const lexToken = z
.object({
type: z.literal('token'),
description: z.string().optional(),
})
.strict()
export type LexToken = z.infer<typeof lexToken>
export const lexObject = z
.object({
type: z.literal('object'),
description: z.string().optional(),
required: z.string().array().optional(),
nullable: z.string().array().optional(),
properties: z.record(
z.union([lexRefVariant, lexIpldType, lexArray, lexBlob, lexPrimitive]),
),
})
.strict()
.superRefine(requiredPropertiesRefinement)
export type LexObject = z.infer<typeof lexObject>
// xrpc
// =
export const lexXrpcParameters = z
.object({
type: z.literal('params'),
description: z.string().optional(),
required: z.string().array().optional(),
properties: z.record(z.union([lexPrimitive, lexPrimitiveArray])),
})
.strict()
.superRefine(requiredPropertiesRefinement)
export type LexXrpcParameters = z.infer<typeof lexXrpcParameters>
export const lexXrpcBody = z
.object({
description: z.string().optional(),
encoding: z.string(),
schema: z.union([lexRefVariant, lexObject]).optional(),
})
.strict()
export type LexXrpcBody = z.infer<typeof lexXrpcBody>
export const lexXrpcSubscriptionMessage = z
.object({
description: z.string().optional(),
schema: z.union([lexRefVariant, lexObject]).optional(),
})
.strict()
export type LexXrpcSubscriptionMessage = z.infer<
typeof lexXrpcSubscriptionMessage
>
export const lexXrpcError = z
.object({
name: z.string(),
description: z.string().optional(),
})
.strict()
export type LexXrpcError = z.infer<typeof lexXrpcError>
export const lexXrpcQuery = z
.object({
type: z.literal('query'),
description: z.string().optional(),
parameters: lexXrpcParameters.optional(),
output: lexXrpcBody.optional(),
errors: lexXrpcError.array().optional(),
})
.strict()
export type LexXrpcQuery = z.infer<typeof lexXrpcQuery>
export const lexXrpcProcedure = z
.object({
type: z.literal('procedure'),
description: z.string().optional(),
parameters: lexXrpcParameters.optional(),
input: lexXrpcBody.optional(),
output: lexXrpcBody.optional(),
errors: lexXrpcError.array().optional(),
})
.strict()
export type LexXrpcProcedure = z.infer<typeof lexXrpcProcedure>
export const lexXrpcSubscription = z
.object({
type: z.literal('subscription'),
description: z.string().optional(),
parameters: lexXrpcParameters.optional(),
message: lexXrpcSubscriptionMessage.optional(),
errors: lexXrpcError.array().optional(),
})
.strict()
export type LexXrpcSubscription = z.infer<typeof lexXrpcSubscription>
// database
// =
export const lexRecord = z
.object({
type: z.literal('record'),
description: z.string().optional(),
key: z.string().optional(),
record: lexObject,
})
.strict()
export type LexRecord = z.infer<typeof lexRecord>
// core
// =
// We need to use `z.custom` here because
// lexXrpcProperty and lexObject are refined
// `z.union` would work, but it's too slow
// see #915 for details
export const lexUserType = z.custom<
| LexRecord
| LexXrpcQuery
| LexXrpcProcedure
| LexXrpcSubscription
| LexBlob
| LexArray
| LexToken
| LexObject
| LexBoolean
| LexInteger
| LexString
| LexBytes
| LexCidLink
| LexUnknown
>(
(val) => {
if (!val || typeof val !== 'object') {
return
}
if (val['type'] === undefined) {
return
}
switch (val['type']) {
case 'record':
return lexRecord.parse(val)
case 'query':
return lexXrpcQuery.parse(val)
case 'procedure':
return lexXrpcProcedure.parse(val)
case 'subscription':
return lexXrpcSubscription.parse(val)
case 'blob':
return lexBlob.parse(val)
case 'array':
return lexArray.parse(val)
case 'token':
return lexToken.parse(val)
case 'object':
return lexObject.parse(val)
case 'boolean':
return lexBoolean.parse(val)
case 'integer':
return lexInteger.parse(val)
case 'string':
return lexString.parse(val)
case 'bytes':
return lexBytes.parse(val)
case 'cid-link':
return lexCidLink.parse(val)
case 'unknown':
return lexUnknown.parse(val)
}
},
(val) => {
if (!val || typeof val !== 'object') {
return {
message: 'Must be an object',
fatal: true,
}
}
if (val['type'] === undefined) {
return {
message: 'Must have a type',
fatal: true,
}
}
return {
message: `Invalid type: ${val['type']} must be one of: record, query, procedure, subscription, blob, array, token, object, boolean, integer, string, bytes, cid-link, unknown`,
fatal: true,
}
},
)
export type LexUserType = z.infer<typeof lexUserType>
export const lexiconDoc = z
.object({
lexicon: z.literal(1),
id: z.string().refine((v: string) => NSID.isValid(v), {
message: 'Must be a valid NSID',
}),
revision: z.number().optional(),
description: z.string().optional(),
defs: z.record(lexUserType),
})
.strict()
.superRefine((doc, ctx) => {
for (const defId in doc.defs) {
const def = doc.defs[defId]
if (
defId !== 'main' &&
(def.type === 'record' ||
def.type === 'procedure' ||
def.type === 'query' ||
def.type === 'subscription')
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Records, procedures, queries, and subscriptions must be the main definition.`,
})
}
}
})
export type LexiconDoc = z.infer<typeof lexiconDoc>
// helpers
// =
export function isValidLexiconDoc(v: unknown): v is LexiconDoc {
return lexiconDoc.safeParse(v).success
}
export function isObj(obj: unknown): obj is Record<string, unknown> {
return obj !== null && typeof obj === 'object'
}
export function hasProp<K extends PropertyKey>(
data: object,
prop: K,
): data is Record<K, unknown> {
return prop in data
}
export const discriminatedObject = z.object({ $type: z.string() })
export type DiscriminatedObject = z.infer<typeof discriminatedObject>
export function isDiscriminatedObject(
value: unknown,
): value is DiscriminatedObject {
return discriminatedObject.safeParse(value).success
}
export function parseLexiconDoc(v: unknown): LexiconDoc {
lexiconDoc.parse(v)
return v as LexiconDoc
}
export type ValidationResult =
| {
success: true
value: unknown
}
| {
success: false
error: ValidationError
}
export class ValidationError extends Error {}
export class InvalidLexiconError extends Error {}
export class LexiconDefNotFoundError extends Error {}