From 5f5391172f3feee3bd4f06a8f44811c3206fd0cf Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Fri, 6 Sep 2024 17:37:54 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20add=20image=20validation=20for?= =?UTF-8?q?=20data=20insight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adminSiteClient/gdocsValidation.ts | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/adminSiteClient/gdocsValidation.ts b/adminSiteClient/gdocsValidation.ts index de14af70eb3..58ac938059c 100644 --- a/adminSiteClient/gdocsValidation.ts +++ b/adminSiteClient/gdocsValidation.ts @@ -13,6 +13,7 @@ import { OwidGdocDataInsightInterface, checkIsAuthor, OwidGdocAuthorInterface, + getFilenameExtension, } from "@ourworldindata/utils" function validateTitle(gdoc: OwidGdoc, errors: OwidGdocErrorMessage[]) { @@ -198,6 +199,40 @@ function validateGrapherUrl( } } +function validateDataInsightImage( + gdoc: OwidGdocDataInsightInterface, + errors: OwidGdocErrorMessage[] +) { + const image = gdoc.content.body.find((block) => block.type === "image") + if (!image) { + errors.push({ + property: "body", + type: OwidGdocErrorMessageType.Warning, + message: `Data insight is missing an image.`, + }) + } else { + for (const property of ["filename", "smallFilename"] as const) { + if (!image[property]) { + errors.push({ + property: "body", + type: OwidGdocErrorMessageType.Error, + message: `Data insight image is missing ${property}`, + }) + } + if ( + image[property] && + getFilenameExtension(image[property]) !== "png" + ) { + errors.push({ + property: "body", + type: OwidGdocErrorMessageType.Warning, + message: `Data insight ${property} should be a PNG`, + }) + } + } + } +} + function validateAtomFields( gdoc: OwidGdocPostInterface, errors: OwidGdocErrorMessage[] @@ -262,6 +297,7 @@ export const getErrors = (gdoc: OwidGdoc): OwidGdocErrorMessage[] => { } else if (checkIsDataInsight(gdoc)) { validateApprovedBy(gdoc, errors) validateGrapherUrl(gdoc, errors) + validateDataInsightImage(gdoc, errors) } else if (checkIsAuthor(gdoc)) { validateSocials(gdoc, errors) }