-
Notifications
You must be signed in to change notification settings - Fork 83
fix(initialize): Prevent SDK from initializing if the datafile version in invalid. #161
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Just minor changes
@@ -146,6 +146,22 @@ describe('lib/core/project_config', function() { | |||
assert.deepEqual(configObj.variationIdMap, expectedVariationIdMap); | |||
}); | |||
|
|||
it('should throw exception with invalid config version', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. throw an error with unsupported datafile version
Same below.
@@ -152,6 +152,27 @@ describe('lib/optimizely', function() { | |||
assert.strictEqual(logMessage, sprintf(ERROR_MESSAGES.INVALID_DATAFILE, 'JSON_SCHEMA_VALIDATOR', 'projectId', 'is missing and it is required')); | |||
}); | |||
|
|||
it('should log an error if the datafile version is not valid', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. is not supported
/* | ||
* Possible types of variables attached to features | ||
*/ | ||
exports.CONFIG_VERSIONS = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. DATAFILE_VERSIONS
@@ -186,3 +187,12 @@ exports.FEATURE_VARIABLE_TYPES = { | |||
INTEGER: 'integer', | |||
STRING: 'string', | |||
}; | |||
|
|||
/* | |||
* Possible types of variables attached to features |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. Remove comment
@@ -59,6 +59,7 @@ exports.ERROR_MESSAGES = { | |||
VARIATION_ID_NOT_IN_DATAFILE: '%s: No variation ID %s defined in datafile for experiment %s.', | |||
VARIATION_ID_NOT_IN_DATAFILE_NO_EXPERIMENT: '%s: Variation ID %s is not in the datafile.', | |||
INVALID_INPUT_FORMAT: '%s: Provided %s is in an invalid format.', | |||
INVALID_CONFIG_VERSION: '%s: This version of the Javascript SDK does not support the given datafile version: %s', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit UNSUPPORTED_DATAFILE_VERSION
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have all the validation in the config_validator
module. Please move this logic there are well: https://github.com/optimizely/javascript-sdk/blob/master/packages/optimizely-sdk/lib/utils/config_validator/index.js#L35
Currently most of the datafile parsing and validations are performed inside Optimizely constructor. The config_validator module actually validates logger and error handler instances. |
Yep, we should move the version validation out of the project_config into the config validator, as well as lines 63 through 77 from the Optimizely constructor into the config validator as well. Moving code out of that constructor makes it more maintainable and easier to follow |
…dator's validate method.
Please don't review this PR. Some of the changes needed to make in this PR, will let you know when ready. |
…dator's validateDatafile method.
@mikeng13 : The approach we should take to isolate datafile validations from Optimizely constructor requires more rework. I have some queries related to that. Right now, I moved datafile validations in a separate config_validator method and called it from Optimizely constructor. To completely isolate validations from Optimizely constructor, we should perform it in createInstance method and move all the instance creation unit tests from optimizely.tests to index.node.tests.js and index.browser.tests.js files. Should I go with that or the current implementation is enough? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfahadahmed no need to move the other validation code for now. This is enough of a refactor. I've also requested some changes in a few places.
* Validates the datafile | ||
* @param {string} datafile | ||
* @return {Boolean} True if the datafile is valid | ||
* @throws If any of the datafile validations get failed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@throws If the datafile is not valid for any of the following reasons:
- The datafile string is undefined
- The datafile string cannot be parsed as a JSON object
- The datafile version is not supported
}, sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, 'CONFIG_VALIDATOR')); | ||
}); | ||
|
||
it('should complain if datafile version is not supported', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some more test cases for things like boolean
, number
, array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm this, it is not really needed.
@@ -59,6 +59,7 @@ exports.ERROR_MESSAGES = { | |||
VARIATION_ID_NOT_IN_DATAFILE: '%s: No variation ID %s defined in datafile for experiment %s.', | |||
VARIATION_ID_NOT_IN_DATAFILE_NO_EXPERIMENT: '%s: Variation ID %s is not in the datafile.', | |||
INVALID_INPUT_FORMAT: '%s: Provided %s is in an invalid format.', | |||
INVALID_DATAFILE_VERSION: '%s: This version of the Javascript SDK does not support the given datafile version: %s', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JavaScript
createdLogger.log.restore(); | ||
}); | ||
|
||
it('should log an error when datafile is not provided', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are duplicate test cases from the config validator. We just need two test cases:
- datafile is valid
- datafile validation throws error (any) error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we need to add only 1 test case here. That config validator does get called from createInstance and if it throws an error, assert that the isValidInstance is set to False.
The datafile validations are already taken care of in config validator tests.
|
||
it('should create optimizely instance with valid datafile', function() { | ||
var optlyInstance = optimizelyFactory.createInstance({ | ||
datafile: JSON.stringify(testData.getUnsupportedVersionConfig()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case, you keep this test case, assert that validator doesn't thrown an error and isValidInstance is true. Instance will always be created like it does here with UnsupportedVersionConfig
Oh I think none of the datafile validation tests are needed in index.node.tests.js as all of these test cases are already covered in optimizely.tests. Sorry, I forgot to remove these test cases from index.node.tests.js after changing validation logic. |
@@ -17,6 +17,13 @@ var sprintf = require('sprintf-js').sprintf; | |||
|
|||
var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES; | |||
var MODULE_NAME = 'CONFIG_VALIDATOR'; | |||
var DATAFILE_VERSIONS = require('../enums').DATAFILE_VERSIONS; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfahadahmed Please update header for this file and it's test file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm
No description provided.