github.com/prebid/prebid-server/v2@v2.18.0/currency/validation.go (about)

     1  package currency
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/text/currency"
     7  
     8  	"github.com/prebid/prebid-server/v2/errortypes"
     9  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    10  )
    11  
    12  // ValidateCustomRates throws a bad input error if any of the 3-digit currency codes found in
    13  // the bidRequest.ext.prebid.currency field is invalid, malfomed or does not represent any actual
    14  // currency. No error is thrown if bidRequest.ext.prebid.currency is invalid or empty.
    15  func ValidateCustomRates(bidReqCurrencyRates *openrtb_ext.ExtRequestCurrency) error {
    16  	if bidReqCurrencyRates == nil {
    17  		return nil
    18  	}
    19  
    20  	for fromCurrency, rates := range bidReqCurrencyRates.ConversionRates {
    21  		// Check if fromCurrency is a valid 3-letter currency code
    22  		if _, err := currency.ParseISO(fromCurrency); err != nil {
    23  			return &errortypes.BadInput{Message: fmt.Sprintf("currency code %s is not recognized or malformed", fromCurrency)}
    24  		}
    25  
    26  		// Check if currencies mapped to fromCurrency are valid 3-letter currency codes
    27  		for toCurrency := range rates {
    28  			if _, err := currency.ParseISO(toCurrency); err != nil {
    29  				return &errortypes.BadInput{Message: fmt.Sprintf("currency code %s is not recognized or malformed", toCurrency)}
    30  			}
    31  		}
    32  	}
    33  	return nil
    34  }