github.com/prebid/prebid-server/v2@v2.18.0/bidadjustment/validate.go (about)

     1  package bidadjustment
     2  
     3  import (
     4  	"math"
     5  
     6  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     7  )
     8  
     9  // Validate checks whether all provided bid adjustments are valid or not against the requirements defined in the issue
    10  func Validate(bidAdjustments *openrtb_ext.ExtRequestPrebidBidAdjustments) bool {
    11  	if bidAdjustments == nil {
    12  		return true
    13  	}
    14  	if !validateForMediaType(bidAdjustments.MediaType.Banner) {
    15  		return false
    16  	}
    17  	if !validateForMediaType(bidAdjustments.MediaType.Audio) {
    18  		return false
    19  	}
    20  	if !validateForMediaType(bidAdjustments.MediaType.VideoInstream) {
    21  		return false
    22  	}
    23  	if !validateForMediaType(bidAdjustments.MediaType.VideoOutstream) {
    24  		return false
    25  	}
    26  	if !validateForMediaType(bidAdjustments.MediaType.Native) {
    27  		return false
    28  	}
    29  	if !validateForMediaType(bidAdjustments.MediaType.WildCard) {
    30  		return false
    31  	}
    32  	return true
    33  }
    34  
    35  func validateForMediaType(bidAdj map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID) bool {
    36  	for bidderName := range bidAdj {
    37  		if bidAdj[bidderName] == nil {
    38  			return false
    39  		}
    40  		for dealID := range bidAdj[bidderName] {
    41  			if bidAdj[bidderName][dealID] == nil {
    42  				return false
    43  			}
    44  			for _, adjustment := range bidAdj[bidderName][dealID] {
    45  				if !validateAdjustment(adjustment) {
    46  					return false
    47  				}
    48  			}
    49  		}
    50  	}
    51  	return true
    52  }
    53  
    54  func validateAdjustment(adjustment openrtb_ext.Adjustment) bool {
    55  	switch adjustment.Type {
    56  	case AdjustmentTypeCPM:
    57  		if adjustment.Currency != "" && adjustment.Value >= 0 && adjustment.Value < math.MaxFloat64 {
    58  			return true
    59  		}
    60  	case AdjustmentTypeMultiplier:
    61  		if adjustment.Value >= 0 && adjustment.Value < 100 {
    62  			return true
    63  		}
    64  	case AdjustmentTypeStatic:
    65  		if adjustment.Currency != "" && adjustment.Value >= 0 && adjustment.Value < math.MaxFloat64 {
    66  			return true
    67  		}
    68  	}
    69  	return false
    70  }