forked from indexexchange/improve-digital-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
improve-digital-htb-validator.js
137 lines (125 loc) · 5.17 KB
/
improve-digital-htb-validator.js
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
/**
* @author: Partner
* @license: UNLICENSED
*
* @copyright: Copyright (c) 2017 by Index Exchange. All rights reserved.
*
* The information contained within this document is confidential, copyrighted
* and or a trade secret. No part of this document may be reproduced or
* distributed in any form or by any means, in whole or in part, without the
* prior written permission of Index Exchange.
*/
'use strict';
////////////////////////////////////////////////////////////////////////////////
// Dependencies ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var Inspector = require('../../../libs/external/schema-inspector.js');
var custom = {
impliesPresence: function (schema, candidate) {
var implication = schema.$impliesPresence;
var impliedIf = implication.if;
var impliedThen = implication.then;
if (candidate[impliedIf] && typeof candidate[impliedThen] === 'undefined') {
this.report('if ' + impliedIf + 'exists then ' + impliedThen + ' must also exist');
}
}
};
Inspector.Validation.extend(custom);
custom = {
mustIncludeOneOnly: function (schema, candidate) {
var mustIncludeOneOnly = schema.$mustIncludeOneOnly.params;
var occurrenceCounter = 0;
for (var index = 0; index < mustIncludeOneOnly.length; index++) {
if (candidate[mustIncludeOneOnly[index]]) {
occurrenceCounter++;
}
}
if (occurrenceCounter != 1) {
this.report('One and only one of the following must be present: ' + JSON.stringify(mustIncludeOneOnly));
}
}
};
Inspector.Validation.extend(custom);
////////////////////////////////////////////////////////////////////////////////
// Main ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/* =============================================================================
* STEP 0 | Config Validation
* -----------------------------------------------------------------------------
* This file contains the necessary validation for the partner configuration.
* This validation will be performed on the partner specific configuration object
* that is passed into the wrapper. The wrapper uses an outside library called
* schema-insepctor to perform the validation. Information about it can be found here:
* https://atinux.fr/schema-inspector/.
*/
var partnerValidator = function (configs) {
var result = Inspector.validate({
type: 'object',
properties: {
xSlots: {
type: 'object',
properties: {
'*': {
type: 'object',
someKeys: ['placementId', 'placementKey'],
$impliesPresence : {
if: "placementKey",
then: "publisherId"
},
$mustIncludeOneOnly : {
params: ["placementId", "placementKey"]
},
properties: {
currency: {
type: 'string',
optional: true,
pattern: /^USD|EUR|GBP|AUD|DKK|SEK|CZK|CHF|NOK$/
},
placementId: {
type: 'number',
optional: true
},
size:{
type: 'object',
optional: true,
properties: {
w: {
type: 'number'
},
h: {
type: 'number'
}
}
},
publisherId: {
type: 'number',
minLength: 1,
optional: true
},
placementKey: {
type: 'string',
minLength: 1,
optional: true
},
keyValues: {
type: 'object',
optional: true,
properties:{
'*': {
type: 'array',
items: { type: 'string'}
}
}
}
}
}
}
}
}
}, configs);
if (!result.valid) {
return result.format();
}
return null;
};
module.exports = partnerValidator;