@@ -24,8 +24,8 @@ var notificationCenter = require('../core/notification_center');
2424var projectConfig = require ( '../core/project_config' ) ;
2525var projectConfigSchema = require ( './project_config_schema' ) ;
2626var sprintf = require ( 'sprintf' ) ;
27- var userIdValidator = require ( '../utils/user_id_validator' ) ;
2827var userProfileServiceValidator = require ( '../utils/user_profile_service_validator' ) ;
28+ var stringValidator = require ( '../utils/string_value_validator' ) ;
2929
3030var ERROR_MESSAGES = enums . ERROR_MESSAGES ;
3131var LOG_LEVEL = enums . LOG_LEVEL ;
@@ -129,6 +129,10 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
129129 return null ;
130130 }
131131
132+ if ( ! this . __validateInputs ( { experiment_key : experimentKey , user_id : userId } , attributes ) ) {
133+ return this . __notActivatingExperiment ( experimentKey , userId ) ;
134+ }
135+
132136 try {
133137 var variationKey = this . getVariation ( experimentKey , userId , attributes ) ;
134138 if ( variationKey === null ) {
@@ -221,7 +225,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
221225 }
222226
223227 try {
224- if ( ! this . __validateInputs ( userId , attributes , eventTags ) ) {
228+ if ( ! this . __validateInputs ( { user_id : userId , event_key : eventKey } , attributes , eventTags ) ) {
225229 return ;
226230 }
227231
@@ -297,7 +301,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
297301 }
298302
299303 try {
300- if ( ! this . __validateInputs ( userId , attributes ) ) {
304+ if ( ! this . __validateInputs ( { experiment_key : experimentKey , user_id : userId } , attributes ) ) {
301305 return null ;
302306 }
303307
@@ -349,15 +353,21 @@ Optimizely.prototype.getForcedVariation = function(experimentKey, userId) {
349353
350354/**
351355 * Validates user ID and attributes parameters
352- * @param {string } userId ID of user
356+ * @param {string } stringInputs Map of string keys and associated values
353357 * @param {Object } userAttributes Optional parameter for user's attributes
354358 * @param {Object } eventTags Optional parameter for event tags
355359 * @return {boolean } True if inputs are valid
356360 *
357361 */
358- Optimizely . prototype . __validateInputs = function ( userId , userAttributes , eventTags ) {
362+ Optimizely . prototype . __validateInputs = function ( stringInputs , userAttributes , eventTags ) {
359363 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+ }
361371 if ( userAttributes ) {
362372 attributesValidator . validate ( userAttributes ) ;
363373 }
@@ -439,12 +449,12 @@ Optimizely.prototype.__notActivatingExperiment = function(experimentKey, userId)
439449Optimizely . prototype . __dispatchEvent = function ( eventToDispatch , callback ) {
440450 var eventDispatcherResponse = this . eventDispatcher . dispatchEvent ( eventToDispatch , callback ) ;
441451 //checking that response value is a promise, not a request object
442- if ( typeof eventDispatcherResponse == "object" && ! eventDispatcherResponse . hasOwnProperty ( 'uri' ) ) {
452+ if ( ! fns . isEmpty ( eventDispatcherResponse ) && typeof eventDispatcherResponse . then === 'function' ) {
443453 eventDispatcherResponse . then ( function ( ) {
444454 callback ( ) ;
445455 } ) ;
446456 }
447- }
457+ } ;
448458
449459/**
450460 * Filters out attributes/eventTags with null or undefined values
@@ -454,11 +464,11 @@ Optimizely.prototype.__dispatchEvent = function (eventToDispatch, callback) {
454464Optimizely . prototype . __filterEmptyValues = function ( map ) {
455465 for ( var key in map ) {
456466 if ( map . hasOwnProperty ( key ) && ( map [ key ] === null || map [ key ] === undefined ) ) {
457- delete map [ key ]
467+ delete map [ key ] ;
458468 }
459469 }
460470 return map ;
461- }
471+ } ;
462472
463473/**
464474 * Returns true if the feature is enabled for the given user.
@@ -473,12 +483,12 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
473483 return false ;
474484 }
475485
476- var feature = projectConfig . getFeatureFromKey ( this . configObj , featureKey , this . logger ) ;
477- if ( ! feature ) {
486+ if ( ! this . __validateInputs ( { feature_key : featureKey , user_id : userId } , attributes ) ) {
478487 return false ;
479488 }
480489
481- if ( ! this . __validateInputs ( userId , attributes ) ) {
490+ var feature = projectConfig . getFeatureFromKey ( this . configObj , featureKey , this . logger ) ;
491+ if ( ! feature ) {
482492 return false ;
483493 }
484494
@@ -546,6 +556,10 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
546556 return null ;
547557 }
548558
559+ if ( ! this . __validateInputs ( { feature_key : featureKey , variable_key : variableKey , user_id : userId } , attributes ) ) {
560+ return null ;
561+ }
562+
549563 var featureFlag = projectConfig . getFeatureFromKey ( this . configObj , featureKey , this . logger ) ;
550564 if ( ! featureFlag ) {
551565 return null ;
@@ -561,10 +575,6 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
561575 return null ;
562576 }
563577
564- if ( ! this . __validateInputs ( userId , attributes ) ) {
565- return null ;
566- }
567-
568578 var decision = this . decisionService . getVariationForFeature ( featureFlag , userId , attributes ) ;
569579 var variableValue ;
570580 if ( decision . variation !== null ) {
0 commit comments