@fedify/vocab-tools 2.5.0-dev.2619 → 2.5.0-dev.2687

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/src/schema.ts CHANGED
@@ -188,69 +188,73 @@ export interface PropertyPreprocessorSchema {
188
188
  function: string;
189
189
  }
190
190
 
191
- export type PropertySchemaTyping = {
192
- /**
193
- * Whether the property value has `@type` field. If `true`, the `range` must
194
- * have only one element.
195
- */
196
- untyped?: false;
197
-
198
- /**
199
- * The qualified URIs of all possible types of the property values.
200
- */
201
- range: [TypeUri] | [TypeUri, ...TypeUri[]];
202
- } | {
203
- /**
204
- * Whether the property value has `@type` field. If `true`, the `range` must
205
- * have only one element.
206
- */
207
- untyped: true;
208
-
209
- /**
210
- * The qualified URIs of all possible types of the property values.
211
- */
212
- range: [TypeUri];
213
- };
191
+ export type PropertySchemaTyping =
192
+ | {
193
+ /**
194
+ * Whether the property value has `@type` field. If `true`, the `range` must
195
+ * have only one element.
196
+ */
197
+ untyped?: false;
198
+
199
+ /**
200
+ * The qualified URIs of all possible types of the property values.
201
+ */
202
+ range: [TypeUri] | [TypeUri, ...TypeUri[]];
203
+ }
204
+ | {
205
+ /**
206
+ * Whether the property value has `@type` field. If `true`, the `range` must
207
+ * have only one element.
208
+ */
209
+ untyped: true;
210
+
211
+ /**
212
+ * The qualified URIs of all possible types of the property values.
213
+ */
214
+ range: [TypeUri];
215
+ };
214
216
 
215
217
  /**
216
218
  * The schema of a property. It is used to generate property accessors of
217
219
  * a class.
218
220
  */
219
221
  export type PropertySchema =
220
- | PropertySchemaBase & PropertySchemaTyping & {
221
- /**
222
- * Marks the property that it can have only one value. Turning on this
223
- * flag will generate only singular property accessors, so `pluralName`
224
- * and `singularAccessor` should not be specified.
225
- */
226
- functional?: false;
227
-
228
- /**
229
- * The plural form of the property name. It is used as the name of the
230
- * generated property accessors.
231
- */
232
- pluralName: string;
233
-
234
- /**
235
- * Whether to generate singular property accessors. Regardless of this
236
- * flag, plural property accessors are generated (unless `functional` is
237
- * turned on).
238
- */
239
- singularAccessor?: boolean;
240
-
241
- /**
242
- * The container type of the property values. It can be unspecified.
243
- */
244
- container?: "graph" | "list";
245
- }
246
- | PropertySchemaBase & PropertySchemaTyping & {
247
- /**
248
- * Marks the property that it can have only one value. Turning on this
249
- * flag will generate only singular property accessors, so `pluralName`
250
- * and `singularAccessor` should not be specified.
251
- */
252
- functional: true;
253
- };
222
+ | (PropertySchemaBase &
223
+ PropertySchemaTyping & {
224
+ /**
225
+ * Marks the property that it can have only one value. Turning on this
226
+ * flag will generate only singular property accessors, so `pluralName`
227
+ * and `singularAccessor` should not be specified.
228
+ */
229
+ functional?: false;
230
+
231
+ /**
232
+ * The plural form of the property name. It is used as the name of the
233
+ * generated property accessors.
234
+ */
235
+ pluralName: string;
236
+
237
+ /**
238
+ * Whether to generate singular property accessors. Regardless of this
239
+ * flag, plural property accessors are generated (unless `functional` is
240
+ * turned on).
241
+ */
242
+ singularAccessor?: boolean;
243
+
244
+ /**
245
+ * The container type of the property values. It can be unspecified.
246
+ */
247
+ container?: "graph" | "list";
248
+ })
249
+ | (PropertySchemaBase &
250
+ PropertySchemaTyping & {
251
+ /**
252
+ * Marks the property that it can have only one value. Turning on this
253
+ * flag will generate only singular property accessors, so `pluralName`
254
+ * and `singularAccessor` should not be specified.
255
+ */
256
+ functional: true;
257
+ });
254
258
 
255
259
  /**
256
260
  * A JSON-LD context, which is placed in the `@context` property of a JSON-LD
@@ -283,12 +287,13 @@ interface ContainerTermDefinition {
283
287
  */
284
288
  export function isNonFunctionalProperty(
285
289
  property: PropertySchema,
286
- ): property is PropertySchemaBase & PropertySchemaTyping & {
287
- functional?: false;
288
- pluralName: string;
289
- singularAccessor?: boolean;
290
- container?: "graph" | "list";
291
- } {
290
+ ): property is PropertySchemaBase &
291
+ PropertySchemaTyping & {
292
+ functional?: false;
293
+ pluralName: string;
294
+ singularAccessor?: boolean;
295
+ container?: "graph" | "list";
296
+ } {
292
297
  return property.functional !== true;
293
298
  }
294
299
 
@@ -297,8 +302,9 @@ export function isNonFunctionalProperty(
297
302
  */
298
303
  export function hasSingularAccessor(property: PropertySchema): boolean {
299
304
  if (property.functional === true) return true;
300
- return isNonFunctionalProperty(property) &&
301
- property.singularAccessor === true;
305
+ return (
306
+ isNonFunctionalProperty(property) && property.singularAccessor === true
307
+ );
302
308
  }
303
309
 
304
310
  const XSD_STRING_URI = "http://www.w3.org/2001/XMLSchema#string";
@@ -316,9 +322,7 @@ const FEDIFY_VOCAB_ENTITY_TYPE_URI = "fedify:vocabEntityType";
316
322
  * @param types The loaded type schemas to validate.
317
323
  * @throws {TypeError} Thrown when an unsupported range combination is found.
318
324
  */
319
- export function validateTypeSchemas(
320
- types: Record<string, TypeSchema>,
321
- ): void {
325
+ export function validateTypeSchemas(types: Record<string, TypeSchema>): void {
322
326
  for (const type of Object.values(types)) {
323
327
  for (const property of type.properties) {
324
328
  const hasString = property.range.includes(XSD_STRING_URI);
@@ -393,7 +397,7 @@ async function loadSchema(path: string): Promise<TypeSchema> {
393
397
  new SchemaError(path, `${path}:${e.instanceLocation}: ${e.error}`),
394
398
  );
395
399
  }
396
- throw new AggregateError(errors);
400
+ throw new AggregateError(errors, "Invalid schema data");
397
401
  }
398
402
 
399
403
  /**
@@ -420,7 +424,8 @@ export async function loadSchemaFiles(
420
424
  schema = await loadSchema(path);
421
425
  } catch (e) {
422
426
  if (
423
- e instanceof AggregateError && e.errors.length > 0 &&
427
+ e instanceof AggregateError &&
428
+ e.errors.length > 0 &&
424
429
  e.errors[0] instanceof SchemaError
425
430
  ) {
426
431
  errors.push(...e.errors);
@@ -430,8 +435,10 @@ export async function loadSchemaFiles(
430
435
  }
431
436
  result[schema.uri] = schema;
432
437
  }
433
- if (errors.length > 0) throw new AggregateError(errors);
438
+ if (errors.length > 0) {
439
+ throw new AggregateError(errors, "Failed to load schema files");
440
+ }
434
441
  const entries = Object.entries(result);
435
- entries.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
442
+ entries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
436
443
  return Object.fromEntries(entries);
437
444
  }
package/src/type.ts CHANGED
@@ -424,7 +424,8 @@ const scalarTypes: Record<string, ScalarType> = {
424
424
  },
425
425
  },
426
426
  "fedify:proofPurpose": {
427
- name: '"assertionMethod" | "authentication" | "capabilityInvocation" | ' +
427
+ name:
428
+ '"assertionMethod" | "authentication" | "capabilityInvocation" | ' +
428
429
  '"capabilityDelegation" | "keyAgreement"',
429
430
  typeGuard(v) {
430
431
  return `${v} === "assertionMethod" || ${v} === "authentication" ||
@@ -455,8 +456,10 @@ const scalarTypes: Record<string, ScalarType> = {
455
456
  "fedify:units": {
456
457
  name: '"cm" | "feet" | "inches" | "km" | "m" | "miles"',
457
458
  typeGuard(v) {
458
- return `${v} == "cm" || ${v} == "feet" || ${v} == "inches" ` +
459
- `|| ${v} == "km" || ${v} == "m" || ${v} == "miles"`;
459
+ return (
460
+ `${v} == "cm" || ${v} == "feet" || ${v} == "inches" ` +
461
+ `|| ${v} == "km" || ${v} == "m" || ${v} == "miles"`
462
+ );
460
463
  },
461
464
  encoder(v) {
462
465
  return `{ "@value": ${v} }`;
@@ -465,10 +468,12 @@ const scalarTypes: Record<string, ScalarType> = {
465
468
  return v;
466
469
  },
467
470
  dataCheck(v) {
468
- return `typeof ${v} === "object" && "@value" in ${v}
471
+ return (
472
+ `typeof ${v} === "object" && "@value" in ${v}
469
473
  && (${v}["@value"] == "cm" || ${v}["@value"] == "feet" ` +
470
474
  `|| ${v}["@value"] == "inches" || ${v}["@value"] == "km" ` +
471
- `|| ${v}["@value"] == "m" || ${v}["@value"] == "miles")`;
475
+ `|| ${v}["@value"] == "m" || ${v}["@value"] == "miles")`
476
+ );
472
477
  },
473
478
  decoder(v) {
474
479
  return `${v}["@value"]`;
@@ -557,12 +562,14 @@ export function isCompactableType(
557
562
  return false;
558
563
  }
559
564
  const defaultContext = type.defaultContext;
560
- return defaultContext != null &&
561
- HEURISTICS_CONTEXTS.includes(defaultContext as string) ||
562
- Array.isArray(defaultContext) &&
565
+ return (
566
+ (defaultContext != null &&
567
+ HEURISTICS_CONTEXTS.includes(defaultContext as string)) ||
568
+ (Array.isArray(defaultContext) &&
563
569
  (defaultContext as string[]).some(
564
570
  HEURISTICS_CONTEXTS.includes.bind(HEURISTICS_CONTEXTS),
565
- );
571
+ ))
572
+ );
566
573
  }
567
574
  throw new Error(`Unknown type: ${typeUri}`);
568
575
  }
@@ -611,8 +618,8 @@ export function getEncoder(
611
618
  ): string {
612
619
  if (typeUri in scalarTypes) {
613
620
  return compact
614
- ? scalarTypes[typeUri].compactEncoder?.(variable) ??
615
- scalarTypes[typeUri].encoder(variable)
621
+ ? (scalarTypes[typeUri].compactEncoder?.(variable) ??
622
+ scalarTypes[typeUri].encoder(variable))
616
623
  : scalarTypes[typeUri].encoder(variable);
617
624
  }
618
625
  if (typeUri in types) {
@@ -671,10 +678,7 @@ export function getDecoder(
671
678
  baseUrlVariable: string,
672
679
  ): string {
673
680
  if (typeUri in scalarTypes) {
674
- return scalarTypes[typeUri].decoder(
675
- variable,
676
- baseUrlVariable,
677
- );
681
+ return scalarTypes[typeUri].decoder(variable, baseUrlVariable);
678
682
  }
679
683
  if (typeUri in types) {
680
684
  return `await ${types[typeUri].name}.fromJsonLd(
@@ -697,12 +701,14 @@ export function getDataCheck(
697
701
  if (typeUri in scalarTypes) return scalarTypes[typeUri].dataCheck(variable);
698
702
  if (typeUri in types) {
699
703
  const subtypes = getSubtypes(typeUri, types);
700
- return `typeof ${variable} === "object" && "@type" in ${variable}
704
+ return (
705
+ `typeof ${variable} === "object" && "@type" in ${variable}
701
706
  && Array.isArray(${variable}["@type"])` +
702
707
  (subtypes.length > 1
703
708
  ? `&& ${JSON.stringify(subtypes)}.some(
704
709
  t => ${variable}["@type"].includes(t))`
705
- : `&& ${variable}["@type"].includes(${JSON.stringify(typeUri)})`);
710
+ : `&& ${variable}["@type"].includes(${JSON.stringify(typeUri)})`)
711
+ );
706
712
  }
707
713
  throw new Error(`Unknown type: ${typeUri}`);
708
714
  }
package/tsdown.config.ts CHANGED
@@ -15,13 +15,15 @@ export default defineConfig({
15
15
  hooks: {
16
16
  "build:done": async (ctx) => {
17
17
  // Each hook reports its own failure; a later watch rebuild can retry.
18
- schemaCopy = schemaCopy.catch(() => {}).then(() =>
19
- cp(
20
- join("src", "schema.yaml"),
21
- join(ctx.options.outDir, "schema.yaml"),
22
- { force: false, errorOnExist: false },
23
- )
24
- );
18
+ schemaCopy = schemaCopy
19
+ .catch(() => {})
20
+ .then(() =>
21
+ cp(
22
+ join("src", "schema.yaml"),
23
+ join(ctx.options.outDir, "schema.yaml"),
24
+ { force: false, errorOnExist: false },
25
+ ),
26
+ );
25
27
  await schemaCopy;
26
28
  },
27
29
  },