@atproto/lexicon 0.0.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.
Files changed (42) hide show
  1. package/README.md +31 -0
  2. package/build.js +22 -0
  3. package/dist/index.d.ts +126 -0
  4. package/dist/index.js +3897 -0
  5. package/dist/index.js.map +7 -0
  6. package/dist/src/index.d.ts +2 -0
  7. package/dist/src/lexicons.d.ts +15 -0
  8. package/dist/src/record/index.d.ts +4 -0
  9. package/dist/src/record/schema.d.ts +9 -0
  10. package/dist/src/record/schemas.d.ts +10 -0
  11. package/dist/src/record/util.d.ts +1 -0
  12. package/dist/src/record/validation.d.ts +24 -0
  13. package/dist/src/record/validator.d.ts +17 -0
  14. package/dist/src/record-validator.d.ts +17 -0
  15. package/dist/src/schema.d.ts +9 -0
  16. package/dist/src/schemas.d.ts +10 -0
  17. package/dist/src/types.d.ts +30268 -0
  18. package/dist/src/util.d.ts +6 -0
  19. package/dist/src/validation.d.ts +6 -0
  20. package/dist/src/validators/blob.d.ts +6 -0
  21. package/dist/src/validators/complex.d.ts +5 -0
  22. package/dist/src/validators/primitives.d.ts +9 -0
  23. package/dist/src/validators/xrpc.d.ts +3 -0
  24. package/dist/src/view-validator.d.ts +13 -0
  25. package/dist/tsconfig.build.tsbuildinfo +1 -0
  26. package/dist/types.d.ts +73 -0
  27. package/dist/types.js +35 -0
  28. package/jest.config.js +6 -0
  29. package/package.json +21 -0
  30. package/src/index.ts +2 -0
  31. package/src/lexicons.ts +203 -0
  32. package/src/types.ts +318 -0
  33. package/src/util.ts +107 -0
  34. package/src/validation.ts +48 -0
  35. package/src/validators/blob.ts +57 -0
  36. package/src/validators/complex.ts +152 -0
  37. package/src/validators/primitives.ts +300 -0
  38. package/src/validators/xrpc.ts +50 -0
  39. package/tests/_scaffolds/lexicons.ts +379 -0
  40. package/tests/general.test.ts +611 -0
  41. package/tsconfig.build.json +4 -0
  42. package/tsconfig.json +11 -0
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Lexicon
2
+
3
+ Lexicon is the semantic schemas & contracts system for ATP. This library provides definitions and APIs for ATP software.
4
+
5
+ ```
6
+ npm install @atproto/lexicon
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```typescript
12
+ import { Lexicons } from '@atproto/lexicon'
13
+
14
+ // create your lexicons collection
15
+ const lex = new Lexicons()
16
+
17
+ // add lexicon documents
18
+ lex.add({
19
+ lex: 1,
20
+ id: 'com.example.post',
21
+ defs: {
22
+ // ...
23
+ }
24
+ })
25
+
26
+ // validate
27
+ lex.assertValidRecord('com.example.record', {$type: 'com.example.record', ...})
28
+ lex.assertValidXrpcParams('com.example.query', {...})
29
+ lex.assertValidXrpcInput('com.example.procedure', {...})
30
+ lex.assertValidXrpcOutput('com.example.query', {...})
31
+ ```
package/build.js ADDED
@@ -0,0 +1,22 @@
1
+ const pkgJson = require('@npmcli/package-json')
2
+ const { nodeExternalsPlugin } = require('esbuild-node-externals')
3
+
4
+ const buildShallow =
5
+ process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'
6
+
7
+ if (process.argv.includes('--update-main-to-dist')) {
8
+ return pkgJson
9
+ .load(__dirname)
10
+ .then((pkg) => pkg.update({ main: 'dist/index.js' }))
11
+ .then((pkg) => pkg.save())
12
+ }
13
+
14
+ require('esbuild').build({
15
+ logLevel: 'info',
16
+ entryPoints: ['src/index.ts'],
17
+ bundle: true,
18
+ sourcemap: true,
19
+ outdir: 'dist',
20
+ platform: 'node',
21
+ plugins: buildShallow ? [nodeExternalsPlugin()] : [],
22
+ })
@@ -0,0 +1,126 @@
1
+ import { ValidateFunction } from 'ajv';
2
+ import { AdxSchemaDefinition } from './types.js';
3
+ declare type SomeObject = Record<string, unknown>;
4
+ export interface AdxRecordValidatorDescription {
5
+ type: string | string[];
6
+ ext?: string | string[];
7
+ }
8
+ export * from './types.js';
9
+ /**
10
+ * A compiled schema.
11
+ */
12
+ export declare class AdxSchema {
13
+ def: AdxSchemaDefinition;
14
+ id: string;
15
+ validateRecord?: ValidateFunction;
16
+ validateParams?: ValidateFunction;
17
+ validateResponse?: ValidateFunction;
18
+ get name(): string;
19
+ constructor(def: AdxSchemaDefinition);
20
+ }
21
+ /**
22
+ * A collection of compiled schemas.
23
+ */
24
+ export declare class AdxSchemas {
25
+ schemas: Map<string, AdxSchema>;
26
+ private _locale;
27
+ get locale(): string;
28
+ set locale(v: string);
29
+ /**
30
+ * Add a schema definition.
31
+ */
32
+ add(schemaDef: unknown): void;
33
+ /**
34
+ * Remove a schema definition.
35
+ */
36
+ remove(key: string): void;
37
+ /**
38
+ * Get a schema definition.
39
+ */
40
+ get(key: string): AdxSchema | undefined;
41
+ /**
42
+ * Create a record validator out of one or more schemas.
43
+ */
44
+ createRecordValidator(desc: string | string[] | AdxRecordValidatorDescription): AdxRecordValidator;
45
+ /**
46
+ * Create a view validator out of a schema.
47
+ */
48
+ createViewValidator(view: string): AdxViewValidator;
49
+ }
50
+ /**
51
+ * Validates records using schemas.
52
+ */
53
+ export declare class AdxRecordValidator {
54
+ private schemas;
55
+ type: AdxSchema[];
56
+ ext: AdxSchema[];
57
+ constructor(schemas: AdxSchemas, type: AdxSchema[], ext: AdxSchema[]);
58
+ /**
59
+ * Returns detailed information about validity and compatibility.
60
+ */
61
+ validate(value: SomeObject): AdxValidationResult;
62
+ /**
63
+ * Provides a simple boolean check of validity.
64
+ */
65
+ isValid(value: any): boolean;
66
+ /**
67
+ * Like validate() but throws if validation fails.
68
+ */
69
+ assertValid(value: any): AdxValidationResult;
70
+ }
71
+ /**
72
+ * Validates views using schemas.
73
+ */
74
+ export declare class AdxViewValidator {
75
+ view: AdxSchema;
76
+ constructor(view: AdxSchema);
77
+ /**
78
+ * Returns detailed information about validity and compatibility.
79
+ */
80
+ validateResponse(value: SomeObject): AdxValidationResult;
81
+ /**
82
+ * Provides a simple boolean check of validity.
83
+ */
84
+ isResponseValid(value: any): boolean;
85
+ /**
86
+ * Like validateResponse() but throws if validation fails.
87
+ */
88
+ assertResponseValid(value: any): AdxValidationResult;
89
+ }
90
+ export declare enum AdxValidationResultCode {
91
+ Full = "full",
92
+ Partial = "partial",
93
+ Incompatible = "incompatible",
94
+ Invalid = "invalid"
95
+ }
96
+ export declare class AdxValidationResult {
97
+ code: AdxValidationResultCode;
98
+ /**
99
+ * The error message (if fatal)
100
+ */
101
+ error: string | undefined;
102
+ /**
103
+ * A collection of all fallback messages
104
+ */
105
+ fallbacks: string[];
106
+ /**
107
+ * A collection of all messages
108
+ */
109
+ messages: string[];
110
+ get valid(): boolean;
111
+ get fullySupported(): boolean;
112
+ get incompatible(): boolean;
113
+ /**
114
+ * Internal - used to transition the state machine.
115
+ */
116
+ _t(to: AdxValidationResultCode, message?: string): void;
117
+ /**
118
+ * Internal - used to transition the state machine.
119
+ */
120
+ _fail(schema: AdxSchema, validator: ValidateFunction): void;
121
+ }
122
+ export declare class AdxValidationError extends Error {
123
+ code: AdxValidationResultCode;
124
+ messages: string[];
125
+ constructor(res: AdxValidationResult);
126
+ }