@atproto/lex-document 0.0.10 → 0.0.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lex-document",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "license": "MIT",
5
5
  "description": "Lexicon document validation tools for AT",
6
6
  "keywords": [
@@ -38,11 +38,11 @@
38
38
  "dependencies": {
39
39
  "core-js": "^3",
40
40
  "tslib": "^2.8.1",
41
- "@atproto/lex-schema": "0.0.9"
41
+ "@atproto/lex-schema": "0.0.10"
42
42
  },
43
43
  "devDependencies": {
44
44
  "vitest": "^4.0.16",
45
- "@atproto/lex-data": "0.0.8"
45
+ "@atproto/lex-data": "0.0.9"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsc --build tsconfig.build.json",
@@ -1,74 +1,62 @@
1
1
  import { l } from '@atproto/lex-schema'
2
2
 
3
- // Re-usable shortcuts (avoid creating too many schemas)
4
-
5
- const bool = l.boolean()
6
- const int = l.integer()
7
- const str = l.string()
8
-
9
- const boopOpt = l.optional(bool)
10
- const intOpt = l.optional(int)
11
- const strOpt = l.optional(str)
12
-
13
- const strArrOpt = l.optional(l.array(str))
14
-
15
3
  // https://atproto.com/specs/lexicon
16
4
 
17
5
  // "Concrete" Types
18
6
 
19
7
  export const lexiconBooleanSchema = l.object({
20
8
  type: l.literal('boolean'),
21
- default: boopOpt,
22
- const: boopOpt,
23
- description: strOpt,
9
+ default: l.optional(l.boolean()),
10
+ const: l.optional(l.boolean()),
11
+ description: l.optional(l.string()),
24
12
  })
25
13
  export type LexiconBoolean = l.Infer<typeof lexiconBooleanSchema>
26
14
 
27
15
  export const lexiconIntegerSchema = l.object({
28
16
  type: l.literal('integer'),
29
- default: intOpt,
30
- minimum: intOpt,
31
- maximum: intOpt,
17
+ default: l.optional(l.integer()),
18
+ minimum: l.optional(l.integer()),
19
+ maximum: l.optional(l.integer()),
32
20
  enum: l.optional(l.array(l.integer())),
33
- const: intOpt,
34
- description: strOpt,
21
+ const: l.optional(l.integer()),
22
+ description: l.optional(l.string()),
35
23
  })
36
24
  export type LexiconInteger = l.Infer<typeof lexiconIntegerSchema>
37
25
 
38
26
  export const lexiconStringSchema = l.object({
39
27
  type: l.literal('string'),
40
28
  format: l.optional(l.enum<l.StringFormat>(l.STRING_FORMATS)),
41
- default: strOpt,
42
- minLength: intOpt,
43
- maxLength: intOpt,
44
- minGraphemes: intOpt,
45
- maxGraphemes: intOpt,
46
- enum: strArrOpt,
47
- const: strOpt,
48
- knownValues: strArrOpt,
49
- description: strOpt,
29
+ default: l.optional(l.string()),
30
+ minLength: l.optional(l.integer()),
31
+ maxLength: l.optional(l.integer()),
32
+ minGraphemes: l.optional(l.integer()),
33
+ maxGraphemes: l.optional(l.integer()),
34
+ enum: l.optional(l.array(l.string())),
35
+ const: l.optional(l.string()),
36
+ knownValues: l.optional(l.array(l.string())),
37
+ description: l.optional(l.string()),
50
38
  })
51
39
  export type LexiconString = l.Infer<typeof lexiconStringSchema>
52
40
 
53
41
  export const lexiconBytesSchema = l.object({
54
42
  type: l.literal('bytes'),
55
- maxLength: intOpt,
56
- minLength: intOpt,
57
- description: strOpt,
43
+ maxLength: l.optional(l.integer()),
44
+ minLength: l.optional(l.integer()),
45
+ description: l.optional(l.string()),
58
46
  })
59
47
  export type LexiconBytes = l.Infer<typeof lexiconBytesSchema>
60
48
 
61
49
  export const lexiconCidLinkSchema = l.object({
62
50
  type: l.literal('cid-link'),
63
- description: strOpt,
51
+ description: l.optional(l.string()),
64
52
  })
65
53
  export type LexiconCid = l.Infer<typeof lexiconCidLinkSchema>
66
54
 
67
55
  export const lexiconBlobSchema = l.object({
68
56
  type: l.literal('blob'),
69
- accept: strArrOpt,
70
- maxSize: intOpt,
71
- description: strOpt,
57
+ accept: l.optional(l.array(l.string())),
58
+ maxSize: l.optional(l.integer()),
59
+ description: l.optional(l.string()),
72
60
  })
73
61
  export type LexiconBlob = l.Infer<typeof lexiconBlobSchema>
74
62
 
@@ -87,28 +75,28 @@ const CONCRETE_TYPES = [
87
75
 
88
76
  export const lexiconUnknownSchema = l.object({
89
77
  type: l.literal('unknown'),
90
- description: strOpt,
78
+ description: l.optional(l.string()),
91
79
  })
92
80
  export type LexiconUnknown = l.Infer<typeof lexiconUnknownSchema>
93
81
 
94
82
  export const lexiconTokenSchema = l.object({
95
83
  type: l.literal('token'),
96
- description: strOpt,
84
+ description: l.optional(l.string()),
97
85
  })
98
86
  export type LexiconToken = l.Infer<typeof lexiconTokenSchema>
99
87
 
100
88
  export const lexiconRefSchema = l.object({
101
89
  type: l.literal('ref'),
102
- ref: str,
103
- description: strOpt,
90
+ ref: l.string(),
91
+ description: l.optional(l.string()),
104
92
  })
105
93
  export type LexiconRef = l.Infer<typeof lexiconRefSchema>
106
94
 
107
95
  export const lexiconRefUnionSchema = l.object({
108
96
  type: l.literal('union'),
109
- refs: l.array(str),
110
- closed: boopOpt,
111
- description: strOpt,
97
+ refs: l.array(l.string()),
98
+ closed: l.optional(l.boolean()),
99
+ description: l.optional(l.string()),
112
100
  })
113
101
  export type LexiconRefUnion = l.Infer<typeof lexiconRefUnionSchema>
114
102
 
@@ -127,9 +115,9 @@ export type LexiconArrayItems = l.Infer<(typeof ARRAY_ITEMS_SCHEMAS)[number]>
127
115
  export const lexiconArraySchema = l.object({
128
116
  type: l.literal('array'),
129
117
  items: l.discriminatedUnion('type', ARRAY_ITEMS_SCHEMAS),
130
- minLength: intOpt,
131
- maxLength: intOpt,
132
- description: strOpt,
118
+ minLength: l.optional(l.integer()),
119
+ maxLength: l.optional(l.integer()),
120
+ description: l.optional(l.string()),
133
121
  })
134
122
  export type LexiconArray = l.Infer<typeof lexiconArraySchema>
135
123
 
@@ -146,15 +134,15 @@ export const lexiconObjectSchema = l.refine(
146
134
  l.object({
147
135
  type: l.literal('object'),
148
136
  properties: l.dict(
149
- str,
137
+ l.string(),
150
138
  l.discriminatedUnion('type', [
151
139
  ...ARRAY_ITEMS_SCHEMAS,
152
140
  lexiconArraySchema,
153
141
  ]),
154
142
  ),
155
- required: strArrOpt,
156
- nullable: strArrOpt,
157
- description: strOpt,
143
+ required: l.optional(l.array(l.string())),
144
+ nullable: l.optional(l.array(l.string())),
145
+ description: l.optional(l.string()),
158
146
  }),
159
147
  requirePropertiesRefinement,
160
148
  )
@@ -172,7 +160,7 @@ export type LexiconRecordKey = l.LexiconRecordKey
172
160
  export const lexiconRecordSchema = l.object({
173
161
  type: l.literal('record'),
174
162
  record: lexiconObjectSchema,
175
- description: strOpt,
163
+ description: l.optional(l.string()),
176
164
  key: lexiconRecordKeySchema,
177
165
  })
178
166
  export type LexiconRecord = l.Infer<typeof lexiconRecordSchema>
@@ -183,7 +171,7 @@ export const lexiconParameters = l.refine(
183
171
  l.object({
184
172
  type: l.literal('params'),
185
173
  properties: l.dict(
186
- str,
174
+ l.string(),
187
175
  l.discriminatedUnion('type', [
188
176
  lexiconBooleanSchema,
189
177
  lexiconIntegerSchema,
@@ -195,21 +183,21 @@ export const lexiconParameters = l.refine(
195
183
  lexiconIntegerSchema,
196
184
  lexiconStringSchema,
197
185
  ]),
198
- minLength: intOpt,
199
- maxLength: intOpt,
200
- description: strOpt,
186
+ minLength: l.optional(l.integer()),
187
+ maxLength: l.optional(l.integer()),
188
+ description: l.optional(l.string()),
201
189
  }),
202
190
  ]),
203
191
  ),
204
- required: strArrOpt,
205
- description: strOpt,
192
+ required: l.optional(l.array(l.string())),
193
+ description: l.optional(l.string()),
206
194
  }),
207
195
  requirePropertiesRefinement,
208
196
  )
209
197
  export type LexiconParameters = l.Infer<typeof lexiconParameters>
210
198
 
211
199
  export const lexiconPayload = l.object({
212
- encoding: str,
200
+ encoding: l.string(),
213
201
  schema: l.optional(
214
202
  l.discriminatedUnion('type', [
215
203
  lexiconRefSchema,
@@ -217,13 +205,13 @@ export const lexiconPayload = l.object({
217
205
  lexiconObjectSchema,
218
206
  ]),
219
207
  ),
220
- description: strOpt,
208
+ description: l.optional(l.string()),
221
209
  })
222
210
  export type LexiconPayload = l.Infer<typeof lexiconPayload>
223
211
 
224
212
  export const lexiconError = l.object({
225
213
  name: l.string({ minLength: 1 }),
226
- description: strOpt,
214
+ description: l.optional(l.string()),
227
215
  })
228
216
  export type LexiconError = l.Infer<typeof lexiconError>
229
217
 
@@ -232,7 +220,7 @@ export const lexiconQuerySchema = l.object({
232
220
  parameters: l.optional(lexiconParameters),
233
221
  output: l.optional(lexiconPayload),
234
222
  errors: l.optional(l.array(lexiconError)),
235
- description: strOpt,
223
+ description: l.optional(l.string()),
236
224
  })
237
225
  export type LexiconQuery = l.Infer<typeof lexiconQuerySchema>
238
226
 
@@ -242,16 +230,16 @@ export const lexiconProcedureSchema = l.object({
242
230
  input: l.optional(lexiconPayload),
243
231
  output: l.optional(lexiconPayload),
244
232
  errors: l.optional(l.array(lexiconError)),
245
- description: strOpt,
233
+ description: l.optional(l.string()),
246
234
  })
247
235
  export type LexiconProcedure = l.Infer<typeof lexiconProcedureSchema>
248
236
 
249
237
  export const lexiconSubscriptionSchema = l.object({
250
238
  type: l.literal('subscription'),
251
- description: strOpt,
239
+ description: l.optional(l.string()),
252
240
  parameters: l.optional(lexiconParameters),
253
241
  message: l.object({
254
- description: strOpt,
242
+ description: l.optional(l.string()),
255
243
  schema: lexiconRefUnionSchema,
256
244
  }),
257
245
  errors: l.optional(l.array(lexiconError)),
@@ -265,7 +253,7 @@ const lexiconLanguageSchema = l.string({ format: 'language' })
265
253
 
266
254
  export type LexiconLanguage = l.Infer<typeof lexiconLanguageSchema>
267
255
 
268
- const lexiconLanguageDict = l.dict(lexiconLanguageSchema, str)
256
+ const lexiconLanguageDict = l.dict(lexiconLanguageSchema, l.string())
269
257
 
270
258
  export type LexiconLanguageDict = l.Infer<typeof lexiconLanguageDict>
271
259
 
@@ -274,7 +262,7 @@ const lexiconPermissionSchema = l.intersection(
274
262
  type: l.literal('permission'),
275
263
  resource: l.string({ minLength: 1 }),
276
264
  }),
277
- l.paramsSchema,
265
+ l.dict(l.string(), l.paramSchema),
278
266
  )
279
267
 
280
268
  export type LexiconPermission = l.Infer<typeof lexiconPermissionSchema>
@@ -282,11 +270,11 @@ export type LexiconPermission = l.Infer<typeof lexiconPermissionSchema>
282
270
  const lexiconPermissionSetSchema = l.object({
283
271
  type: l.literal('permission-set'),
284
272
  permissions: l.array(lexiconPermissionSchema),
285
- title: strOpt,
273
+ title: l.optional(l.string()),
286
274
  'title:lang': l.optional(lexiconLanguageDict),
287
- detail: strOpt,
275
+ detail: l.optional(l.string()),
288
276
  'detail:lang': l.optional(lexiconLanguageDict),
289
- description: strOpt,
277
+ description: l.optional(l.string()),
290
278
  })
291
279
 
292
280
  export type LexiconPermissionSet = l.Infer<typeof lexiconPermissionSetSchema>
@@ -323,8 +311,8 @@ export type LexiconIdentifier = l.Infer<typeof lexiconIdentifierSchema>
323
311
  export const lexiconDocumentSchema = l.object({
324
312
  lexicon: l.literal(1),
325
313
  id: lexiconIdentifierSchema,
326
- revision: intOpt,
327
- description: strOpt,
314
+ revision: l.optional(l.integer()),
315
+ description: l.optional(l.string()),
328
316
  defs: l.intersection(
329
317
  l.object({
330
318
  main: l.optional(l.discriminatedUnion('type', MAIN_LEXICON_SCHEMAS)),
@@ -173,7 +173,7 @@ describe('LexiconSchemaBuilder', () => {
173
173
  })
174
174
  })
175
175
 
176
- it('does not apply defaults when allowTransform is false', () => {
176
+ it('does not apply defaults when validating', () => {
177
177
  const schema = getSchema(
178
178
  'com.example.kitchenSink#object',
179
179
  l.TypedObjectSchema,
@@ -187,7 +187,7 @@ describe('LexiconSchemaBuilder', () => {
187
187
  string: 'string',
188
188
  }
189
189
 
190
- expect(schema.safeParse(value, { allowTransform: false })).toStrictEqual({
190
+ expect(schema.safeValidate(value)).toStrictEqual({
191
191
  success: true,
192
192
  value: {
193
193
  object: { boolean: true },
@@ -198,13 +198,12 @@ export class LexiconSchemaBuilder {
198
198
 
199
199
  const result =
200
200
  def.const != null
201
- ? l.literal(def.const, def)
201
+ ? l.literal(def.const)
202
202
  : def.enum != null
203
- ? l.enum(def.enum, def)
203
+ ? l.enum(def.enum)
204
204
  : schema
205
205
 
206
- if (def.default != null) result.check(def.default)
207
- return result
206
+ return def.default != null ? l.withDefault(result, def.default) : result
208
207
  }
209
208
  case 'integer': {
210
209
  const schema = l.integer(def)
@@ -214,25 +213,22 @@ export class LexiconSchemaBuilder {
214
213
 
215
214
  const result =
216
215
  def.const != null
217
- ? l.literal(def.const, def)
216
+ ? l.literal(def.const)
218
217
  : def.enum != null
219
- ? l.enum(def.enum, def)
218
+ ? l.enum(def.enum)
220
219
  : schema
221
220
 
222
- if (def.default != null) result.check(def.default)
223
- return result
221
+ return def.default != null ? l.withDefault(result, def.default) : result
224
222
  }
225
223
  case 'boolean': {
226
- const result =
227
- def.const != null ? l.literal(def.const, def) : l.boolean(def)
224
+ const result = def.const != null ? l.literal(def.const) : l.boolean()
228
225
 
229
- if (def.default != null) result.check(def.default)
230
- return result
226
+ return def.default != null ? l.withDefault(result, def.default) : result
231
227
  }
232
228
  case 'blob':
233
229
  return l.blob(def)
234
230
  case 'cid-link':
235
- return l.cidLink()
231
+ return l.cid()
236
232
  case 'bytes':
237
233
  return l.bytes(def)
238
234
  case 'unknown':
@@ -322,21 +318,18 @@ export class LexiconSchemaBuilder {
322
318
  protected compileParams(doc: LexiconDocument, def?: LexiconParameters) {
323
319
  if (!def) return l.params()
324
320
 
325
- const props: Record<string, l.Validator> = {}
326
- for (const [key, propDef] of Object.entries(def.properties)) {
327
- if (propDef === undefined) continue
328
-
329
- const isRequired = def.required?.includes(key)
321
+ const shape: l.ParamsSchemaShape = {}
322
+ for (const [paramName, paramDef] of Object.entries(def.properties)) {
323
+ if (paramDef === undefined) continue
330
324
 
331
- let schema = this.compileLeaf(doc, propDef)
325
+ const isRequired = def.required?.includes(paramName)
332
326
 
333
- if (!isRequired) {
334
- schema = l.optional(schema)
335
- }
327
+ const propSchema = this.compileLeaf(doc, paramDef) as l.Validator<l.Param>
336
328
 
337
- props[key] = schema
329
+ shape[paramName] = isRequired ? propSchema : l.optional(propSchema)
338
330
  }
339
- return l.params(props)
331
+
332
+ return l.params(shape)
340
333
  }
341
334
  }
342
335