@fedify/vocab-tools 2.4.0-pr.936.41 → 2.4.0

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/type.ts CHANGED
@@ -19,6 +19,12 @@ interface ScalarType {
19
19
  compactEncoder?: (variable: string) => string;
20
20
  dataCheck(variable: string): string;
21
21
  decoder(variable: string, baseUrlVar: string): string;
22
+ /**
23
+ * Whether a value that fails `dataCheck()` should be skipped instead of
24
+ * being handed to `decoder()`. Set this for types decoded from untrusted
25
+ * remote input, where one malformed value must not fail the whole object.
26
+ */
27
+ skipUnparsable?: boolean;
22
28
  }
23
29
 
24
30
  const scalarTypes: Record<string, ScalarType> = {
@@ -187,7 +193,8 @@ const scalarTypes: Record<string, ScalarType> = {
187
193
  dataCheck(v) {
188
194
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
189
195
  && typeof ${v}["@language"] === "string"
190
- && typeof ${v}["@value"] === "string"`;
196
+ && typeof ${v}["@value"] === "string"
197
+ && isValidLanguageTag(${v}["@language"])`;
191
198
  },
192
199
  decoder(v) {
193
200
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -318,13 +325,14 @@ const scalarTypes: Record<string, ScalarType> = {
318
325
  return `formatIri(${v})`;
319
326
  },
320
327
  dataCheck(v) {
321
- return `typeof ${v} === "object" && "@value" in ${v}
322
- && typeof ${v}["@value"] === "string"
323
- && ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
328
+ return `typeof ${v} === "object" &&
329
+ ((typeof ${v}["@id"] === "string" && canDecodeIri(${v}["@id"])) ||
330
+ (typeof ${v}["@value"] === "string" && canDecodeIri(${v}["@value"])))`;
324
331
  },
325
332
  decoder(v) {
326
- return `parseIri(${v}["@value"])`;
333
+ return `decodeIri(typeof ${v}["@id"] === "string" ? ${v}["@id"] : ${v}["@value"])`;
327
334
  },
335
+ skipUnparsable: true,
328
336
  },
329
337
  "fedify:gatewayUrl": {
330
338
  name: "URL",
@@ -332,10 +340,10 @@ const scalarTypes: Record<string, ScalarType> = {
332
340
  return `${v} instanceof URL && isGatewayUrl(${v})`;
333
341
  },
334
342
  encoder(v) {
335
- return `{ "@id": formatIri(${v}) }`;
343
+ return `{ "@id": ${v}.origin }`;
336
344
  },
337
345
  compactEncoder(v) {
338
- return `formatIri(${v})`;
346
+ return `${v}.origin`;
339
347
  },
340
348
  dataCheck(v) {
341
349
  return `${v} != null && typeof ${v} === "object" &&
@@ -657,6 +665,10 @@ export function getDecoder(
657
665
  throw new Error(`Unknown type: ${typeUri}`);
658
666
  }
659
667
 
668
+ export function skipsUnparsable(typeUri: string): boolean {
669
+ return scalarTypes[typeUri]?.skipUnparsable ?? false;
670
+ }
671
+
660
672
  export function getDataCheck(
661
673
  typeUri: string,
662
674
  types: Record<string, TypeSchema>,