fix: resolve crash for when a list property contains invalid data

This commit is contained in:
DecafDev 2024-08-06 18:54:26 -06:00
parent ecdb33f6bb
commit 94f96e4cf4
2 changed files with 125 additions and 29 deletions

View file

@ -140,12 +140,15 @@ export const loadPropertyValue = <T>(
//If the property is not an array, return it as an array
//This is a bug in Obsidian?
if (!Array.isArray(propertyValue)) {
return [propertyValue] as unknown as T;
if (typeof propertyValue === "string") {
return [propertyValue] as unknown as T;
}
return null;
}
//Filter out null and undefined values
//Filter out values that are not strings
return propertyValue.filter(
(v) => v !== null && v !== undefined
(v) => typeof v === "string"
) as unknown as T;
}

View file

@ -1,4 +1,3 @@
import { FrontMatterCache } from "obsidian";
import { PropertyType } from "src/types";
import { loadPropertyValue } from "src/svelte/shared/services/load-property-value";
@ -9,7 +8,11 @@ describe("loadPropertyValue", () => {
const frontmatter: FrontMatterCache | undefined = undefined;
//Act
const result = loadPropertyValue<string>(frontmatter, "test", PropertyType.TEXT);
const result = loadPropertyValue<string>(
frontmatter,
"test",
PropertyType.TEXT
);
//Assert
expect(result).toBeNull();
@ -21,7 +24,11 @@ describe("loadPropertyValue", () => {
const propertyName = "";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.TEXT);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.TEXT
);
//Assert
expect(result).toBeNull();
@ -33,7 +40,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.TEXT);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.TEXT
);
//Assert
expect(result).toBeNull();
@ -45,7 +56,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.TEXT);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.TEXT
);
//Assert
expect(result).toBeNull();
@ -57,7 +72,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.TEXT);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.TEXT
);
//Assert
expect(result).toBeNull();
@ -69,7 +88,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.TEXT);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.TEXT
);
//Assert
expect(result).toBe("test");
@ -81,7 +104,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<number>(frontmatter, propertyName, PropertyType.NUMBER);
const result = loadPropertyValue<number>(
frontmatter,
propertyName,
PropertyType.NUMBER
);
//Assert
expect(result).toBeNull();
@ -93,7 +120,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<number>(frontmatter, propertyName, PropertyType.NUMBER);
const result = loadPropertyValue<number>(
frontmatter,
propertyName,
PropertyType.NUMBER
);
//Assert
expect(result).toBe(1);
@ -105,7 +136,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<boolean>(frontmatter, propertyName, PropertyType.CHECKBOX);
const result = loadPropertyValue<boolean>(
frontmatter,
propertyName,
PropertyType.CHECKBOX
);
//Assert
expect(result).toBeNull();
@ -117,19 +152,43 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<boolean>(frontmatter, propertyName, PropertyType.CHECKBOX);
const result = loadPropertyValue<boolean>(
frontmatter,
propertyName,
PropertyType.CHECKBOX
);
//Assert
expect(result).toBe(true);
});
it("returns null if the expectedType is PropertyType.LIST and value is not a string", () => {
//Arrange
const frontmatter: FrontMatterCache = { test: 123 };
const propertyName = "test";
//Act
const result = loadPropertyValue<string[]>(
frontmatter,
propertyName,
PropertyType.LIST
);
//Assert
expect(result).toEqual(null);
});
it("returns propertyValue as array if expectedType is PropertyType.LIST and value is an string", () => {
//Arrange
const frontmatter: FrontMatterCache = { test: "test" };
const propertyName = "test";
//Act
const result = loadPropertyValue<string[]>(frontmatter, propertyName, PropertyType.LIST);
const result = loadPropertyValue<string[]>(
frontmatter,
propertyName,
PropertyType.LIST
);
//Assert
expect(result).toEqual(["test"]);
@ -141,20 +200,27 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string[]>(frontmatter, propertyName, PropertyType.LIST);
const result = loadPropertyValue<string[]>(
frontmatter,
propertyName,
PropertyType.LIST
);
//Assert
expect(result).toEqual(["test"]);
});
it("returns propertyValue without null values if expectedType is PropertyType.LIST and value is an array", () => {
it("returns propertyValue without invalid values if expectedType is PropertyType.LIST and value is an array", () => {
//Arrange
const frontmatter: FrontMatterCache = { test: ["test", null] };
const frontmatter: FrontMatterCache = { test: ["test", null, 123] };
const propertyName = "test";
//Act
const result = loadPropertyValue<string[]>(frontmatter, propertyName, PropertyType.LIST);
const result = loadPropertyValue<string[]>(
frontmatter,
propertyName,
PropertyType.LIST
);
//Assert
expect(result).toEqual(["test"]);
@ -166,7 +232,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.DATE);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.DATE
);
//Assert
expect(result).toBeNull();
@ -174,24 +244,33 @@ describe("loadPropertyValue", () => {
it("returns null if expectedType is PropertyType.DATETIME and value is not a supported format", () => {
//Arrange
const frontmatter: FrontMatterCache = { test: '<% tp.file.creation_date() %>' };
const frontmatter: FrontMatterCache = {
test: "<% tp.file.creation_date() %>",
};
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.DATETIME);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.DATETIME
);
//Assert
expect(result).toBeNull();
});
it("returns propertyValue if expectedType is PropertyType.DATE and value is a string", () => {
//Arrange
const frontmatter: FrontMatterCache = { test: "2021-01-01" };
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.DATE);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.DATE
);
//Assert
expect(result).toBe("2021-01-01");
@ -203,7 +282,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.DATETIME);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.DATETIME
);
//Assert
expect(result).toBeNull();
@ -211,11 +294,17 @@ describe("loadPropertyValue", () => {
it("returns null if expectedType is PropertyType.DATETIME and value is not a supported format", () => {
//Arrange
const frontmatter: FrontMatterCache = { test: '<% tp.file.last_modified_date("dddd Do MMMM YYYY HH: mm: ss") %>' };
const frontmatter: FrontMatterCache = {
test: '<% tp.file.last_modified_date("dddd Do MMMM YYYY HH: mm: ss") %>',
};
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.DATETIME);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.DATETIME
);
//Assert
expect(result).toBeNull();
@ -227,7 +316,11 @@ describe("loadPropertyValue", () => {
const propertyName = "test";
//Act
const result = loadPropertyValue<string>(frontmatter, propertyName, PropertyType.DATETIME);
const result = loadPropertyValue<string>(
frontmatter,
propertyName,
PropertyType.DATETIME
);
//Assert
expect(result).toBe("2021-01-01T00:00:00");