MCPcopy Create free account
hub / github.com/ampproject/amphtml / assertVariant

Function assertVariant

extensions/amp-experiment/1.0/variant.js:208–236  ·  view source on GitHub ↗

* Validates the variant schema of a config. * @param {!JsonObject} variant * @param {string} experimentName * @param {string} variantName * @throws {!Error}

(variant, experimentName, variantName)

Source from the content-addressed store, hash-verified

206 * @throws {!Error}
207 */
208function assertVariant(variant, experimentName, variantName) {
209 // Assert that the variant is an object
210 userAssert(
211 isObject(variant),
212 `${experimentName}.variants.${variantName} must be an object.`
213 );
214
215 // Assert the variant weight
216 userAssert(
217 variant['weight'] !== undefined && typeof variant['weight'] === 'number',
218 `${experimentName}.variants.${variantName} must have a number weight.`
219 );
220
221 // Assert the variant weight is a percentage
222 const percentage = variant['weight'];
223 userAssert(
224 percentage > 0 && percentage < 100,
225 'Invalid weight percentage %s.' +
226 ` ${experimentName}.variants.${variantName}` +
227 ' Has to be greater than 0 and less than 100',
228 percentage
229 );
230
231 // Assert the variant mutations
232 userAssert(
233 variant['mutations'] && isArray(variant['mutations']),
234 `${experimentName}.variants.${variantName} must have a mutations array.`
235 );
236}

Callers 1

validateExperimentFunction · 0.85

Calls 3

userAssertFunction · 0.90
isObjectFunction · 0.90
isArrayFunction · 0.85

Tested by

no test coverage detected