@fedify/vocab-tools 2.5.0-dev.2664 → 2.5.0-dev.2710
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/dist/mod.cjs +822 -99
- package/dist/mod.d.cts +3 -3
- package/dist/mod.d.ts +3 -3
- package/dist/mod.js +822 -99
- package/package.json +19 -2
- package/deno.json +0 -32
- package/src/__snapshots__/class.test.ts.deno.snap +0 -169445
- package/src/__snapshots__/class.test.ts.node.snap +0 -169443
- package/src/__snapshots__/class.test.ts.snap +0 -169445
- package/src/class.test.ts +0 -302
- package/src/class.ts +0 -486
- package/src/codec.test.ts +0 -479
- package/src/codec.ts +0 -703
- package/src/constructor.ts +0 -464
- package/src/deep-clone.ts +0 -175
- package/src/field.ts +0 -46
- package/src/fs.test.ts +0 -31
- package/src/fs.ts +0 -21
- package/src/generate.ts +0 -24
- package/src/inspector.ts +0 -116
- package/src/mod.ts +0 -8
- package/src/property-array.ts +0 -198
- package/src/property.ts +0 -754
- package/src/schema.test.ts +0 -391
- package/src/schema.ts +0 -439
- package/src/schema.yaml +0 -336
- package/src/type.ts +0 -738
- package/tsconfig.check.json +0 -12
- package/tsdown.config.ts +0 -28
package/src/schema.ts
DELETED
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
import { type Schema as JsonSchema, Validator } from "@cfworker/json-schema";
|
|
2
|
-
import { readFile } from "node:fs/promises";
|
|
3
|
-
import { join, posix as url } from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { parse } from "yaml";
|
|
6
|
-
import { readDirRecursive } from "./fs.ts";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The qualified URI of a type. It is used as the range of a property.
|
|
10
|
-
*/
|
|
11
|
-
export type TypeUri =
|
|
12
|
-
| `https://${string}`
|
|
13
|
-
| `http://${string}`
|
|
14
|
-
| `fedify:${string}`;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The schema of a type. It is used to generate a class.
|
|
18
|
-
*/
|
|
19
|
-
export interface TypeSchema {
|
|
20
|
-
/**
|
|
21
|
-
* The type name. It is used as the name of the generated class.
|
|
22
|
-
*/
|
|
23
|
-
name: string;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The qualified URI of the type.
|
|
27
|
-
*/
|
|
28
|
-
uri: TypeUri;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The qualified URIs of the base type of the type (if any).
|
|
32
|
-
*/
|
|
33
|
-
extends?: TypeUri;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The type name used in the compacted JSON-LD document. It is used as the
|
|
37
|
-
* value of the `type` field.
|
|
38
|
-
*/
|
|
39
|
-
compactName?: string;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Marks the type an entity type rather than a value type. Turning on this
|
|
43
|
-
* flag will make property accessors for the type asynchronous, so that they
|
|
44
|
-
* can load the values of the properties from the remote server.
|
|
45
|
-
*
|
|
46
|
-
* The extended subtypes must have the consistent value of this flag.
|
|
47
|
-
*/
|
|
48
|
-
entity: boolean;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Whether this type's id can establish same-origin trust for its own
|
|
52
|
-
* embedded entity-valued properties. Defaults to true. Set to false for
|
|
53
|
-
* metadata whose id does not identify an independently fetched resource.
|
|
54
|
-
* Locally constructed and explicitly trusted values remain trusted.
|
|
55
|
-
*/
|
|
56
|
-
trustEmbeddedObjects?: boolean;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Whether the type omits `@type` in JSON-LD serialization. When `true`,
|
|
60
|
-
* the generated `toJsonLd()` method will not emit `@type` (or `type` in
|
|
61
|
-
* compact form) in the serialized JSON-LD. The generated `fromJsonLd()`
|
|
62
|
-
* method will still accept `@type` if present for backward compatibility.
|
|
63
|
-
*
|
|
64
|
-
* This is useful for types that are not real vocabulary types but rather
|
|
65
|
-
* anonymous object structures (e.g., `Endpoints`, `Source` in ActivityStreams).
|
|
66
|
-
*/
|
|
67
|
-
typeless?: boolean;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* The description of the type. It is used as the doc comment of
|
|
71
|
-
* the generated class.
|
|
72
|
-
*/
|
|
73
|
-
description: string;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* The possible properties of the type.
|
|
77
|
-
*/
|
|
78
|
-
properties: PropertySchema[];
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The default JSON-LD context of the type. It is used as the default
|
|
82
|
-
* context of the generated `toJsonLd()` method.
|
|
83
|
-
*/
|
|
84
|
-
defaultContext: Context;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface PropertySchemaBase {
|
|
88
|
-
/**
|
|
89
|
-
* Synonyms read after the canonical property, in declaration order.
|
|
90
|
-
* Deserialization selects the first non-empty raw value set before decoding
|
|
91
|
-
* its values and containers; it never merges synonyms or retries invalid
|
|
92
|
-
* values. Serialization writes all synonyms by default.
|
|
93
|
-
*/
|
|
94
|
-
redundantProperties?: {
|
|
95
|
-
/** The qualified URI of the synonym. */
|
|
96
|
-
uri: string;
|
|
97
|
-
|
|
98
|
-
/** The synonym's key in the default compact JSON-LD context. */
|
|
99
|
-
compactName?: string;
|
|
100
|
-
}[];
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Whether serialization writes every synonym or only the canonical property.
|
|
104
|
-
* Defaults to `"all"`. This does not change deserialization precedence or
|
|
105
|
-
* the existing cache of parsed JSON-LD documents.
|
|
106
|
-
*/
|
|
107
|
-
redundantPropertiesWrite?: "all" | "canonical";
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* A context URL added to the default serialization context when this
|
|
111
|
-
* property's expanded IRI appears in the compacted document. Explicit
|
|
112
|
-
* caller contexts are unchanged. Populated properties with this option
|
|
113
|
-
* use the JSON-LD processor instead of compact serialization heuristics.
|
|
114
|
-
*/
|
|
115
|
-
extraContext?: string;
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* The singular form of the property name. It is used as the name of the
|
|
119
|
-
* generated property accessors.
|
|
120
|
-
*/
|
|
121
|
-
singularName: string;
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* The qualified URI of the property.
|
|
125
|
-
*/
|
|
126
|
-
uri: string;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* The property name used in the compacted JSON-LD document. It is used as
|
|
130
|
-
* the key of the property.
|
|
131
|
-
*/
|
|
132
|
-
compactName?: string;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* The qualified URI of the superproperty of the property (if any).
|
|
136
|
-
* It means that the property is a specialization of the referenced property.
|
|
137
|
-
*/
|
|
138
|
-
subpropertyOf?: string;
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* The description of the property. It is used as the doc comment of
|
|
142
|
-
* the generated property accessors.
|
|
143
|
-
*/
|
|
144
|
-
description: string;
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Whether the enclosed object should have its own context when the document
|
|
148
|
-
* is compacted.
|
|
149
|
-
*/
|
|
150
|
-
embedContext?: {
|
|
151
|
-
/**
|
|
152
|
-
* The compact name of the property that contains the context.
|
|
153
|
-
*/
|
|
154
|
-
compactName: string;
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Whether the embedded context should be the same as the context of
|
|
158
|
-
* the enclosing document.
|
|
159
|
-
*/
|
|
160
|
-
inherit: true;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Preprocessors for this property. Each preprocessor receives an expanded
|
|
165
|
-
* JSON-LD property value and may return a vocabulary object matching the
|
|
166
|
-
* property's declared range, `undefined` when it did not handle the value,
|
|
167
|
-
* or an `Error` when it recognized the value but failed while converting it.
|
|
168
|
-
*
|
|
169
|
-
* `module` is resolved from the generated vocabulary source file
|
|
170
|
-
* and imported statically at the top of the generated file.
|
|
171
|
-
*/
|
|
172
|
-
preprocessors?: PropertyPreprocessorSchema[];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* A schema for a property preprocessor that normalizes wire-level values
|
|
177
|
-
* before the generated range decoder runs.
|
|
178
|
-
*/
|
|
179
|
-
export interface PropertyPreprocessorSchema {
|
|
180
|
-
/**
|
|
181
|
-
* Module specifier resolved from the generated vocabulary source file.
|
|
182
|
-
*/
|
|
183
|
-
module: string;
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* The name of the exported function in the module.
|
|
187
|
-
*/
|
|
188
|
-
function: string;
|
|
189
|
-
}
|
|
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
|
-
};
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* The schema of a property. It is used to generate property accessors of
|
|
217
|
-
* a class.
|
|
218
|
-
*/
|
|
219
|
-
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
|
-
};
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* A JSON-LD context, which is placed in the `@context` property of a JSON-LD
|
|
257
|
-
* document.
|
|
258
|
-
*/
|
|
259
|
-
export type Context = Uri | EmbeddedContext | (Uri | EmbeddedContext)[];
|
|
260
|
-
|
|
261
|
-
type Uri = "http://{string}" | "https://{string}";
|
|
262
|
-
type EmbeddedContext = Record<string, TermDefinition>;
|
|
263
|
-
type TermDefinition =
|
|
264
|
-
| Uri
|
|
265
|
-
| Record<string, Uri | "@id">
|
|
266
|
-
| ContainerTermDefinition;
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* A term definition that carries a JSON-LD `@container` keyword.
|
|
270
|
-
*
|
|
271
|
-
* The JSON Schema in *schema.yaml* enforces this shape conditionally whenever
|
|
272
|
-
* an embedded context term has `@container`; the TypeScript union cannot
|
|
273
|
-
* express the conditional and serves only as a guide.
|
|
274
|
-
*/
|
|
275
|
-
interface ContainerTermDefinition {
|
|
276
|
-
"@id": string;
|
|
277
|
-
"@type"?: Uri | "@id";
|
|
278
|
-
"@container": "@set" | "@list";
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Type guard to check if a property is not functional (has pluralName).
|
|
283
|
-
*/
|
|
284
|
-
export function isNonFunctionalProperty(
|
|
285
|
-
property: PropertySchema,
|
|
286
|
-
): property is PropertySchemaBase & PropertySchemaTyping & {
|
|
287
|
-
functional?: false;
|
|
288
|
-
pluralName: string;
|
|
289
|
-
singularAccessor?: boolean;
|
|
290
|
-
container?: "graph" | "list";
|
|
291
|
-
} {
|
|
292
|
-
return property.functional !== true;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Type guard to check if a property has singular accessor.
|
|
297
|
-
*/
|
|
298
|
-
export function hasSingularAccessor(property: PropertySchema): boolean {
|
|
299
|
-
if (property.functional === true) return true;
|
|
300
|
-
return isNonFunctionalProperty(property) &&
|
|
301
|
-
property.singularAccessor === true;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const XSD_STRING_URI = "http://www.w3.org/2001/XMLSchema#string";
|
|
305
|
-
const XSD_DECIMAL_URI = "http://www.w3.org/2001/XMLSchema#decimal";
|
|
306
|
-
const FEDIFY_VOCAB_ENTITY_TYPE_URI = "fedify:vocabEntityType";
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Validates schema combinations that cannot be represented safely by the
|
|
310
|
-
* generated code.
|
|
311
|
-
*
|
|
312
|
-
* In particular, `xsd:string` and `xsd:decimal` cannot coexist in the same
|
|
313
|
-
* property range because both are represented as runtime strings, which makes
|
|
314
|
-
* JSON-LD serialization ambiguous and order-dependent.
|
|
315
|
-
*
|
|
316
|
-
* @param types The loaded type schemas to validate.
|
|
317
|
-
* @throws {TypeError} Thrown when an unsupported range combination is found.
|
|
318
|
-
*/
|
|
319
|
-
export function validateTypeSchemas(
|
|
320
|
-
types: Record<string, TypeSchema>,
|
|
321
|
-
): void {
|
|
322
|
-
for (const type of Object.values(types)) {
|
|
323
|
-
for (const property of type.properties) {
|
|
324
|
-
const hasString = property.range.includes(XSD_STRING_URI);
|
|
325
|
-
const hasDecimal = property.range.includes(XSD_DECIMAL_URI);
|
|
326
|
-
if (hasString && hasDecimal) {
|
|
327
|
-
throw new TypeError(
|
|
328
|
-
`The property ${type.name}.${property.singularName} cannot have ` +
|
|
329
|
-
`both xsd:string and xsd:decimal in its range because the ` +
|
|
330
|
-
`generated encoder cannot disambiguate them at runtime.`,
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
|
-
if (
|
|
334
|
-
property.range.includes(FEDIFY_VOCAB_ENTITY_TYPE_URI) &&
|
|
335
|
-
property.range.length > 1
|
|
336
|
-
) {
|
|
337
|
-
throw new TypeError(
|
|
338
|
-
`The property ${type.name}.${property.singularName} cannot mix ` +
|
|
339
|
-
`fedify:vocabEntityType with other range types because the ` +
|
|
340
|
-
`generated decoder cannot disambiguate entity type references ` +
|
|
341
|
-
`from ordinary IRIs.`,
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* An error that occurred while loading a schema file.
|
|
350
|
-
*/
|
|
351
|
-
export class SchemaError extends Error {
|
|
352
|
-
/**
|
|
353
|
-
* The path of the schema file.
|
|
354
|
-
*/
|
|
355
|
-
readonly path: string;
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Constructs a new `SchemaError`.
|
|
359
|
-
* @param path The path of the schema file.
|
|
360
|
-
* @param message The error message.
|
|
361
|
-
*/
|
|
362
|
-
constructor(path: string, message?: string) {
|
|
363
|
-
super(message);
|
|
364
|
-
this.path = path;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
async function loadSchemaValidator(): Promise<Validator> {
|
|
369
|
-
const thisFile = import.meta.url;
|
|
370
|
-
const schemaFile = new URL(url.join(url.dirname(thisFile), "schema.yaml"));
|
|
371
|
-
let content: string;
|
|
372
|
-
if (schemaFile.protocol !== "file:") {
|
|
373
|
-
const response = await fetch(schemaFile);
|
|
374
|
-
content = await response.text();
|
|
375
|
-
} else {
|
|
376
|
-
content = await readFile(fileURLToPath(schemaFile), { encoding: "utf-8" });
|
|
377
|
-
}
|
|
378
|
-
const schemaObject = parse(content);
|
|
379
|
-
return new Validator(schemaObject as JsonSchema);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
let schemaValidator: Validator | undefined = undefined;
|
|
383
|
-
|
|
384
|
-
async function loadSchema(path: string): Promise<TypeSchema> {
|
|
385
|
-
const content = await readFile(path, { encoding: "utf-8" });
|
|
386
|
-
const schema = parse(content);
|
|
387
|
-
if (schemaValidator == null) schemaValidator = await loadSchemaValidator();
|
|
388
|
-
const result = schemaValidator.validate(schema);
|
|
389
|
-
const errors: SchemaError[] = [];
|
|
390
|
-
if (result.valid) return schema as TypeSchema;
|
|
391
|
-
for (const e of result.errors) {
|
|
392
|
-
errors.push(
|
|
393
|
-
new SchemaError(path, `${path}:${e.instanceLocation}: ${e.error}`),
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
|
-
throw new AggregateError(errors, "Invalid schema data");
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Loads all schema files in the directory.
|
|
401
|
-
* @param dir The path of the directory to load schema files from.
|
|
402
|
-
* @returns A map from the qualified URI of a type to its {@link TypeSchema}.
|
|
403
|
-
* @throws {@link AggregateError} if any schema file is invalid. It contains
|
|
404
|
-
* all {@link SchemaError}s of the invalid schema files.
|
|
405
|
-
*/
|
|
406
|
-
export async function loadSchemaFiles(
|
|
407
|
-
dir: string,
|
|
408
|
-
): Promise<Record<string, TypeSchema>> {
|
|
409
|
-
if (typeof dir !== "string") {
|
|
410
|
-
throw new TypeError("Expected a directory path in string");
|
|
411
|
-
}
|
|
412
|
-
const result: Record<string, TypeSchema> = {};
|
|
413
|
-
const errors: SchemaError[] = [];
|
|
414
|
-
for await (const relPath of readDirRecursive(dir)) {
|
|
415
|
-
if (!relPath.match(/\.ya?ml$/i)) continue;
|
|
416
|
-
if (relPath.match(/(^|[/\\])schema.yaml$/i)) continue;
|
|
417
|
-
const path = join(dir, relPath);
|
|
418
|
-
let schema: TypeSchema;
|
|
419
|
-
try {
|
|
420
|
-
schema = await loadSchema(path);
|
|
421
|
-
} catch (e) {
|
|
422
|
-
if (
|
|
423
|
-
e instanceof AggregateError && e.errors.length > 0 &&
|
|
424
|
-
e.errors[0] instanceof SchemaError
|
|
425
|
-
) {
|
|
426
|
-
errors.push(...e.errors);
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
throw e;
|
|
430
|
-
}
|
|
431
|
-
result[schema.uri] = schema;
|
|
432
|
-
}
|
|
433
|
-
if (errors.length > 0) {
|
|
434
|
-
throw new AggregateError(errors, "Failed to load schema files");
|
|
435
|
-
}
|
|
436
|
-
const entries = Object.entries(result);
|
|
437
|
-
entries.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
438
|
-
return Object.fromEntries(entries);
|
|
439
|
-
}
|