Skip to content

Commit

Permalink
Merge pull request #4112 from Shopify/punkstar/supported-buyer-contex…
Browse files Browse the repository at this point in the history
…t-single-format

supported buyer context: disallow mixed definition types
  • Loading branch information
punkstar authored Jun 26, 2024
2 parents 0cd5b3f + 4081be9 commit 3bfabdc
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,54 @@ describe('SupportedBuyerContextSchema', async () => {
)
})

test('throws an error if an unexpected key is present', async () => {
expect(() =>
SupportedBuyerContextsSchema.parse({
supported_buyer_contexts: [{currency: 'USD', random: 123}],
}),
).toThrowError(
new zod.ZodError([
{
code: zod.ZodIssueCode.unrecognized_keys,
keys: ['random'],
path: ['supported_buyer_contexts', 0],
message: "Unrecognized key(s) in object: 'random'",
},
]),
)
})

test('throws an error if a mixture of currency and currency plus countries provided', async () => {
expect(() =>
SupportedBuyerContextsSchema.parse({
supported_buyer_contexts: [{currency: 'USD'}, {currency: 'EUR', countries: ['DE']}],
}),
).toThrowError(
new zod.ZodError([
{
code: zod.ZodIssueCode.custom,
message:
'Must all be defined with only a currency, or must all be defined with a currency plus countries -- a mixture of the two is not allowed',
path: ['supported_buyer_contexts'],
},
]),
)
})

test('is valid if countries are not provided', async () => {
const {success} = SupportedBuyerContextsSchema.safeParse({
supported_buyer_contexts: [{currency: 'USD'}],
supported_buyer_contexts: [{currency: 'USD'}, {currency: 'CAD'}],
})

expect(success).toBe(true)
})

test('is valid if currrency and countries are provided', async () => {
const {success} = SupportedBuyerContextsSchema.safeParse({
supported_buyer_contexts: [{currency: 'USD', countries: ['US']}],
supported_buyer_contexts: [
{currency: 'USD', countries: ['US']},
{currency: 'EUR', countries: ['FR', 'DE']},
],
})

expect(success).toBe(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,23 @@ export const ConfirmationSchema = zod.object({
export const SupportedBuyerContextsSchema = zod.object({
supported_buyer_contexts: zod
.array(
zod.object({
currency: zod.string(),
countries: zod.array(zod.string()).nonempty().optional(),
}),
zod
.object({
currency: zod.string(),
countries: zod.array(zod.string()).nonempty().optional(),
})
.strict(),
)
.optional(),
.optional()
.refine(
(values) => {
return (
values === undefined || values.every((value) => value.countries) || values.every((value) => !value.countries)
)
},
{
message:
'Must all be defined with only a currency, or must all be defined with a currency plus countries -- a mixture of the two is not allowed',
},
),
})
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ const config: CreditCardPaymentsAppExtensionConfigType = {
merchant_label: 'some-label',
supported_countries: ['CA'],
supported_payment_methods: ['PAYMENT_METHOD'],
supported_buyer_contexts: [
{currency: 'USD'},
{currency: 'CAD', countries: ['CA']},
{currency: 'EUR', countries: ['DE', 'FR']},
],
supported_buyer_contexts: [{currency: 'USD'}, {currency: 'CAD'}],
supports_3ds: false,
test_mode_available: true,
supports_deferred_payments: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const config: CustomCreditCardPaymentsAppExtensionConfigType = {
supported_countries: ['CA'],
supported_payment_methods: ['visa'],
supported_buyer_contexts: [
{currency: 'USD'},
{currency: 'CAD', countries: ['CA']},
{currency: 'EUR', countries: ['DE', 'FR']},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ const config: CustomOnsitePaymentsAppExtensionConfigType = {
merchant_label: 'some-label',
supported_countries: ['CA'],
supported_payment_methods: ['visa'],
supported_buyer_contexts: [
{currency: 'USD'},
{currency: 'CAD', countries: ['CA']},
{currency: 'EUR', countries: ['DE', 'FR']},
],
supported_buyer_contexts: [{currency: 'EUR', countries: ['DE', 'FR']}],
supports_oversell_protection: true,
supports_3ds: true,
supports_installments: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ const config: OffsitePaymentsAppExtensionConfigType = {
merchant_label: 'some-label',
supported_countries: ['CA'],
supported_payment_methods: ['PAYMENT_METHOD'],
supported_buyer_contexts: [
{currency: 'USD'},
{currency: 'CAD', countries: ['CA']},
{currency: 'EUR', countries: ['DE', 'FR']},
],
supported_buyer_contexts: [{currency: 'USD'}],
supports_3ds: false,
supports_oversell_protection: false,
test_mode_available: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ const config: RedeemablePaymentsAppExtensionConfigType = {
merchant_label: 'some-label',
supported_countries: ['CA'],
supported_payment_methods: ['gift-card'],
supported_buyer_contexts: [
{currency: 'USD'},
{currency: 'CAD', countries: ['CA']},
{currency: 'EUR', countries: ['DE', 'FR']},
],
supported_buyer_contexts: [{currency: 'CAD'}],
test_mode_available: true,
ui_extension_handle: 'sample-ui-extension',
targeting: [{target: 'payments.redeemable.render'}],
Expand Down

0 comments on commit 3bfabdc

Please sign in to comment.