@atproto/lex-document 0.0.5 → 0.0.7

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 CHANGED
@@ -1,5 +1,21 @@
1
1
  # @atproto/lex-document
2
2
 
3
+ ## 0.0.7
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4443](https://github.com/bluesky-social/atproto/pull/4443) [`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Ensure that default values match constraints
8
+
9
+ - Updated dependencies [[`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c), [`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c), [`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c), [`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c), [`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c), [`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c), [`9af7a2d`](https://github.com/bluesky-social/atproto/commit/9af7a2d12240e91248610ce4fe7d93387733c59c)]:
10
+ - @atproto/lex-schema@0.0.6
11
+
12
+ ## 0.0.6
13
+
14
+ ### Patch Changes
15
+
16
+ - Updated dependencies [[`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece)]:
17
+ - @atproto/lex-schema@0.0.5
18
+
3
19
  ## 0.0.5
4
20
 
5
21
  ### Patch Changes
@@ -18,21 +18,21 @@ export declare class LexiconSchemaBuilder {
18
18
  #private;
19
19
  protected indexer: LexiconIndexer;
20
20
  static build(indexer: LexiconIndexer, fullRef: string): Promise<l.Validator<unknown>>;
21
- static buildAll(indexer: LexiconIndexer): Promise<Map<string, l.Validator<unknown> | l.Query<any, any, any, any> | l.Subscription<any, any, any, any> | l.Procedure<any, any, any, any, any> | l.PermissionSet<any, any>>>;
21
+ static buildAll(indexer: LexiconIndexer): Promise<Map<string, l.Validator<unknown> | l.Query<`${string}.${string}.${string}`, l.ParamsSchema<l.ParamsSchemaShape>, l.Payload<string | undefined, l.Schema<any> | undefined>, readonly string[] | undefined> | l.Subscription<`${string}.${string}.${string}`, l.ParamsSchema<l.ParamsSchemaShape>, l.Schema<any>, readonly string[] | undefined> | l.Procedure<`${string}.${string}.${string}`, l.ParamsSchema<l.ParamsSchemaShape>, l.Payload<string | undefined, l.Schema<any> | undefined>, l.Payload<string | undefined, l.Schema<any> | undefined>, readonly string[] | undefined> | l.PermissionSet<any, any>>>;
22
22
  constructor(indexer: LexiconIndexer);
23
23
  done(): Promise<void>;
24
24
  buildFullRef: (fullRef: string) => Promise<l.Validator<unknown> | l.PermissionSet<`${string}.${string}.${string}`, l.Permission<string, {
25
25
  [x: string]: string | number | boolean | (string | number | boolean)[];
26
- }>[]> | l.Procedure<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Validator<any> | undefined>, l.Payload<string | undefined, l.Validator<any> | undefined>, string[]> | l.Query<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Validator<any> | undefined>, string[]> | l.Subscription<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.ObjectSchema<any> | l.RefSchema<unknown> | l.TypedUnionSchema<l.TypedRefSchema<{
26
+ }>[]> | l.Procedure<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Schema<any> | undefined>, l.Payload<string | undefined, l.Schema<any> | undefined>, string[]> | l.Query<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Schema<any> | undefined>, string[]> | l.Subscription<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.ObjectSchema<any> | l.RefSchema<unknown> | l.TypedUnionSchema<l.TypedRefSchema<{
27
27
  [x: string]: any;
28
- }>[], boolean> | undefined, string[]>>;
28
+ }>[], boolean>, string[]>>;
29
29
  protected buildRefGetter(fullRef: string): () => l.Validator<unknown>;
30
30
  protected buildTypedRefGetter(fullRef: string): () => l.TypedObjectSchema | l.RecordSchema;
31
31
  protected compileDef(doc: LexiconDocument, hash: string): l.Validator<unknown> | l.PermissionSet<`${string}.${string}.${string}`, l.Permission<string, {
32
32
  [x: string]: string | number | boolean | (string | number | boolean)[];
33
- }>[]> | l.Procedure<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Validator<any> | undefined>, l.Payload<string | undefined, l.Validator<any> | undefined>, string[]> | l.Query<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Validator<any> | undefined>, string[]> | l.Subscription<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.ObjectSchema<any> | l.RefSchema<unknown> | l.TypedUnionSchema<l.TypedRefSchema<{
33
+ }>[]> | l.Procedure<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Schema<any> | undefined>, l.Payload<string | undefined, l.Schema<any> | undefined>, string[]> | l.Query<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.Payload<string | undefined, l.Schema<any> | undefined>, string[]> | l.Subscription<`${string}.${string}.${string}`, l.ParamsSchema<{}>, l.ObjectSchema<any> | l.RefSchema<unknown> | l.TypedUnionSchema<l.TypedRefSchema<{
34
34
  [x: string]: any;
35
- }>[], boolean> | undefined, string[]>;
35
+ }>[], boolean>, string[]>;
36
36
  protected compileLeaf(doc: LexiconDocument, def: LexiconArray | LexiconArrayItems): l.Validator<unknown>;
37
37
  protected compileRef(doc: LexiconDocument, def: LexiconRef | LexiconRefUnion): l.RefSchema<unknown> | l.TypedUnionSchema<l.TypedRefSchema<{
38
38
  [x: string]: any;
@@ -40,9 +40,9 @@ export declare class LexiconSchemaBuilder {
40
40
  protected compileObject(doc: LexiconDocument, def: LexiconObject): l.ObjectSchema;
41
41
  protected compilePayload(doc: LexiconDocument, def: LexiconPayload | undefined): l.Payload;
42
42
  protected compileErrors(_doc: LexiconDocument, errors?: readonly LexiconError[]): undefined | string[];
43
- protected compilePayloadSchema(doc: LexiconDocument, def?: LexiconObject | LexiconRef | LexiconRefUnion): l.ObjectSchema<any> | l.RefSchema<unknown> | l.TypedUnionSchema<l.TypedRefSchema<{
43
+ protected compilePayloadSchema(doc: LexiconDocument, def: LexiconObject | LexiconRef | LexiconRefUnion): l.ObjectSchema<any> | l.RefSchema<unknown> | l.TypedUnionSchema<l.TypedRefSchema<{
44
44
  [x: string]: any;
45
- }>[], boolean> | undefined;
45
+ }>[], boolean>;
46
46
  protected compileParams(doc: LexiconDocument, def?: LexiconParameters): l.ParamsSchema<{}>;
47
47
  }
48
48
  export declare function memoize<Fn extends (arg: string) => unknown>(fn: Fn): Fn;
@@ -1 +1 @@
1
- {"version":3,"file":"lexicon-schema-builder.d.ts","sourceRoot":"","sources":["../src/lexicon-schema-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,qBAAqB,CAAA;AACvC,OAAO,EACL,YAAY,EACZ,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,UAAU,EACV,eAAe,EAChB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAErD;;;;;;;;;;;;GAYG;AACH,qBAAa,oBAAoB;;IA8CnB,SAAS,CAAC,OAAO,EAAE,cAAc;WA7ChC,KAAK,CAChB,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;WAanB,QAAQ,CAAC,OAAO,EAAE,cAAc;gBA6BvB,OAAO,EAAE,cAAc;IAEvC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,YAAY,YAA2B,MAAM;;;;2CAM3C;IAEF,SAAS,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC;IAkBrE,SAAS,CAAC,mBAAmB,CAC3B,OAAO,EAAE,MAAM,GACd,MAAM,CAAC,CAAC,iBAAiB,GAAG,CAAC,CAAC,YAAY;IAqB7C,SAAS,CAAC,UAAU,CAAC,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM;;;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AAG,EAAE,aAAa,GAAG,UAAU,GAAG,eAAe;;;IAUnD,SAAS,CAAC,aAAa,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,CAAC,EAAE,iBAAiB;CAmBtE;AA6CD,wBAAgB,OAAO,CAAC,EAAE,SAAS,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,CAQvE"}
@@ -106,7 +106,7 @@ class LexiconSchemaBuilder {
106
106
  case 'query':
107
107
  return lex_schema_1.l.query(doc.id, this.compileParams(doc, def.parameters), this.compilePayload(doc, def.output), this.compileErrors(doc, def.errors));
108
108
  case 'subscription':
109
- return lex_schema_1.l.subscription(doc.id, this.compileParams(doc, def.parameters), this.compilePayloadSchema(doc, def.message?.schema), this.compileErrors(doc, def.errors));
109
+ return lex_schema_1.l.subscription(doc.id, this.compileParams(doc, def.parameters), this.compilePayloadSchema(doc, def.message.schema), this.compileErrors(doc, def.errors));
110
110
  case 'token':
111
111
  return lex_schema_1.l.token(doc.id, hash);
112
112
  case 'record':
@@ -128,41 +128,45 @@ class LexiconSchemaBuilder {
128
128
  switch (def.type) {
129
129
  case 'string': {
130
130
  const schema = lex_schema_1.l.string(def);
131
- if (def.const != null) {
132
- schema.assert(def.const);
133
- return lex_schema_1.l.literal(def.const, def);
134
- }
135
- else if (def.enum != null) {
131
+ if (def.default != null)
132
+ schema.check(def.default);
133
+ if (def.const != null)
134
+ schema.check(def.const);
135
+ if (def.enum != null)
136
136
  for (const v of def.enum)
137
- schema.assert(v);
138
- return lex_schema_1.l.enum(def.enum, def);
139
- }
140
- else {
141
- return schema;
142
- }
137
+ schema.check(v);
138
+ const result = def.const != null
139
+ ? lex_schema_1.l.literal(def.const, def)
140
+ : def.enum != null
141
+ ? lex_schema_1.l.enum(def.enum, def)
142
+ : schema;
143
+ if (def.default != null)
144
+ result.check(def.default);
145
+ return result;
143
146
  }
144
147
  case 'integer': {
145
148
  const schema = lex_schema_1.l.integer(def);
146
- if (def.const != null) {
147
- schema.assert(def.const);
148
- return lex_schema_1.l.literal(def.const, def);
149
- }
150
- else if (def.enum != null) {
149
+ if (def.default != null)
150
+ schema.check(def.default);
151
+ if (def.const != null)
152
+ schema.check(def.const);
153
+ if (def.enum != null)
151
154
  for (const v of def.enum)
152
- schema.assert(v);
153
- return lex_schema_1.l.enum(def.enum, def);
154
- }
155
- else {
156
- return schema;
157
- }
155
+ schema.check(v);
156
+ const result = def.const != null
157
+ ? lex_schema_1.l.literal(def.const, def)
158
+ : def.enum != null
159
+ ? lex_schema_1.l.enum(def.enum, def)
160
+ : schema;
161
+ if (def.default != null)
162
+ result.check(def.default);
163
+ return result;
158
164
  }
159
165
  case 'boolean': {
160
- if (def.const != null) {
161
- return lex_schema_1.l.literal(def.const, def);
162
- }
163
- else {
164
- return lex_schema_1.l.boolean(def);
165
- }
166
+ const result = def.const != null ? lex_schema_1.l.literal(def.const, def) : lex_schema_1.l.boolean(def);
167
+ if (def.default != null)
168
+ result.check(def.default);
169
+ return result;
166
170
  }
167
171
  case 'blob':
168
172
  return lex_schema_1.l.blob(def);
@@ -214,8 +218,6 @@ class LexiconSchemaBuilder {
214
218
  return errors?.map((e) => e.name);
215
219
  }
216
220
  compilePayloadSchema(doc, def) {
217
- if (!def)
218
- return undefined;
219
221
  switch (def.type) {
220
222
  case 'object':
221
223
  return this.compileObject(doc, def);
@@ -1 +1 @@
1
- {"version":3,"file":"lexicon-schema-builder.js","sourceRoot":"","sources":["../src/lexicon-schema-builder.ts"],"names":[],"mappings":";;;AA0XA,0BAQC;AAlYD,oDAAuC;AAcvC;;;;;;;;;;;;GAYG;AACH,MAAa,oBAAoB;IA8CT;IA7CtB,MAAM,CAAC,KAAK,CAAC,KAAK,CAChB,OAAuB,EACvB,OAAe;QAEf,MAAM,GAAG,GAAG,IAAI,oBAAoB,CAAC,OAAO,CAAC,CAAA;QAC7C,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC,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AE,IAAI,EAAE,CAAA;AACvB,CAAC;AAED,SAAS,YAAY,CAAC,IAAqB,EAAE,GAAW;IACtD,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,IAAI,CAAC,EAAE,GAAG,GAAG,EAAE,CAAA;IAClD,OAAO,GAAG,CAAA;AACZ,CAAC;AAED,SAAgB,OAAO,CAAsC,EAAM;IACjE,MAAM,KAAK,GAAG,IAAI,GAAG,EAA0B,CAAA;IAC/C,OAAO,CAAC,CAAC,GAAW,EAAE,EAAE;QACtB,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAE,CAAA;QAC1C,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,CAAmB,CAAA;QACxC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QACtB,OAAO,MAAM,CAAA;IACf,CAAC,CAAO,CAAA;AACV,CAAC;AAED,SAAS,qBAAqB,CAC5B,GAAM;IAEN,OAAO,CACL,GAAG,IAAI,IAAI;QACX,OAAO,GAAG,KAAK,QAAQ;QACvB,MAAM,CAAC,aAAa,IAAI,GAAG;QAC3B,OAAO,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,UAAU,CAChD,CAAA;AACH,CAAC","sourcesContent":["import { l } from '@atproto/lex-schema'\nimport {\n LexiconArray,\n LexiconArrayItems,\n LexiconDocument,\n LexiconError,\n LexiconObject,\n LexiconParameters,\n LexiconPayload,\n LexiconRef,\n LexiconRefUnion,\n} from './lexicon-document.js'\nimport { LexiconIndexer } from './lexicon-indexer.js'\n\n/**\n * Builds a validator for a given lexicon \"ref\" from a lexicon indexer.\n *\n * @example\n *\n * ```ts\n * import { LexiconSchemaBuilder } from '@atproto/lex/doc'\n * import { LexiconStreamIndexer } from '@atproto/lex/doc'\n *\n * const indexer = new LexiconStreamIndexer(lexiconDocs)\n * const validator = await LexiconSchemaBuilder.build(indexer, 'com.example.foo#bar')\n * ```\n */\nexport class LexiconSchemaBuilder {\n static async build(\n indexer: LexiconIndexer,\n fullRef: string,\n ): Promise<l.Validator<unknown>> {\n const ctx = new LexiconSchemaBuilder(indexer)\n try {\n const result = await ctx.buildFullRef(fullRef)\n if (!(result instanceof l.Schema)) {\n throw new Error(`Ref ${fullRef} is not a schema type`)\n }\n return result\n } finally {\n await ctx.done()\n }\n }\n\n static async buildAll(indexer: LexiconIndexer) {\n const builder = new LexiconSchemaBuilder(indexer)\n const schemas = new Map<\n string,\n | l.Validator<unknown>\n | l.Query\n | l.Subscription\n | l.Procedure\n | l.PermissionSet\n >()\n if (!isAsyncIterableObject(indexer)) {\n throw new Error('An iterable indexer is required to build all schemas')\n }\n try {\n for await (const doc of indexer) {\n for (const hash of Object.keys(doc.defs)) {\n const fullRef = `${doc.id}#${hash}`\n const schema = await builder.buildFullRef(fullRef)\n schemas.set(fullRef, schema)\n }\n }\n return schemas\n } finally {\n await builder.done()\n }\n }\n\n #asyncTasks = new AsyncTasks()\n\n constructor(protected indexer: LexiconIndexer) {}\n\n async done(): Promise<void> {\n await this.#asyncTasks.done()\n }\n\n buildFullRef = memoize(async (fullRef: string) => {\n const { nsid, hash } = parseRef(fullRef)\n\n const doc = await this.indexer.get(nsid)\n\n return this.compileDef(doc, hash)\n })\n\n protected buildRefGetter(fullRef: string): () => l.Validator<unknown> {\n let validator: l.Validator<unknown>\n\n this.#asyncTasks.add(\n this.buildFullRef(fullRef).then((v) => {\n if (!(v instanceof l.Schema)) {\n throw new Error(`Only refs to schema types are allowed`)\n }\n validator = v\n }),\n )\n\n return () => {\n if (validator) return validator\n throw new Error('Validator not yet built. Did you await done()?')\n }\n }\n\n protected buildTypedRefGetter(\n fullRef: string,\n ): () => l.TypedObjectSchema | l.RecordSchema {\n let validator: l.TypedObjectSchema | l.RecordSchema\n\n this.#asyncTasks.add(\n this.buildFullRef(fullRef).then((v) => {\n if (v instanceof l.TypedObjectSchema || v instanceof l.RecordSchema) {\n validator = v\n } else {\n throw new Error(\n 'Only refs to records and object definitions are allowed',\n )\n }\n }),\n )\n\n return () => {\n if (validator) return validator\n throw new Error('Validator not yet built. Did you await done()?')\n }\n }\n\n protected compileDef(doc: LexiconDocument, hash: string) {\n const def = Object.hasOwn(doc.defs, hash) ? doc.defs[hash] : null\n if (!def) {\n throw new Error(\n `No definition found for hash \"${JSON.stringify(hash)}\" in ${doc.id}`,\n )\n }\n switch (def.type) {\n case 'permission-set':\n return l.permissionSet(\n doc.id,\n def.permissions.map(({ resource, type, ...p }) =>\n l.permission(resource, p),\n ),\n def,\n )\n case 'procedure':\n return l.procedure(\n doc.id,\n this.compileParams(doc, def.parameters),\n this.compilePayload(doc, def.input),\n this.compilePayload(doc, def.output),\n this.compileErrors(doc, def.errors),\n )\n case 'query':\n return l.query(\n doc.id,\n this.compileParams(doc, def.parameters),\n this.compilePayload(doc, def.output),\n this.compileErrors(doc, def.errors),\n )\n case 'subscription':\n return l.subscription(\n doc.id,\n this.compileParams(doc, def.parameters),\n this.compilePayloadSchema(doc, def.message?.schema),\n this.compileErrors(doc, def.errors),\n )\n case 'token':\n return l.token(doc.id, hash)\n case 'record':\n return l.record(def.key, doc.id, this.compileObject(doc, def.record))\n case 'object':\n return l.typedObject(doc.id, hash, this.compileObject(doc, def))\n default:\n return this.compileLeaf(doc, def)\n }\n }\n\n protected compileLeaf(\n doc: LexiconDocument,\n def: LexiconArray | LexiconArrayItems,\n ): l.Validator<unknown> {\n if (\n 'const' in def &&\n 'enum' in def &&\n def.enum != null &&\n def.const !== undefined &&\n !(def.enum as readonly unknown[]).includes(def.const)\n ) {\n return l.never()\n }\n\n switch (def.type) {\n case 'string': {\n const schema: l.StringSchema = l.string(def)\n if (def.const != null) {\n schema.assert(def.const)\n return l.literal(def.const, def)\n } else if (def.enum != null) {\n for (const v of def.enum) schema.assert(v)\n return l.enum(def.enum, def)\n } else {\n return schema\n }\n }\n case 'integer': {\n const schema: l.IntegerSchema = l.integer(def)\n if (def.const != null) {\n schema.assert(def.const)\n return l.literal(def.const, def)\n } else if (def.enum != null) {\n for (const v of def.enum) schema.assert(v)\n return l.enum(def.enum, def)\n } else {\n return schema\n }\n }\n case 'boolean': {\n if (def.const != null) {\n return l.literal(def.const, def)\n } else {\n return l.boolean(def)\n }\n }\n case 'blob':\n return l.blob(def)\n case 'cid-link':\n return l.cidLink()\n case 'bytes':\n return l.bytes(def)\n case 'unknown':\n return l.unknown()\n case 'array':\n return l.array(this.compileLeaf(doc, def.items), def)\n default:\n return this.compileRef(doc, def)\n }\n }\n\n protected compileRef(\n doc: LexiconDocument,\n def: LexiconRef | LexiconRefUnion,\n ) {\n switch (def.type) {\n case 'ref':\n return l.ref(this.buildRefGetter(buildFullRef(doc, def.ref)))\n case 'union':\n return l.typedUnion(\n def.refs.map((r) =>\n l.typedRef(this.buildTypedRefGetter(buildFullRef(doc, r))),\n ),\n def.closed ?? false,\n )\n default:\n // @ts-expect-error\n throw new Error(`Unknown lexicon type: ${def.type}`)\n }\n }\n\n protected compileObject(\n doc: LexiconDocument,\n def: LexiconObject,\n ): l.ObjectSchema {\n const props: Record<string, l.Validator> = {}\n for (const [key, propDef] of Object.entries(def.properties)) {\n if (propDef === undefined) continue\n\n const isNullable = def.nullable?.includes(key)\n const isRequired = def.required?.includes(key)\n\n let schema = this.compileLeaf(doc, propDef)\n\n if (isNullable) {\n schema = l.nullable(schema)\n }\n\n if (!isRequired) {\n schema = l.optional(schema)\n }\n\n props[key] = schema\n }\n return l.object(props)\n }\n\n protected compilePayload(\n doc: LexiconDocument,\n def: LexiconPayload | undefined,\n ): l.Payload {\n return l.payload(\n def?.encoding,\n def?.schema ? this.compilePayloadSchema(doc, def.schema) : undefined,\n )\n }\n\n protected compileErrors(\n _doc: LexiconDocument,\n errors?: readonly LexiconError[],\n ): undefined | string[] {\n return errors?.map((e) => e.name)\n }\n\n protected compilePayloadSchema(\n doc: LexiconDocument,\n def?: LexiconObject | LexiconRef | LexiconRefUnion,\n ) {\n if (!def) return undefined\n switch (def.type) {\n case 'object':\n return this.compileObject(doc, def)\n default:\n return this.compileRef(doc, def)\n }\n }\n\n protected compileParams(doc: LexiconDocument, def?: LexiconParameters) {\n if (!def) return l.params()\n\n const props: Record<string, l.Validator> = {}\n for (const [key, propDef] of Object.entries(def.properties)) {\n if (propDef === undefined) continue\n\n const isRequired = def.required?.includes(key)\n\n let schema = this.compileLeaf(doc, propDef)\n\n if (!isRequired) {\n schema = l.optional(schema)\n }\n\n props[key] = schema\n }\n return l.params(props)\n }\n}\n\nclass AsyncTasks {\n /**\n * A set that, eventually, contains only rejected promises.\n */\n #promises = new Set<Promise<void>>()\n\n async done(): Promise<void> {\n do {\n // @NOTE this is going to throw on the first rejected promise (which is\n // what we want)\n for (const p of this.#promises) await p\n // At this point, all settled promises should have been removed. If\n // this.#promises is not empty, it means new promises were added during\n // the awaiting process, so we loop again.\n } while (this.#promises.size > 0)\n }\n\n add(p: Promise<void>) {\n const promise = Promise.resolve(p).then(() => {\n // No need to keep the promise any longer\n this.#promises.delete(promise)\n })\n\n void promise.catch((_err) => {\n // ignore errors here, they should be caught though done()\n })\n\n this.#promises.add(promise)\n }\n}\n\nfunction parseRef(fullRef: string) {\n const { length, 0: nsid, 1: hash } = fullRef.split('#')\n if (length !== 2) throw new Error('Uri can only have one hash segment')\n if (!nsid || !hash) throw new Error('Invalid ref, missing hash')\n return { nsid, hash }\n}\n\nfunction buildFullRef(from: LexiconDocument, ref: string) {\n if (ref.startsWith('#')) return `${from.id}${ref}`\n return ref\n}\n\nexport function memoize<Fn extends (arg: string) => unknown>(fn: Fn): Fn {\n const cache = new Map<string, ReturnType<Fn>>()\n return ((arg: string) => {\n if (cache.has(arg)) return cache.get(arg)!\n const result = fn(arg) as ReturnType<Fn>\n cache.set(arg, result)\n return result\n }) as Fn\n}\n\nfunction isAsyncIterableObject<T>(\n obj: T,\n): obj is T & object & AsyncIterable<unknown> {\n return (\n obj != null &&\n typeof obj === 'object' &&\n Symbol.asyncIterator in obj &&\n typeof obj[Symbol.asyncIterator] === 'function'\n )\n}\n"]}
1
+ {"version":3,"file":"lexicon-schema-builder.js","sourceRoot":"","sources":["../src/lexicon-schema-builder.ts"],"names":[],"mappings":";;;AAiYA,0BAQC;AAzYD,oDAAuC;AAcvC;;;;;;;;;;;;GAYG;AACH,MAAa,oBAAoB;IA8CT;IA7CtB,MAAM,CAAC,KAAK,CAAC,KAAK,CAChB,OAAuB,EACvB,OAAe;QAEf,MAAM,GAAG,GAAG,IAAI,oBAAoB,CAAC,OAAO,CAAC,CAAA;QAC7C,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,CAAA;YAC9C,IAAI,CAAC,CAAC,MAAM,YAAY,cAAC,CAAC,MAAM,CAAC,EAAE,CAAC;gBAClC,MAAM,IAAI,KAAK,CAAC,OAAO,OAAO,uBAAuB,CAAC,CAAA;YACxD,CAAC;YACD,OAAO,MAAM,CAAA;QACf,CAAC;gBAAS,CAAC;YACT,MAAM,GAAG,CAAC,IAAI,EAAE,CAAA;QAClB,CAAC;IACH,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAuB;QAC3C,MAAM,OAAO,GAAG,IAAI,oBAAoB,CAAC,OAAO,CAAC,CAAA;QACjD,MAAM,OAAO,GAAG,IAAI,GAAG,EAOpB,CAAA;QACH,IAAI,CAAC,qBAAqB,CAAC,OAAO,CAAC,EAAE,CAAC;YACpC,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;QACzE,CAAC;QACD,IAAI,CAAC;YACH,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;gBAChC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;oBACzC,MAAM,OAAO,GAAG,GAAG,GAAG,CAAC,EAAE,IAAI,IAAI,EAAE,CAAA;oBAC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AED,SAAgB,OAAO,CAAsC,EAAM;IACjE,MAAM,KAAK,GAAG,IAAI,GAAG,EAA0B,CAAA;IAC/C,OAAO,CAAC,CAAC,GAAW,EAAE,EAAE;QACtB,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAE,CAAA;QAC1C,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,CAAmB,CAAA;QACxC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QACtB,OAAO,MAAM,CAAA;IACf,CAAC,CAAO,CAAA;AACV,CAAC;AAED,SAAS,qBAAqB,CAC5B,GAAM;IAEN,OAAO,CACL,GAAG,IAAI,IAAI;QACX,OAAO,GAAG,KAAK,QAAQ;QACvB,MAAM,CAAC,aAAa,IAAI,GAAG;QAC3B,OAAO,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,UAAU,CAChD,CAAA;AACH,CAAC","sourcesContent":["import { l } from '@atproto/lex-schema'\nimport {\n LexiconArray,\n LexiconArrayItems,\n LexiconDocument,\n LexiconError,\n LexiconObject,\n LexiconParameters,\n LexiconPayload,\n LexiconRef,\n LexiconRefUnion,\n} from './lexicon-document.js'\nimport { LexiconIndexer } from './lexicon-indexer.js'\n\n/**\n * Builds a validator for a given lexicon \"ref\" from a lexicon indexer.\n *\n * @example\n *\n * ```ts\n * import { LexiconSchemaBuilder } from '@atproto/lex/doc'\n * import { LexiconStreamIndexer } from '@atproto/lex/doc'\n *\n * const indexer = new LexiconStreamIndexer(lexiconDocs)\n * const validator = await LexiconSchemaBuilder.build(indexer, 'com.example.foo#bar')\n * ```\n */\nexport class LexiconSchemaBuilder {\n static async build(\n indexer: LexiconIndexer,\n fullRef: string,\n ): Promise<l.Validator<unknown>> {\n const ctx = new LexiconSchemaBuilder(indexer)\n try {\n const result = await ctx.buildFullRef(fullRef)\n if (!(result instanceof l.Schema)) {\n throw new Error(`Ref ${fullRef} is not a schema type`)\n }\n return result\n } finally {\n await ctx.done()\n }\n }\n\n static async buildAll(indexer: LexiconIndexer) {\n const builder = new LexiconSchemaBuilder(indexer)\n const schemas = new Map<\n string,\n | l.Validator<unknown>\n | l.Query\n | l.Subscription\n | l.Procedure\n | l.PermissionSet\n >()\n if (!isAsyncIterableObject(indexer)) {\n throw new Error('An iterable indexer is required to build all schemas')\n }\n try {\n for await (const doc of indexer) {\n for (const hash of Object.keys(doc.defs)) {\n const fullRef = `${doc.id}#${hash}`\n const schema = await builder.buildFullRef(fullRef)\n schemas.set(fullRef, schema)\n }\n }\n return schemas\n } finally {\n await builder.done()\n }\n }\n\n #asyncTasks = new AsyncTasks()\n\n constructor(protected indexer: LexiconIndexer) {}\n\n async done(): Promise<void> {\n await this.#asyncTasks.done()\n }\n\n buildFullRef = memoize(async (fullRef: string) => {\n const { nsid, hash } = parseRef(fullRef)\n\n const doc = await this.indexer.get(nsid)\n\n return this.compileDef(doc, hash)\n })\n\n protected buildRefGetter(fullRef: string): () => l.Validator<unknown> {\n let validator: l.Validator<unknown>\n\n this.#asyncTasks.add(\n this.buildFullRef(fullRef).then((v) => {\n if (!(v instanceof l.Schema)) {\n throw new Error(`Only refs to schema types are allowed`)\n }\n validator = v\n }),\n )\n\n return () => {\n if (validator) return validator\n throw new Error('Validator not yet built. Did you await done()?')\n }\n }\n\n protected buildTypedRefGetter(\n fullRef: string,\n ): () => l.TypedObjectSchema | l.RecordSchema {\n let validator: l.TypedObjectSchema | l.RecordSchema\n\n this.#asyncTasks.add(\n this.buildFullRef(fullRef).then((v) => {\n if (v instanceof l.TypedObjectSchema || v instanceof l.RecordSchema) {\n validator = v\n } else {\n throw new Error(\n 'Only refs to records and object definitions are allowed',\n )\n }\n }),\n )\n\n return () => {\n if (validator) return validator\n throw new Error('Validator not yet built. Did you await done()?')\n }\n }\n\n protected compileDef(doc: LexiconDocument, hash: string) {\n const def = Object.hasOwn(doc.defs, hash) ? doc.defs[hash] : null\n if (!def) {\n throw new Error(\n `No definition found for hash \"${JSON.stringify(hash)}\" in ${doc.id}`,\n )\n }\n switch (def.type) {\n case 'permission-set':\n return l.permissionSet(\n doc.id,\n def.permissions.map(({ resource, type, ...p }) =>\n l.permission(resource, p),\n ),\n def,\n )\n case 'procedure':\n return l.procedure(\n doc.id,\n this.compileParams(doc, def.parameters),\n this.compilePayload(doc, def.input),\n this.compilePayload(doc, def.output),\n this.compileErrors(doc, def.errors),\n )\n case 'query':\n return l.query(\n doc.id,\n this.compileParams(doc, def.parameters),\n this.compilePayload(doc, def.output),\n this.compileErrors(doc, def.errors),\n )\n case 'subscription':\n return l.subscription(\n doc.id,\n this.compileParams(doc, def.parameters),\n this.compilePayloadSchema(doc, def.message.schema),\n this.compileErrors(doc, def.errors),\n )\n case 'token':\n return l.token(doc.id, hash)\n case 'record':\n return l.record(def.key, doc.id, this.compileObject(doc, def.record))\n case 'object':\n return l.typedObject(doc.id, hash, this.compileObject(doc, def))\n default:\n return this.compileLeaf(doc, def)\n }\n }\n\n protected compileLeaf(\n doc: LexiconDocument,\n def: LexiconArray | LexiconArrayItems,\n ): l.Validator<unknown> {\n if (\n 'const' in def &&\n 'enum' in def &&\n def.enum != null &&\n def.const !== undefined &&\n !(def.enum as readonly unknown[]).includes(def.const)\n ) {\n return l.never()\n }\n\n switch (def.type) {\n case 'string': {\n const schema = l.string(def)\n if (def.default != null) schema.check(def.default)\n if (def.const != null) schema.check(def.const)\n if (def.enum != null) for (const v of def.enum) schema.check(v)\n\n const result =\n def.const != null\n ? l.literal(def.const, def)\n : def.enum != null\n ? l.enum(def.enum, def)\n : schema\n\n if (def.default != null) result.check(def.default)\n return result\n }\n case 'integer': {\n const schema = l.integer(def)\n if (def.default != null) schema.check(def.default)\n if (def.const != null) schema.check(def.const)\n if (def.enum != null) for (const v of def.enum) schema.check(v)\n\n const result =\n def.const != null\n ? l.literal(def.const, def)\n : def.enum != null\n ? l.enum(def.enum, def)\n : schema\n\n if (def.default != null) result.check(def.default)\n return result\n }\n case 'boolean': {\n const result =\n def.const != null ? l.literal(def.const, def) : l.boolean(def)\n\n if (def.default != null) result.check(def.default)\n return result\n }\n case 'blob':\n return l.blob(def)\n case 'cid-link':\n return l.cidLink()\n case 'bytes':\n return l.bytes(def)\n case 'unknown':\n return l.unknown()\n case 'array':\n return l.array(this.compileLeaf(doc, def.items), def)\n default:\n return this.compileRef(doc, def)\n }\n }\n\n protected compileRef(\n doc: LexiconDocument,\n def: LexiconRef | LexiconRefUnion,\n ) {\n switch (def.type) {\n case 'ref':\n return l.ref(this.buildRefGetter(buildFullRef(doc, def.ref)))\n case 'union':\n return l.typedUnion(\n def.refs.map((r) =>\n l.typedRef(this.buildTypedRefGetter(buildFullRef(doc, r))),\n ),\n def.closed ?? false,\n )\n default:\n // @ts-expect-error\n throw new Error(`Unknown lexicon type: ${def.type}`)\n }\n }\n\n protected compileObject(\n doc: LexiconDocument,\n def: LexiconObject,\n ): l.ObjectSchema {\n const props: Record<string, l.Validator> = {}\n for (const [key, propDef] of Object.entries(def.properties)) {\n if (propDef === undefined) continue\n\n const isNullable = def.nullable?.includes(key)\n const isRequired = def.required?.includes(key)\n\n let schema = this.compileLeaf(doc, propDef)\n\n if (isNullable) {\n schema = l.nullable(schema)\n }\n\n if (!isRequired) {\n schema = l.optional(schema)\n }\n\n props[key] = schema\n }\n return l.object(props)\n }\n\n protected compilePayload(\n doc: LexiconDocument,\n def: LexiconPayload | undefined,\n ): l.Payload {\n return l.payload(\n def?.encoding,\n def?.schema ? this.compilePayloadSchema(doc, def.schema) : undefined,\n )\n }\n\n protected compileErrors(\n _doc: LexiconDocument,\n errors?: readonly LexiconError[],\n ): undefined | string[] {\n return errors?.map((e) => e.name)\n }\n\n protected compilePayloadSchema(\n doc: LexiconDocument,\n def: LexiconObject | LexiconRef | LexiconRefUnion,\n ) {\n switch (def.type) {\n case 'object':\n return this.compileObject(doc, def)\n default:\n return this.compileRef(doc, def)\n }\n }\n\n protected compileParams(doc: LexiconDocument, def?: LexiconParameters) {\n if (!def) return l.params()\n\n const props: Record<string, l.Validator> = {}\n for (const [key, propDef] of Object.entries(def.properties)) {\n if (propDef === undefined) continue\n\n const isRequired = def.required?.includes(key)\n\n let schema = this.compileLeaf(doc, propDef)\n\n if (!isRequired) {\n schema = l.optional(schema)\n }\n\n props[key] = schema\n }\n return l.params(props)\n }\n}\n\nclass AsyncTasks {\n /**\n * A set that, eventually, contains only rejected promises.\n */\n #promises = new Set<Promise<void>>()\n\n async done(): Promise<void> {\n do {\n // @NOTE this is going to throw on the first rejected promise (which is\n // what we want)\n for (const p of this.#promises) await p\n // At this point, all settled promises should have been removed. If\n // this.#promises is not empty, it means new promises were added during\n // the awaiting process, so we loop again.\n } while (this.#promises.size > 0)\n }\n\n add(p: Promise<void>) {\n const promise = Promise.resolve(p).then(() => {\n // No need to keep the promise any longer\n this.#promises.delete(promise)\n })\n\n void promise.catch((_err) => {\n // ignore errors here, they should be caught though done()\n })\n\n this.#promises.add(promise)\n }\n}\n\nfunction parseRef(fullRef: string) {\n const { length, 0: nsid, 1: hash } = fullRef.split('#')\n if (length !== 2) throw new Error('Uri can only have one hash segment')\n if (!nsid || !hash) throw new Error('Invalid ref, missing hash')\n return { nsid, hash }\n}\n\nfunction buildFullRef(from: LexiconDocument, ref: string) {\n if (ref.startsWith('#')) return `${from.id}${ref}`\n return ref\n}\n\nexport function memoize<Fn extends (arg: string) => unknown>(fn: Fn): Fn {\n const cache = new Map<string, ReturnType<Fn>>()\n return ((arg: string) => {\n if (cache.has(arg)) return cache.get(arg)!\n const result = fn(arg) as ReturnType<Fn>\n cache.set(arg, result)\n return result\n }) as Fn\n}\n\nfunction isAsyncIterableObject<T>(\n obj: T,\n): obj is T & object & AsyncIterable<unknown> {\n return (\n obj != null &&\n typeof obj === 'object' &&\n Symbol.asyncIterator in obj &&\n typeof obj[Symbol.asyncIterator] === 'function'\n )\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lex-document",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "license": "MIT",
5
5
  "description": "Lexicon document validation tools for AT",
6
6
  "keywords": [
@@ -29,23 +29,23 @@
29
29
  "types": "./dist/index.d.ts",
30
30
  "exports": {
31
31
  ".": {
32
+ "types": "./dist/index.d.ts",
32
33
  "browser": "./dist/index.js",
33
34
  "import": "./dist/index.js",
34
- "require": "./dist/index.js",
35
- "types": "./dist/index.d.ts"
35
+ "require": "./dist/index.js"
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
39
  "core-js": "^3",
40
40
  "tslib": "^2.8.1",
41
- "@atproto/lex-schema": "0.0.4"
41
+ "@atproto/lex-schema": "0.0.6"
42
42
  },
43
43
  "devDependencies": {
44
- "jest": "^28.1.2",
45
- "@atproto/lex-data": "0.0.3"
44
+ "vitest": "^4.0.16",
45
+ "@atproto/lex-data": "0.0.5"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsc --build tsconfig.build.json",
49
- "test": "jest"
49
+ "test": "vitest run"
50
50
  }
51
51
  }
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { lexiconDocumentSchema } from './lexicon-document.js'
2
3
 
3
4
  describe('lexiconDocumentSchema', () => {
@@ -49,7 +50,7 @@ describe('lexiconDocumentSchema', () => {
49
50
  }),
50
51
  ).toMatchObject({
51
52
  success: false,
52
- error: { issues: [{ code: 'invalid_value', values: [1] }] },
53
+ reason: { issues: [{ code: 'invalid_value', values: [1] }] },
53
54
  })
54
55
  })
55
56
 
@@ -66,7 +67,7 @@ describe('lexiconDocumentSchema', () => {
66
67
  }),
67
68
  ).toMatchObject({
68
69
  success: false,
69
- error: { issues: [{ code: 'invalid_format', format: 'nsid' }] },
70
+ reason: { issues: [{ code: 'invalid_format', format: 'nsid' }] },
70
71
  })
71
72
  })
72
73
 
@@ -83,7 +84,7 @@ describe('lexiconDocumentSchema', () => {
83
84
  }),
84
85
  ).toMatchObject({
85
86
  success: false,
86
- error: { issues: [{ code: 'invalid_type', expected: ['string'] }] },
87
+ reason: { issues: [{ code: 'invalid_type', expected: ['string'] }] },
87
88
  })
88
89
  })
89
90
 
@@ -104,7 +105,7 @@ describe('lexiconDocumentSchema', () => {
104
105
  }),
105
106
  ).toMatchObject({
106
107
  success: false,
107
- error: {
108
+ reason: {
108
109
  issues: [{ code: 'custom', path: ['defs', 'demo', 'required'] }],
109
110
  },
110
111
  })
@@ -1,3 +1,4 @@
1
+ import { beforeAll, describe, expect, it } from 'vitest'
1
2
  import { parseCid } from '@atproto/lex-data'
2
3
  import { l } from '@atproto/lex-schema'
3
4
  import { LexiconDocument, lexiconDocumentSchema } from './lexicon-document.js'
@@ -160,7 +161,7 @@ describe('LexiconSchemaBuilder', () => {
160
161
 
161
162
  expect(schema.safeParse(value)).toMatchObject({
162
163
  success: false,
163
- error: {
164
+ reason: {
164
165
  issues: [
165
166
  {
166
167
  code: 'invalid_value',
@@ -231,7 +232,7 @@ describe('LexiconSchemaBuilder', () => {
231
232
 
232
233
  expect(schema.safeParse(value)).toMatchObject({
233
234
  success: false,
234
- error: { issues: [{ code: 'required_key', key: 'array' }] },
235
+ reason: { issues: [{ code: 'required_key', key: 'array' }] },
235
236
  })
236
237
  })
237
238
 
@@ -161,7 +161,7 @@ export class LexiconSchemaBuilder {
161
161
  return l.subscription(
162
162
  doc.id,
163
163
  this.compileParams(doc, def.parameters),
164
- this.compilePayloadSchema(doc, def.message?.schema),
164
+ this.compilePayloadSchema(doc, def.message.schema),
165
165
  this.compileErrors(doc, def.errors),
166
166
  )
167
167
  case 'token':
@@ -191,35 +191,43 @@ export class LexiconSchemaBuilder {
191
191
 
192
192
  switch (def.type) {
193
193
  case 'string': {
194
- const schema: l.StringSchema = l.string(def)
195
- if (def.const != null) {
196
- schema.assert(def.const)
197
- return l.literal(def.const, def)
198
- } else if (def.enum != null) {
199
- for (const v of def.enum) schema.assert(v)
200
- return l.enum(def.enum, def)
201
- } else {
202
- return schema
203
- }
194
+ const schema = l.string(def)
195
+ if (def.default != null) schema.check(def.default)
196
+ if (def.const != null) schema.check(def.const)
197
+ if (def.enum != null) for (const v of def.enum) schema.check(v)
198
+
199
+ const result =
200
+ def.const != null
201
+ ? l.literal(def.const, def)
202
+ : def.enum != null
203
+ ? l.enum(def.enum, def)
204
+ : schema
205
+
206
+ if (def.default != null) result.check(def.default)
207
+ return result
204
208
  }
205
209
  case 'integer': {
206
- const schema: l.IntegerSchema = l.integer(def)
207
- if (def.const != null) {
208
- schema.assert(def.const)
209
- return l.literal(def.const, def)
210
- } else if (def.enum != null) {
211
- for (const v of def.enum) schema.assert(v)
212
- return l.enum(def.enum, def)
213
- } else {
214
- return schema
215
- }
210
+ const schema = l.integer(def)
211
+ if (def.default != null) schema.check(def.default)
212
+ if (def.const != null) schema.check(def.const)
213
+ if (def.enum != null) for (const v of def.enum) schema.check(v)
214
+
215
+ const result =
216
+ def.const != null
217
+ ? l.literal(def.const, def)
218
+ : def.enum != null
219
+ ? l.enum(def.enum, def)
220
+ : schema
221
+
222
+ if (def.default != null) result.check(def.default)
223
+ return result
216
224
  }
217
225
  case 'boolean': {
218
- if (def.const != null) {
219
- return l.literal(def.const, def)
220
- } else {
221
- return l.boolean(def)
222
- }
226
+ const result =
227
+ def.const != null ? l.literal(def.const, def) : l.boolean(def)
228
+
229
+ if (def.default != null) result.check(def.default)
230
+ return result
223
231
  }
224
232
  case 'blob':
225
233
  return l.blob(def)
@@ -301,9 +309,8 @@ export class LexiconSchemaBuilder {
301
309
 
302
310
  protected compilePayloadSchema(
303
311
  doc: LexiconDocument,
304
- def?: LexiconObject | LexiconRef | LexiconRefUnion,
312
+ def: LexiconObject | LexiconRef | LexiconRefUnion,
305
313
  ) {
306
- if (!def) return undefined
307
314
  switch (def.type) {
308
315
  case 'object':
309
316
  return this.compileObject(doc, def)
@@ -1,6 +1,6 @@
1
1
  {
2
- "extends": "../../../tsconfig/tests.json",
3
- "include": ["./tests", "./src/**.test.ts"],
2
+ "extends": "../../../tsconfig/vitest.json",
3
+ "include": ["./tests", "./src/**/*.test.ts"],
4
4
  "compilerOptions": {
5
5
  "noImplicitAny": true,
6
6
  "rootDir": "./",