@atproto/lexicon 0.2.3 → 0.3.1

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.
@@ -2,8 +2,8 @@ import { LexiconDoc, LexUserType, ValidationResult } from './types';
2
2
  export declare class Lexicons {
3
3
  docs: Map<string, LexiconDoc>;
4
4
  defs: Map<string, LexUserType>;
5
- constructor(docs?: unknown[]);
6
- add(doc: unknown): void;
5
+ constructor(docs?: LexiconDoc[]);
6
+ add(doc: LexiconDoc): void;
7
7
  remove(uri: string): void;
8
8
  get(uri: string): LexiconDoc | undefined;
9
9
  getDef(uri: string): LexUserType | undefined;
package/dist/types.d.ts CHANGED
@@ -21737,11 +21737,7 @@ export declare const discriminatedObject: z.ZodObject<{
21737
21737
  }>;
21738
21738
  export declare type DiscriminatedObject = z.infer<typeof discriminatedObject>;
21739
21739
  export declare function isDiscriminatedObject(value: unknown): value is DiscriminatedObject;
21740
- export declare class LexiconDocMalformedError extends Error {
21741
- schemaDef: unknown;
21742
- issues?: z.ZodIssue[] | undefined;
21743
- constructor(message: string, schemaDef: unknown, issues?: z.ZodIssue[] | undefined);
21744
- }
21740
+ export declare function parseLexiconDoc(v: unknown): LexiconDoc;
21745
21741
  export declare type ValidationResult = {
21746
21742
  success: true;
21747
21743
  value: unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lexicon",
3
- "version": "0.2.3",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
5
  "description": "atproto Lexicon schema language library",
6
6
  "keywords": [
@@ -18,8 +18,8 @@
18
18
  "iso-datestring-validator": "^2.2.2",
19
19
  "multiformats": "^9.9.0",
20
20
  "zod": "^3.21.4",
21
- "@atproto/common-web": "^0.2.2",
22
- "@atproto/syntax": "^0.1.3"
21
+ "@atproto/common-web": "^0.2.3",
22
+ "@atproto/syntax": "^0.1.5"
23
23
  },
24
24
  "scripts": {
25
25
  "test": "jest",
package/src/lexicons.ts CHANGED
@@ -1,12 +1,9 @@
1
- import { ZodError } from 'zod'
2
1
  import {
3
2
  LexiconDoc,
4
- lexiconDoc,
5
3
  LexRecord,
6
4
  LexXrpcProcedure,
7
5
  LexXrpcQuery,
8
6
  LexUserType,
9
- LexiconDocMalformedError,
10
7
  LexiconDefNotFoundError,
11
8
  InvalidLexiconError,
12
9
  ValidationResult,
@@ -32,7 +29,7 @@ export class Lexicons {
32
29
  docs: Map<string, LexiconDoc> = new Map()
33
30
  defs: Map<string, LexUserType> = new Map()
34
31
 
35
- constructor(docs?: unknown[]) {
32
+ constructor(docs?: LexiconDoc[]) {
36
33
  if (docs?.length) {
37
34
  for (const doc of docs) {
38
35
  this.add(doc)
@@ -43,24 +40,8 @@ export class Lexicons {
43
40
  /**
44
41
  * Add a lexicon doc.
45
42
  */
46
- add(doc: unknown): void {
47
- try {
48
- lexiconDoc.parse(doc)
49
- } catch (e) {
50
- if (e instanceof ZodError) {
51
- throw new LexiconDocMalformedError(
52
- `Failed to parse schema definition ${
53
- (doc as Record<string, string>).id
54
- }`,
55
- doc,
56
- e.issues,
57
- )
58
- } else {
59
- throw e
60
- }
61
- }
62
- const validatedDoc = doc as LexiconDoc
63
- const uri = toLexUri(validatedDoc.id)
43
+ add(doc: LexiconDoc): void {
44
+ const uri = toLexUri(doc.id)
64
45
  if (this.docs.has(uri)) {
65
46
  throw new Error(`${uri} has already been registered`)
66
47
  }
@@ -68,10 +49,10 @@ export class Lexicons {
68
49
  // WARNING
69
50
  // mutates the object
70
51
  // -prf
71
- resolveRefUris(validatedDoc, uri)
52
+ resolveRefUris(doc, uri)
72
53
 
73
- this.docs.set(uri, validatedDoc)
74
- for (const [defUri, def] of iterDefs(validatedDoc)) {
54
+ this.docs.set(uri, doc)
55
+ for (const [defUri, def] of iterDefs(doc)) {
75
56
  this.defs.set(defUri, def)
76
57
  }
77
58
  }
package/src/types.ts CHANGED
@@ -415,16 +415,9 @@ export function isDiscriminatedObject(
415
415
  return discriminatedObject.safeParse(value).success
416
416
  }
417
417
 
418
- export class LexiconDocMalformedError extends Error {
419
- constructor(
420
- message: string,
421
- public schemaDef: unknown,
422
- public issues?: z.ZodIssue[],
423
- ) {
424
- super(message)
425
- this.schemaDef = schemaDef
426
- this.issues = issues
427
- }
418
+ export function parseLexiconDoc(v: unknown): LexiconDoc {
419
+ lexiconDoc.parse(v)
420
+ return v as LexiconDoc
428
421
  }
429
422
 
430
423
  export type ValidationResult =
@@ -18,7 +18,7 @@ export function datetime(path: string, value: string): ValidationResult {
18
18
  return {
19
19
  success: false,
20
20
  error: new ValidationError(
21
- `${path} must be an iso8601 formatted datetime`,
21
+ `${path} must be an valid atproto datetime (both RFC-3339 and ISO-8601)`,
22
22
  ),
23
23
  }
24
24
  }
@@ -1,4 +1,6 @@
1
- export default [
1
+ import { LexiconDoc } from '../../src/index'
2
+
3
+ const lexicons: LexiconDoc[] = [
2
4
  {
3
5
  lexicon: 1,
4
6
  id: 'com.example.kitchenSink',
@@ -521,3 +523,5 @@ export default [
521
523
  },
522
524
  },
523
525
  ]
526
+
527
+ export default lexicons
@@ -1,5 +1,5 @@
1
1
  import { CID } from 'multiformats/cid'
2
- import { lexiconDoc, Lexicons } from '../src/index'
2
+ import { LexiconDoc, Lexicons, parseLexiconDoc } from '../src/index'
3
3
  import LexiconDocs from './_scaffolds/lexicons'
4
4
 
5
5
  describe('Lexicons collection', () => {
@@ -97,7 +97,7 @@ describe('General validation', () => {
97
97
  },
98
98
  }
99
99
  expect(() => {
100
- lexiconDoc.parse(schema)
100
+ parseLexiconDoc(schema)
101
101
  }).toThrow('Required field \\"foo\\" not defined')
102
102
  })
103
103
  it('fails when unknown fields are present', () => {
@@ -113,11 +113,11 @@ describe('General validation', () => {
113
113
  }
114
114
 
115
115
  expect(() => {
116
- lexiconDoc.parse(schema)
116
+ parseLexiconDoc(schema)
117
117
  }).toThrow("Unrecognized key(s) in object: 'foo'")
118
118
  })
119
119
  it('fails lexicon parsing when uri is invalid', () => {
120
- const schema = {
120
+ const schema: LexiconDoc = {
121
121
  lexicon: 1,
122
122
  id: 'com.example.invalidUri',
123
123
  defs: {
@@ -135,7 +135,7 @@ describe('General validation', () => {
135
135
  }).toThrow('Uri can only have one hash segment')
136
136
  })
137
137
  it('fails validation when ref uri has multiple hash segments', () => {
138
- const schema = {
138
+ const schema: LexiconDoc = {
139
139
  lexicon: 1,
140
140
  id: 'com.example.invalidUri',
141
141
  defs: {
@@ -168,7 +168,7 @@ describe('General validation', () => {
168
168
  }).toThrow('Uri can only have one hash segment')
169
169
  })
170
170
  it('union handles both implicit and explicit #main', () => {
171
- const schemas = [
171
+ const schemas: LexiconDoc[] = [
172
172
  {
173
173
  lexicon: 1,
174
174
  id: 'com.example.implicitMain',
@@ -659,7 +659,9 @@ describe('Record validation', () => {
659
659
  $type: 'com.example.datetime',
660
660
  datetime: 'bad date',
661
661
  }),
662
- ).toThrow('Record/datetime must be an iso8601 formatted datetime')
662
+ ).toThrow(
663
+ 'Record/datetime must be an valid atproto datetime (both RFC-3339 and ISO-8601)',
664
+ )
663
665
  })
664
666
 
665
667
  it('Applies uri formatting constraint', () => {
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022-2023 Bluesky PBC
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.