@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/deno.json +2 -2
- package/dist/mod.cjs +6 -5
- package/dist/mod.d.cts +3 -3
- package/dist/mod.d.ts +3 -3
- package/dist/mod.js +6 -5
- package/package.json +3 -3
- package/src/__snapshots__/class.test.ts.deno.snap +1 -0
- package/src/__snapshots__/class.test.ts.node.snap +1 -0
- package/src/__snapshots__/class.test.ts.snap +1 -0
- package/src/class.test.ts +43 -46
- package/src/class.ts +21 -15
- package/src/codec.test.ts +281 -248
- package/src/codec.ts +14 -22
- package/src/constructor.ts +20 -17
- package/src/deep-clone.ts +3 -2
- package/src/field.ts +5 -3
- package/src/inspector.ts +4 -3
- package/src/property.ts +64 -63
- package/src/schema.test.ts +60 -62
- package/src/schema.ts +79 -72
- package/src/type.ts +23 -17
- package/tsdown.config.ts +9 -7
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
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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 &
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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 &
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
|
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 &&
|
|
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)
|
|
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:
|
|
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
|
|
459
|
-
|
|
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
|
|
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
|
|
561
|
-
|
|
562
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
},
|