Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,72 @@ describe("inferEntityFields", () => {
);
});

it("does not classify an object containing an image field as type.image", () => {
const inferred = inferEntityFields(
{
c_aboutSection: {
title: "About",
description: {
html: "<p>Description</p>",
},
image: {
url: "https://example.com/about.jpg",
width: 1200,
height: 800,
},
cta: {
label: "Learn more",
link: "/about",
linkType: "URL",
},
},
},
{
fields: [
"c_aboutSection.title",
"c_aboutSection.description.html",
"c_aboutSection.image",
"c_aboutSection.cta",
],
}
);

const aboutSection = inferred.fields.find(
(field) => field.name === "c_aboutSection"
);

expect(aboutSection?.definition.typeRegistryId).toBe("type.object");
expect(aboutSection?.children?.fields).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "description",
displayName: "About Section > Description",
definition: expect.objectContaining({
typeRegistryId: "type.rich_text_v2",
}),
}),
expect.objectContaining({
name: "image",
definition: expect.objectContaining({
typeRegistryId: "type.image",
}),
}),
expect.objectContaining({
name: "cta",
definition: expect.objectContaining({
typeRegistryId: "type.cta",
}),
}),
expect.objectContaining({
name: "title",
definition: expect.objectContaining({
typeRegistryId: "type.string",
}),
}),
])
);
});

it("does not classify unrelated json-shaped objects as rich text", () => {
const inferred = inferEntityFields({
analyticsPayload: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,10 @@ const inferStructuredObjectType = (
};

const isImageValue = (value: Record<string, unknown>): boolean => {
const nestedImage = value.image;
if (isPlainObject(nestedImage) && isImageValue(nestedImage)) {
return true;
}
return isDirectImageValue(value) || isComplexImageValue(value);
};

const isDirectImageValue = (value: Record<string, unknown>): boolean => {
return (
typeof value.url === "string" &&
(typeof value.alternateText === "string" ||
Expand All @@ -230,6 +229,16 @@ const isImageValue = (value: Record<string, unknown>): boolean => {
);
};

const isComplexImageValue = (value: Record<string, unknown>): boolean => {
const valueKeys = Object.keys(value);
if (valueKeys.length !== 1 || valueKeys[0] !== "image") {
return false;
}

const nestedImage = value.image;
return isPlainObject(nestedImage) && isDirectImageValue(nestedImage);
};

const isCtaValue = (value: Record<string, unknown>): boolean => {
return (
(typeof value.label === "string" || typeof value.link === "string") &&
Expand Down
Loading