@@ -24,8 +24,8 @@ var notificationCenter = require('../core/notification_center');
24
24
var projectConfig = require ( '../core/project_config' ) ;
25
25
var projectConfigSchema = require ( './project_config_schema' ) ;
26
26
var sprintf = require ( 'sprintf' ) ;
27
- var userIdValidator = require ( '../utils/user_id_validator' ) ;
28
27
var userProfileServiceValidator = require ( '../utils/user_profile_service_validator' ) ;
28
+ var stringValidator = require ( '../utils/string_value_validator' ) ;
29
29
30
30
var ERROR_MESSAGES = enums . ERROR_MESSAGES ;
31
31
var LOG_LEVEL = enums . LOG_LEVEL ;
@@ -129,6 +129,10 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
129
129
return null ;
130
130
}
131
131
132
+ if ( ! this . __validateInputs ( { experiment_key : experimentKey , user_id : userId } , attributes ) ) {
133
+ return this . __notActivatingExperiment ( experimentKey , userId ) ;
134
+ }
135
+
132
136
try {
133
137
var variationKey = this . getVariation ( experimentKey , userId , attributes ) ;
134
138
if ( variationKey === null ) {
@@ -221,7 +225,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
221
225
}
222
226
223
227
try {
224
- if ( ! this . __validateInputs ( userId , attributes , eventTags ) ) {
228
+ if ( ! this . __validateInputs ( { user_id : userId , event_key : eventKey } , attributes , eventTags ) ) {
225
229
return ;
226
230
}
227
231
@@ -297,7 +301,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
297
301
}
298
302
299
303
try {
300
- if ( ! this . __validateInputs ( userId , attributes ) ) {
304
+ if ( ! this . __validateInputs ( { experiment_key : experimentKey , user_id : userId } , attributes ) ) {
301
305
return null ;
302
306
}
303
307
@@ -349,15 +353,21 @@ Optimizely.prototype.getForcedVariation = function(experimentKey, userId) {
349
353
350
354
/**
351
355
* Validates user ID and attributes parameters
352
- * @param {string } userId ID of user
356
+ * @param {string } stringInputs Map of string keys and associated values
353
357
* @param {Object } userAttributes Optional parameter for user's attributes
354
358
* @param {Object } eventTags Optional parameter for event tags
355
359
* @return {boolean } True if inputs are valid
356
360
*
357
361
*/
358
- Optimizely . prototype . __validateInputs = function ( userId , userAttributes , eventTags ) {
362
+ Optimizely . prototype . __validateInputs = function ( stringInputs , userAttributes , eventTags ) {
359
363
try {
360
- userIdValidator . validate ( userId ) ;
364
+ var inputKeys = Object . keys ( stringInputs ) ;
365
+ for ( var index = 0 ; index < inputKeys . length ; index ++ ) {
366
+ var key = inputKeys [ index ] ;
367
+ if ( ! stringValidator . validate ( stringInputs [ key ] ) ) {
368
+ throw new Error ( sprintf ( ERROR_MESSAGES . INVALID_INPUT_FORMAT , MODULE_NAME , key ) ) ;
369
+ }
370
+ }
361
371
if ( userAttributes ) {
362
372
attributesValidator . validate ( userAttributes ) ;
363
373
}
@@ -473,12 +483,12 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
473
483
return false ;
474
484
}
475
485
476
- var feature = projectConfig . getFeatureFromKey ( this . configObj , featureKey , this . logger ) ;
477
- if ( ! feature ) {
486
+ if ( ! this . __validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
478
487
return false ;
479
488
}
480
489
481
- if ( ! this . __validateInputs ( userId , attributes ) ) {
490
+ var feature = projectConfig . getFeatureFromKey ( this . configObj , featureKey , this . logger ) ;
491
+ if ( ! feature ) {
482
492
return false ;
483
493
}
484
494
@@ -546,6 +556,10 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
546
556
return null ;
547
557
}
548
558
559
+ if ( ! this . __validateInputs ( { feature_key : featureKey , variable_key : variableKey , user_id : userId } , attributes ) ) {
560
+ return null ;
561
+ }
562
+
549
563
var featureFlag = projectConfig . getFeatureFromKey ( this . configObj , featureKey , this . logger ) ;
550
564
if ( ! featureFlag ) {
551
565
return null ;
@@ -561,10 +575,6 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
561
575
return null ;
562
576
}
563
577
564
- if ( ! this . __validateInputs ( userId , attributes ) ) {
565
- return null ;
566
- }
567
-
568
578
var decision = this . decisionService . getVariationForFeature ( featureFlag , userId , attributes ) ;
569
579
var variableValue ;
570
580
if ( decision . variation !== null ) {
0 commit comments