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