From 84f80f9acd92a6c83c31d4a7a030a57758bb20c7 Mon Sep 17 00:00:00 2001 From: Idris Regg Date: Sun, 14 Dec 2025 18:19:57 +0100 Subject: [PATCH 1/2] Require non-empty items field for checkout Added validation to ensure items field is not empty. --- src/classes/client.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/classes/client.ts b/src/classes/client.ts index 8bab95b..62d7271 100644 --- a/src/classes/client.ts +++ b/src/classes/client.ts @@ -307,6 +307,11 @@ export class ChargilyClient { ); } + // createCheckout class accepts empty Array (items[]) from src/types/params which is by nature truthy - the code above's validation bypassed and invalid checkout may be created. + if (!checkout_data.items || checkout_data.items.length === 0){ + throw new Error('The items field is required.'); + } + return this.request('checkouts', 'POST', checkout_data); } From fc7565eab964cf93f181066b6e806e450dd0e465 Mon Sep 17 00:00:00 2001 From: Idris Regg Date: Mon, 15 Dec 2025 16:32:00 +0100 Subject: [PATCH 2/2] Update client.ts Updated validation logic to require either a non-empty items array or both amount and currency, preventing invalid checkouts. --- src/classes/client.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/classes/client.ts b/src/classes/client.ts index 62d7271..a9d5f2b 100644 --- a/src/classes/client.ts +++ b/src/classes/client.ts @@ -307,11 +307,20 @@ export class ChargilyClient { ); } - // createCheckout class accepts empty Array (items[]) from src/types/params which is by nature truthy - the code above's validation bypassed and invalid checkout may be created. - if (!checkout_data.items || checkout_data.items.length === 0){ - throw new Error('The items field is required.'); + // require either a non-empty items array or both amount and currency, This prevents empty items arrays from bypassing validation. + const hasItems = + Array.isArray(checkout_data.items) && + checkout_data.items.length > 0; + + const hasAmountAndCurrency = + typeof checkout_data.amount === 'number' && + typeof checkout_data.currency === 'string'; + + if (!hasItems && !hasAmountAndCurrency) { + throw new Error( + 'Either a non-empty items array or both amount and currency must be provided.' + ); } - return this.request('checkouts', 'POST', checkout_data); }