Skip to content

Commit

Permalink
validateCustomCard should not modify its input.
Browse files Browse the repository at this point in the history
Fixes an issue when inheriting an 'image' property (duplicate 'image' and 'image_uris' properties in children).
  • Loading branch information
Senryoku committed Mar 5, 2025
1 parent a664411 commit 733515f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/CustomCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,24 @@ export function validateCustomCard(inputCard: any): SocketError | Card {
if ("image_uris" in inputCard && "image" in inputCard)
return valErr("Invalid Property", "Only one of 'image_uris' or 'image' can be used.");

// Defaults to an invalid image. The client will display a placeholder.
// This might seem odd, but the client will always have to handle the case where the image is invalid or unreachable anyway.
let image_uris = { en: "" };

if ("image_uris" in inputCard) {
if (Object.keys(inputCard["image_uris"]).length === 0)
return valErr(
`Invalid Property`,
`Invalid property 'image_uris' in custom card: Should have at least one entry.`
);
if (!Object.keys(inputCard["image_uris"]).includes("en")) {
if (!Object.keys(inputCard["image_uris"]).includes("en"))
return valErr(`Invalid Property`, `Invalid property 'image_uris' in custom card: Missing 'en' property.`);
}
image_uris = inputCard["image_uris"];
} else if ("image" in inputCard) {
// Shortcut for specifying a single image
if (!isString(inputCard.image))
return valErr("Invalid Property", "Invalid property 'image' in custom card, must be a string.");
inputCard.image_uris = { en: inputCard.image };
} else {
inputCard.image_uris = { en: "" }; // Defaults to an invalid image. The client will display a placeholder.
// This might seem odd, but the client will always have to handle the case where the image is invalid or unreachable anyway.
image_uris = { en: inputCard.image };
}

if ("colors" in inputCard) {
Expand Down Expand Up @@ -193,7 +194,7 @@ export function validateCustomCard(inputCard: any): SocketError | Card {
card.in_booster = inputCard.in_booster ?? true;
card.layout = inputCard.layout;
card.printed_names = inputCard.printed_names ?? { en: inputCard.name };
card.image_uris = inputCard.image_uris;
card.image_uris = image_uris;
card.foil = inputCard.foil;
card.oracle_text = inputCard.oracle_text;
card.power = inputCard.power;
Expand Down
5 changes: 5 additions & 0 deletions test/data/CustomCards_MultiplePrintings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
"en": "https://cards.scryfall.io/large/front/b/9/b9b6ec63-7df4-450e-b849-920121fe245c.jpg?1695422185"
},
},
{
"name": "Image Prop.",
"collector_number": "729-DefaultImage",
},
]
[DefaultSlot]
Nazgûl
Expand All @@ -80,4 +84,5 @@ Nazgûl (LTR) 334
Image Prop.
Image Prop. (LTR) 729
Image Prop. (LTR) 730
Image Prop. (LTR) 729-DefaultImage
Card related to Nazgûl somehow

0 comments on commit 733515f

Please sign in to comment.