-
Notifications
You must be signed in to change notification settings - Fork 741
/
bidders.go
665 lines (617 loc) · 21.2 KB
/
bidders.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package openrtb_ext
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/xeipuuv/gojsonschema"
)
// BidderName refers to a core bidder id or an alias id.
type BidderName string
var aliasBidderToParent map[BidderName]BidderName = map[BidderName]BidderName{}
var coreBidderNames []BidderName = []BidderName{
Bidder33Across,
BidderAax,
BidderAceex,
BidderAcuityAds,
BidderAdf,
BidderAdgeneration,
BidderAdhese,
BidderAdkernel,
BidderAdkernelAdn,
BidderAdman,
BidderAdmixer,
BidderAdnuntius,
BidderAdOcean,
BidderAdoppler,
BidderAdot,
BidderAdpone,
BidderAdprime,
BidderAdquery,
BidderAdrino,
BidderAdsinteractive,
BidderAdtarget,
BidderAdtrgtme,
BidderAdtelligent,
BidderAdvangelists,
BidderAdView,
BidderAdxcg,
BidderAdyoulike,
BidderAidem,
BidderAJA,
BidderAlgorix,
BidderAlkimi,
BidderAMX,
BidderApacdex,
BidderAppnexus,
BidderAppush,
BidderAudienceNetwork,
BidderAutomatad,
BidderAvocet,
BidderAxis,
BidderAxonix,
BidderBeachfront,
BidderBeintoo,
BidderBematterfull,
BidderBetween,
BidderBeyondMedia,
BidderBidmachine,
BidderBidmyadz,
BidderBidsCube,
BidderBidstack,
BidderBizzclick,
BidderBliink,
BidderBlue,
BidderBluesea,
BidderBmtm,
BidderBoldwin,
BidderBrave,
BidderCadentApertureMX,
BidderCcx,
BidderCoinzilla,
BidderColossus,
BidderCompass,
BidderConnectAd,
BidderConsumable,
BidderConversant,
BidderCpmstar,
BidderCriteo,
BidderCWire,
BidderDatablocks,
BidderDecenterAds,
BidderDeepintent,
BidderDefinemedia,
BidderDianomi,
BidderEdge226,
BidderDmx,
BidderDXKulture,
BidderEmtv,
BidderEmxDigital,
BidderEPlanning,
BidderEpom,
BidderEVolution,
BidderFlipp,
BidderFreewheelSSP,
BidderFRVRAdNetwork,
BidderGamma,
BidderGamoshi,
BidderGlobalsun,
BidderGothamads,
BidderGrid,
BidderGumGum,
BidderHuaweiAds,
BidderImds,
BidderImpactify,
BidderImprovedigital,
BidderInfyTV,
BidderInMobi,
BidderInteractiveoffers,
BidderInvibes,
BidderIQX,
BidderIQZone,
BidderIx,
BidderJixie,
BidderKargo,
BidderKayzen,
BidderKidoz,
BidderKiviads,
BidderLmKiviads,
BidderKrushmedia,
BidderLemmadigital,
BidderLiftoff,
BidderLimelightDigital,
BidderLockerDome,
BidderLogan,
BidderLogicad,
BidderLunaMedia,
BidderMabidder,
BidderMadvertise,
BidderMarsmedia,
BidderMediafuse,
BidderMedianet,
BidderMgid,
BidderMgidX,
BidderMobfoxpb,
BidderMobileFuse,
BidderMotorik,
BidderNextMillennium,
BidderNoBid,
BidderOms,
BidderOneTag,
BidderOpenWeb,
BidderOpenx,
BidderOperaads,
BidderOrbidder,
BidderOutbrain,
BidderOwnAdx,
BidderPangle,
BidderPGAMSsp,
BidderPubmatic,
BidderPubnative,
BidderPulsepoint,
BidderPWBid,
BidderRelevantDigital,
BidderRevcontent,
BidderRichaudience,
BidderRise,
BidderRTBHouse,
BidderRubicon,
BidderSeedingAlliance,
BidderSaLunaMedia,
BidderScreencore,
BidderSharethrough,
BidderSilverMob,
BidderSilverPush,
BidderSmaato,
BidderSmartAdserver,
BidderSmartHub,
BidderSmartRTB,
BidderSmartx,
BidderSmartyAds,
BidderSmileWanted,
BidderSonobi,
BidderSovrn,
BidderSovrnXsp,
BidderSspBC,
BidderStroeerCore,
BidderTaboola,
BidderTappx,
BidderTeads,
BidderTelaria,
BidderTpmn,
BidderTrafficGate,
BidderTriplelift,
BidderTripleliftNative,
BidderUcfunnel,
BidderUndertone,
BidderUnicorn,
BidderUnruly,
BidderVideoByte,
BidderVideoHeroes,
BidderVidoomy,
BidderVisibleMeasures,
BidderVisx,
BidderVox,
BidderVrtcal,
BidderXeworks,
BidderYahooAds,
BidderYeahmobi,
BidderYieldlab,
BidderYieldmo,
BidderYieldone,
BidderZeroClickFraud,
BidderZetaGlobalSsp,
}
func GetAliasBidderToParent() map[BidderName]BidderName {
return aliasBidderToParent
}
func SetAliasBidderName(aliasBidderName string, parentBidderName BidderName) error {
if IsBidderNameReserved(aliasBidderName) {
return fmt.Errorf("alias %s is a reserved bidder name and cannot be used", aliasBidderName)
}
aliasBidder := BidderName(aliasBidderName)
coreBidderNames = append(coreBidderNames, aliasBidder)
aliasBidderToParent[aliasBidder] = parentBidderName
bidderNameLookup[strings.ToLower(aliasBidderName)] = aliasBidder
return nil
}
func (name BidderName) MarshalJSON() ([]byte, error) {
return []byte(name), nil
}
func (name *BidderName) String() string {
if name == nil {
return ""
}
return string(*name)
}
// Names of reserved bidders. These names may not be used by a core bidder or alias.
const (
BidderReservedAll BidderName = "all" // Reserved for the /info/bidders/all endpoint.
BidderReservedContext BidderName = "context" // Reserved for first party data.
BidderReservedData BidderName = "data" // Reserved for first party data.
BidderReservedGeneral BidderName = "general" // Reserved for non-bidder specific messages when using a map keyed on the bidder name.
BidderReservedGPID BidderName = "gpid" // Reserved for Global Placement ID (GPID).
BidderReservedPrebid BidderName = "prebid" // Reserved for Prebid Server configuration.
BidderReservedSKAdN BidderName = "skadn" // Reserved for Apple's SKAdNetwork OpenRTB extension.
BidderReservedTID BidderName = "tid" // Reserved for Per-Impression Transactions IDs for Multi-Impression Bid Requests.
BidderReservedAE BidderName = "ae" // Reserved for FLEDGE Auction Environment
)
// IsBidderNameReserved returns true if the specified name is a case insensitive match for a reserved bidder name.
func IsBidderNameReserved(name string) bool {
if strings.EqualFold(name, string(BidderReservedAll)) {
return true
}
if strings.EqualFold(name, string(BidderReservedContext)) {
return true
}
if strings.EqualFold(name, string(BidderReservedData)) {
return true
}
if strings.EqualFold(name, string(BidderReservedGeneral)) {
return true
}
if strings.EqualFold(name, string(BidderReservedGPID)) {
return true
}
if strings.EqualFold(name, string(BidderReservedSKAdN)) {
return true
}
if strings.EqualFold(name, string(BidderReservedPrebid)) {
return true
}
if strings.EqualFold(name, string(BidderReservedTID)) {
return true
}
if strings.EqualFold(name, string(BidderReservedAE)) {
return true
}
return false
}
// Names of core bidders. These names *must* match the bidder code in Prebid.js if an adapter also exists in that
// project. You may *not* use the name 'general' as that is reserved for general error messages nor 'context' as
// that is reserved for first party data.
//
// Please keep this list alphabetized to minimize merge conflicts.
const (
Bidder33Across BidderName = "33across"
BidderAax BidderName = "aax"
BidderAceex BidderName = "aceex"
BidderAcuityAds BidderName = "acuityads"
BidderAdf BidderName = "adf"
BidderAdgeneration BidderName = "adgeneration"
BidderAdhese BidderName = "adhese"
BidderAdkernel BidderName = "adkernel"
BidderAdkernelAdn BidderName = "adkernelAdn"
BidderAdman BidderName = "adman"
BidderAdmixer BidderName = "admixer"
BidderAdnuntius BidderName = "adnuntius"
BidderAdOcean BidderName = "adocean"
BidderAdoppler BidderName = "adoppler"
BidderAdot BidderName = "adot"
BidderAdpone BidderName = "adpone"
BidderAdprime BidderName = "adprime"
BidderAdquery BidderName = "adquery"
BidderAdrino BidderName = "adrino"
BidderAdsinteractive BidderName = "adsinteractive"
BidderAdtarget BidderName = "adtarget"
BidderAdtrgtme BidderName = "adtrgtme"
BidderAdtelligent BidderName = "adtelligent"
BidderAdvangelists BidderName = "advangelists"
BidderAdView BidderName = "adview"
BidderAdxcg BidderName = "adxcg"
BidderAdyoulike BidderName = "adyoulike"
BidderAidem BidderName = "aidem"
BidderAJA BidderName = "aja"
BidderAlgorix BidderName = "algorix"
BidderAlkimi BidderName = "alkimi"
BidderAMX BidderName = "amx"
BidderApacdex BidderName = "apacdex"
BidderAppnexus BidderName = "appnexus"
BidderAppush BidderName = "appush"
BidderAudienceNetwork BidderName = "audienceNetwork"
BidderAutomatad BidderName = "automatad"
BidderAvocet BidderName = "avocet"
BidderAxis BidderName = "axis"
BidderAxonix BidderName = "axonix"
BidderBeachfront BidderName = "beachfront"
BidderBeintoo BidderName = "beintoo"
BidderBematterfull BidderName = "bematterfull"
BidderBetween BidderName = "between"
BidderBeyondMedia BidderName = "beyondmedia"
BidderBidmachine BidderName = "bidmachine"
BidderBidmyadz BidderName = "bidmyadz"
BidderBidsCube BidderName = "bidscube"
BidderBidstack BidderName = "bidstack"
BidderBizzclick BidderName = "bizzclick"
BidderBliink BidderName = "bliink"
BidderBlue BidderName = "blue"
BidderBluesea BidderName = "bluesea"
BidderBmtm BidderName = "bmtm"
BidderBoldwin BidderName = "boldwin"
BidderBrave BidderName = "brave"
BidderCadentApertureMX BidderName = "cadent_aperture_mx"
BidderCcx BidderName = "ccx"
BidderCoinzilla BidderName = "coinzilla"
BidderColossus BidderName = "colossus"
BidderCompass BidderName = "compass"
BidderConnectAd BidderName = "connectad"
BidderConsumable BidderName = "consumable"
BidderConversant BidderName = "conversant"
BidderCpmstar BidderName = "cpmstar"
BidderCriteo BidderName = "criteo"
BidderCWire BidderName = "cwire"
BidderDatablocks BidderName = "datablocks"
BidderDecenterAds BidderName = "decenterads"
BidderDeepintent BidderName = "deepintent"
BidderDefinemedia BidderName = "definemedia"
BidderDianomi BidderName = "dianomi"
BidderEdge226 BidderName = "edge226"
BidderDmx BidderName = "dmx"
BidderDXKulture BidderName = "dxkulture"
BidderEmtv BidderName = "emtv"
BidderEmxDigital BidderName = "emx_digital"
BidderEPlanning BidderName = "eplanning"
BidderEpom BidderName = "epom"
BidderEVolution BidderName = "e_volution"
BidderFlipp BidderName = "flipp"
BidderFreewheelSSP BidderName = "freewheelssp"
BidderFRVRAdNetwork BidderName = "frvradn"
BidderGamma BidderName = "gamma"
BidderGamoshi BidderName = "gamoshi"
BidderGlobalsun BidderName = "globalsun"
BidderGothamads BidderName = "gothamads"
BidderGrid BidderName = "grid"
BidderGumGum BidderName = "gumgum"
BidderHuaweiAds BidderName = "huaweiads"
BidderImds BidderName = "imds"
BidderImpactify BidderName = "impactify"
BidderImprovedigital BidderName = "improvedigital"
BidderInfyTV BidderName = "infytv"
BidderInMobi BidderName = "inmobi"
BidderInteractiveoffers BidderName = "interactiveoffers"
BidderInvibes BidderName = "invibes"
BidderIQX BidderName = "iqx"
BidderIQZone BidderName = "iqzone"
BidderIx BidderName = "ix"
BidderJixie BidderName = "jixie"
BidderKargo BidderName = "kargo"
BidderKayzen BidderName = "kayzen"
BidderKidoz BidderName = "kidoz"
BidderKiviads BidderName = "kiviads"
BidderLmKiviads BidderName = "lm_kiviads"
BidderKrushmedia BidderName = "krushmedia"
BidderLemmadigital BidderName = "lemmadigital"
BidderLiftoff BidderName = "liftoff"
BidderLimelightDigital BidderName = "limelightDigital"
BidderLockerDome BidderName = "lockerdome"
BidderLogan BidderName = "logan"
BidderLogicad BidderName = "logicad"
BidderLunaMedia BidderName = "lunamedia"
BidderMabidder BidderName = "mabidder"
BidderMadvertise BidderName = "madvertise"
BidderMarsmedia BidderName = "marsmedia"
BidderMediafuse BidderName = "mediafuse"
BidderMedianet BidderName = "medianet"
BidderMgid BidderName = "mgid"
BidderMgidX BidderName = "mgidX"
BidderMobfoxpb BidderName = "mobfoxpb"
BidderMobileFuse BidderName = "mobilefuse"
BidderMotorik BidderName = "motorik"
BidderNextMillennium BidderName = "nextmillennium"
BidderNoBid BidderName = "nobid"
BidderOms BidderName = "oms"
BidderOneTag BidderName = "onetag"
BidderOpenWeb BidderName = "openweb"
BidderOpenx BidderName = "openx"
BidderOperaads BidderName = "operaads"
BidderOrbidder BidderName = "orbidder"
BidderOutbrain BidderName = "outbrain"
BidderOwnAdx BidderName = "ownadx"
BidderPangle BidderName = "pangle"
BidderPGAMSsp BidderName = "pgamssp"
BidderPubmatic BidderName = "pubmatic"
BidderPubnative BidderName = "pubnative"
BidderPulsepoint BidderName = "pulsepoint"
BidderPWBid BidderName = "pwbid"
BidderRelevantDigital BidderName = "relevantdigital"
BidderRevcontent BidderName = "revcontent"
BidderRichaudience BidderName = "richaudience"
BidderRise BidderName = "rise"
BidderRTBHouse BidderName = "rtbhouse"
BidderRubicon BidderName = "rubicon"
BidderSeedingAlliance BidderName = "seedingAlliance"
BidderSaLunaMedia BidderName = "sa_lunamedia"
BidderScreencore BidderName = "screencore"
BidderSharethrough BidderName = "sharethrough"
BidderSilverMob BidderName = "silvermob"
BidderSilverPush BidderName = "silverpush"
BidderSmaato BidderName = "smaato"
BidderSmartAdserver BidderName = "smartadserver"
BidderSmartHub BidderName = "smarthub"
BidderSmartRTB BidderName = "smartrtb"
BidderSmartx BidderName = "smartx"
BidderSmartyAds BidderName = "smartyads"
BidderSmileWanted BidderName = "smilewanted"
BidderSonobi BidderName = "sonobi"
BidderSovrn BidderName = "sovrn"
BidderSovrnXsp BidderName = "sovrnXsp"
BidderSspBC BidderName = "sspBC"
BidderStroeerCore BidderName = "stroeerCore"
BidderTaboola BidderName = "taboola"
BidderTappx BidderName = "tappx"
BidderTeads BidderName = "teads"
BidderTelaria BidderName = "telaria"
BidderTpmn BidderName = "tpmn"
BidderTrafficGate BidderName = "trafficgate"
BidderTriplelift BidderName = "triplelift"
BidderTripleliftNative BidderName = "triplelift_native"
BidderUcfunnel BidderName = "ucfunnel"
BidderUndertone BidderName = "undertone"
BidderUnicorn BidderName = "unicorn"
BidderUnruly BidderName = "unruly"
BidderVideoByte BidderName = "videobyte"
BidderVideoHeroes BidderName = "videoheroes"
BidderVidoomy BidderName = "vidoomy"
BidderVisibleMeasures BidderName = "visiblemeasures"
BidderVisx BidderName = "visx"
BidderVox BidderName = "vox"
BidderVrtcal BidderName = "vrtcal"
BidderXeworks BidderName = "xeworks"
BidderYahooAds BidderName = "yahooAds"
BidderYeahmobi BidderName = "yeahmobi"
BidderYieldlab BidderName = "yieldlab"
BidderYieldmo BidderName = "yieldmo"
BidderYieldone BidderName = "yieldone"
BidderZeroClickFraud BidderName = "zeroclickfraud"
BidderZetaGlobalSsp BidderName = "zeta_global_ssp"
)
// CoreBidderNames returns a slice of all core bidders.
func CoreBidderNames() []BidderName {
return coreBidderNames
}
// BuildBidderMap builds a map of string to BidderName, to remain compatbile with the
// prebioud BidderMap variable.
func BuildBidderMap() map[string]BidderName {
lookup := make(map[string]BidderName)
for _, name := range CoreBidderNames() {
lookup[string(name)] = name
}
return lookup
}
// BuildBidderStringSlice builds a slioce of strings for each BidderName.
func BuildBidderStringSlice() []string {
coreBidders := CoreBidderNames()
slice := make([]string, len(coreBidders))
for i, name := range CoreBidderNames() {
slice[i] = string(name)
}
return slice
}
func BuildBidderNameHashSet() map[string]struct{} {
hashSet := make(map[string]struct{})
for _, name := range CoreBidderNames() {
hashSet[string(name)] = struct{}{}
}
return hashSet
}
// bidderNameLookup is a map of the lower case version of the bidder name to the precise BidderName value.
var bidderNameLookup = func() map[string]BidderName {
lookup := make(map[string]BidderName)
for _, name := range CoreBidderNames() {
bidderNameLower := strings.ToLower(string(name))
lookup[bidderNameLower] = name
}
return lookup
}()
func NormalizeBidderName(name string) (BidderName, bool) {
nameLower := strings.ToLower(name)
bidderName, exists := bidderNameLookup[nameLower]
return bidderName, exists
}
// NormalizeBidderNameOrUnchanged returns the normalized name of known bidders, otherwise returns
// the name exactly as provided.
func NormalizeBidderNameOrUnchanged(name string) BidderName {
if normalized, exists := NormalizeBidderName(name); exists {
return normalized
}
return BidderName(name)
}
// The BidderParamValidator is used to enforce bidrequest.imp[i].ext.prebid.bidder.{anyBidder} values.
//
// This is treated differently from the other types because we rely on JSON-schemas to validate bidder params.
type BidderParamValidator interface {
Validate(name BidderName, ext json.RawMessage) error
// Schema returns the JSON schema used to perform validation.
Schema(name BidderName) string
}
type bidderParamsFileSystem interface {
readDir(name string) ([]os.DirEntry, error)
readFile(name string) ([]byte, error)
newReferenceLoader(source string) gojsonschema.JSONLoader
newSchema(l gojsonschema.JSONLoader) (*gojsonschema.Schema, error)
abs(path string) (string, error)
}
type standardBidderParamsFileSystem struct{}
func (standardBidderParamsFileSystem) readDir(name string) ([]os.DirEntry, error) {
return os.ReadDir(name)
}
func (standardBidderParamsFileSystem) readFile(name string) ([]byte, error) {
return os.ReadFile(name)
}
func (standardBidderParamsFileSystem) newReferenceLoader(source string) gojsonschema.JSONLoader {
return gojsonschema.NewReferenceLoader(source)
}
func (standardBidderParamsFileSystem) newSchema(l gojsonschema.JSONLoader) (*gojsonschema.Schema, error) {
return gojsonschema.NewSchema(l)
}
func (standardBidderParamsFileSystem) abs(path string) (string, error) {
return filepath.Abs(path)
}
var paramsValidator bidderParamsFileSystem = standardBidderParamsFileSystem{}
// NewBidderParamsValidator makes a BidderParamValidator, assuming all the necessary files exist in the filesystem.
// This will error if, for example, a Bidder gets added but no JSON schema is written for them.
func NewBidderParamsValidator(schemaDirectory string) (BidderParamValidator, error) {
fileInfos, err := paramsValidator.readDir(schemaDirectory)
if err != nil {
return nil, fmt.Errorf("Failed to read JSON schemas from directory %s. %v", schemaDirectory, err)
}
bidderMap := BuildBidderMap()
schemaContents := make(map[BidderName]string, 50)
schemas := make(map[BidderName]*gojsonschema.Schema, 50)
for _, fileInfo := range fileInfos {
bidderName := strings.TrimSuffix(fileInfo.Name(), ".json")
if _, ok := bidderMap[bidderName]; !ok {
return nil, fmt.Errorf("File %s/%s does not match a valid BidderName.", schemaDirectory, fileInfo.Name())
}
toOpen, err := paramsValidator.abs(filepath.Join(schemaDirectory, fileInfo.Name()))
if err != nil {
return nil, fmt.Errorf("Failed to get an absolute representation of the path: %s, %v", toOpen, err)
}
schemaLoader := paramsValidator.newReferenceLoader("file:///" + filepath.ToSlash(toOpen))
loadedSchema, err := paramsValidator.newSchema(schemaLoader)
if err != nil {
return nil, fmt.Errorf("Failed to load json schema at %s: %v", toOpen, err)
}
fileBytes, err := paramsValidator.readFile(fmt.Sprintf("%s/%s", schemaDirectory, fileInfo.Name()))
if err != nil {
return nil, fmt.Errorf("Failed to read file %s/%s: %v", schemaDirectory, fileInfo.Name(), err)
}
schemas[BidderName(bidderName)] = loadedSchema
schemaContents[BidderName(bidderName)] = string(fileBytes)
}
// set alias bidder params schema to its parent
for alias, parent := range aliasBidderToParent {
parentSchema := schemas[parent]
schemas[alias] = parentSchema
parentSchemaContents := schemaContents[parent]
schemaContents[alias] = parentSchemaContents
}
return &bidderParamValidator{
schemaContents: schemaContents,
parsedSchemas: schemas,
}, nil
}
type bidderParamValidator struct {
schemaContents map[BidderName]string
parsedSchemas map[BidderName]*gojsonschema.Schema
}
func (validator *bidderParamValidator) Validate(name BidderName, ext json.RawMessage) error {
result, err := validator.parsedSchemas[name].Validate(gojsonschema.NewBytesLoader(ext))
if err != nil {
return err
}
if !result.Valid() {
errBuilder := bytes.NewBuffer(make([]byte, 0, 300))
for _, err := range result.Errors() {
errBuilder.WriteString(err.String())
}
return errors.New(errBuilder.String())
}
return nil
}
func (validator *bidderParamValidator) Schema(name BidderName) string {
return validator.schemaContents[name]
}