diff --git a/commercelayer/resource_payment_method.go b/commercelayer/resource_payment_method.go index e1183b5..57c259d 100644 --- a/commercelayer/resource_payment_method.go +++ b/commercelayer/resource_payment_method.go @@ -39,11 +39,12 @@ func resourcePaymentMethod() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "payment_source_type": { - Description: "The payment source type, can be one of: 'AdyenPayment', 'BraintreePayment', " + - "'CheckoutComPayment', 'CreditCard', 'ExternalPayment', 'KlarnaPayment', 'PaypalPayment', " + - "'StripePayment', or 'WireTransfer'.", - Type: schema.TypeString, - Required: true, + Description: "The payment source type, can be one of: AdyenPayment, BraintreePayment, " + + "CheckoutComPayment, CreditCard, ExternalPayment, KlarnaPayment, PaypalPayment, " + + "StripePayment or WireTransfer", + Type: schema.TypeString, + Required: true, + ValidateDiagFunc: paymentSourceValidation, }, "currency_code": { Description: "The international 3-letter currency code as defined by the ISO 4217 standard. " + diff --git a/commercelayer/validation.go b/commercelayer/validation.go index 066e5be..e03a5d0 100644 --- a/commercelayer/validation.go +++ b/commercelayer/validation.go @@ -30,3 +30,27 @@ var inventoryModelStrategyValidation = func(i interface{}, path cty.Path) diag.D return diag.Errorf("Invalid inventory model strategy provided: %s. Must be one of %s", i.(string), strings.Join(getInventoryModelStrategies(), ", ")) } + +func getPaymentSources() []string { + return []string{ + "AdyenPayment", + "BraintreePayment", + "CheckoutComPayment", + "CreditCard", + "ExternalPayment", + "KlarnaPayment", + "PaypalPayment", + "StripePayment", + "WireTransfer", + } +} + +var paymentSourceValidation = func(i interface{}, path cty.Path) diag.Diagnostics { + for _, s := range getPaymentSources() { + if s == i.(string) { + return nil + } + } + return diag.Errorf("Invalid payment source provided: %s. Must be one of %s", + i.(string), strings.Join(getPaymentSources(), ", ")) +} diff --git a/commercelayer/validation_test.go b/commercelayer/validation_test.go index 0e2de41..263155d 100644 --- a/commercelayer/validation_test.go +++ b/commercelayer/validation_test.go @@ -14,3 +14,13 @@ func TestCurrencyCodeValidationOK(t *testing.T) { diag := currencyCodeValidation("EUR", nil) assert.False(t, diag.HasError()) } + +func TestPaymentSourceValidationError(t *testing.T) { + diag := paymentSourceValidation("Adyen", nil) + assert.True(t, diag.HasError()) +} + +func TestPaymentSourceValidationOK(t *testing.T) { + diag := paymentSourceValidation("BraintreePayment", nil) + assert.False(t, diag.HasError()) +}