Skip to content

2.0.2 #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 24, 2018
Merged

2.0.2 #104

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ node_js:
- '4'
- '6'
- '8'
- node
- '9'
# TODO(#102): restore node/10 config
- '10.0'
branches:
only:
- master
Expand Down
7 changes: 7 additions & 0 deletions packages/optimizely-sdk/CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.2
May 24, 2018

* Remove [`request`](https://www.npmjs.com/package/request) dependency ([#98](https://github.com/optimizely/javascript-sdk/pull/98))
* Add package-lock.json ([#100](https://github.com/optimizely/javascript-sdk/pull/100))
* Input validation in Activate, Track, and GetVariation methods ([#91](https://github.com/optimizely/javascript-sdk/pull/91) by [@mfahadahmed](https://github.com/mfahadahmed))

## 2.0.1
April 16th, 2018

Expand Down
44 changes: 27 additions & 17 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var notificationCenter = require('../core/notification_center');
var projectConfig = require('../core/project_config');
var projectConfigSchema = require('./project_config_schema');
var sprintf = require('sprintf');
var userIdValidator = require('../utils/user_id_validator');
var userProfileServiceValidator = require('../utils/user_profile_service_validator');
var stringValidator = require('../utils/string_value_validator');

var ERROR_MESSAGES = enums.ERROR_MESSAGES;
var LOG_LEVEL = enums.LOG_LEVEL;
Expand Down Expand Up @@ -129,6 +129,10 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
return null;
}

if (!this.__validateInputs({experiment_key: experimentKey, user_id: userId}, attributes)) {
return this.__notActivatingExperiment(experimentKey, userId);
}

try {
var variationKey = this.getVariation(experimentKey, userId, attributes);
if (variationKey === null) {
Expand Down Expand Up @@ -221,7 +225,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
}

try {
if (!this.__validateInputs(userId, attributes, eventTags)) {
if (!this.__validateInputs({user_id: userId, event_key: eventKey}, attributes, eventTags)) {
return;
}

Expand Down Expand Up @@ -297,7 +301,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
}

try {
if (!this.__validateInputs(userId, attributes)) {
if (!this.__validateInputs({experiment_key: experimentKey, user_id: userId}, attributes)) {
return null;
}

Expand Down Expand Up @@ -349,15 +353,21 @@ Optimizely.prototype.getForcedVariation = function(experimentKey, userId) {

/**
* Validates user ID and attributes parameters
* @param {string} userId ID of user
* @param {string} stringInputs Map of string keys and associated values
* @param {Object} userAttributes Optional parameter for user's attributes
* @param {Object} eventTags Optional parameter for event tags
* @return {boolean} True if inputs are valid
*
*/
Optimizely.prototype.__validateInputs = function(userId, userAttributes, eventTags) {
Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, eventTags) {
try {
userIdValidator.validate(userId);
var inputKeys = Object.keys(stringInputs);
for (var index = 0; index < inputKeys.length; index++) {
var key = inputKeys[index];
if (!stringValidator.validate(stringInputs[key])) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
}
}
if (userAttributes) {
attributesValidator.validate(userAttributes);
}
Expand Down Expand Up @@ -439,12 +449,12 @@ Optimizely.prototype.__notActivatingExperiment = function(experimentKey, userId)
Optimizely.prototype.__dispatchEvent = function (eventToDispatch, callback) {
var eventDispatcherResponse = this.eventDispatcher.dispatchEvent(eventToDispatch, callback);
//checking that response value is a promise, not a request object
if (typeof eventDispatcherResponse == "object" && !eventDispatcherResponse.hasOwnProperty('uri')) {
if (!fns.isEmpty(eventDispatcherResponse) && typeof eventDispatcherResponse.then === 'function') {
eventDispatcherResponse.then(function() {
callback();
});
}
}
};

/**
* Filters out attributes/eventTags with null or undefined values
Expand All @@ -454,11 +464,11 @@ Optimizely.prototype.__dispatchEvent = function (eventToDispatch, callback) {
Optimizely.prototype.__filterEmptyValues = function (map) {
for (var key in map) {
if (map.hasOwnProperty(key) && (map[key] === null || map[key] === undefined)) {
delete map[key]
delete map[key];
}
}
return map;
}
};

/**
* Returns true if the feature is enabled for the given user.
Expand All @@ -473,12 +483,12 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
return false;
}

var feature = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
if (!feature) {
if (!this.__validateInputs({feature_key: featureKey, user_id: userId}, attributes)) {
return false;
}

if (!this.__validateInputs(userId, attributes)) {
var feature = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
if (!feature) {
return false;
}

Expand Down Expand Up @@ -546,6 +556,10 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
return null;
}

if (!this.__validateInputs({feature_key: featureKey, variable_key: variableKey, user_id: userId}, attributes)) {
return null;
}

var featureFlag = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
if (!featureFlag) {
return null;
Expand All @@ -561,10 +575,6 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
return null;
}

if (!this.__validateInputs(userId, attributes)) {
return null;
}

var decision = this.decisionService.getVariationForFeature(featureFlag, userId, attributes);
var variableValue;
if (decision.variation !== null) {
Expand Down
Loading