Skip to content

[OASIS-6102]: changed functionality of the JSON schema validator module #438

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 7 commits into from
Apr 3, 2020
Merged
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
3 changes: 1 addition & 2 deletions packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var fns = require('../../utils/fns');
var enums = require('../../utils/enums');
var jsSdkUtils = require('@optimizely/js-sdk-utils');
var configValidator = require('../../utils/config_validator');
var projectConfigSchema = require('./project_config_schema');

var EXPERIMENT_RUNNING_STATUS = 'Running';
var RESERVED_ATTRIBUTE_PREFIX = '$opt_';
Expand Down Expand Up @@ -508,7 +507,7 @@ module.exports = {
if (config.skipJSONValidation === true) {
config.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME));
} else if (config.jsonSchemaValidator) {
config.jsonSchemaValidator.validate(projectConfigSchema, config.datafile);
config.jsonSchemaValidator.validate(config.datafile);
config.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME));
}
return module.exports.createProjectConfig(config.datafile);
Expand Down
1 change: 0 additions & 1 deletion packages/optimizely-sdk/lib/utils/enums/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ exports.ERROR_MESSAGES = {
INVALID_ROLLOUT_ID: '%s: Invalid rollout ID %s attached to feature %s',
INVALID_USER_ID: '%s: Provided user ID is in an invalid format.',
INVALID_USER_PROFILE_SERVICE: '%s: Provided user profile service instance is in an invalid format: %s.',
JSON_SCHEMA_EXPECTED: '%s: JSON schema expected.',
NO_DATAFILE_SPECIFIED: '%s: No datafile specified. Cannot start optimizely.',
NO_JSON_PROVIDED: '%s: No JSON object to validate against schema.',
NO_VARIATION_FOR_EXPERIMENT_KEY: '%s: No variation key %s defined in datafile for experiment %s.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
var validate = require('json-schema').validate;
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var projectConfigSchema = require('../../core/project_config/project_config_schema');

var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
var MODULE_NAME = 'JSON_SCHEMA_VALIDATOR';
Expand All @@ -26,16 +27,12 @@ module.exports = {
* @param {Object} jsonObject The object to validate against the schema
* @return {Boolean} True if the given object is valid
*/
validate: function(jsonSchema, jsonObject) {
if (!jsonSchema) {
throw new Error(sprintf(ERROR_MESSAGES.JSON_SCHEMA_EXPECTED, MODULE_NAME));
}

validate: function(jsonObject) {
if (!jsonObject) {
throw new Error(sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, MODULE_NAME));
}

var result = validate(jsonObject, jsonSchema);
var result = validate(jsonObject, projectConfigSchema);
if (result.valid) {
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
var chai = require('chai');
var assert = chai.assert;
var jsonSchemaValidator = require('./');
var projectConfigSchema = require('../../core/project_config/project_config_schema');
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var testData = require('../../tests/test_data.js');

Expand All @@ -25,30 +24,20 @@ var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
describe('lib/utils/json_schema_validator', function() {
describe('APIs', function() {
describe('validate', function() {
it('should validate the given object against the specified schema', function() {
assert.isTrue(jsonSchemaValidator.validate({ type: 'number' }, 4));
});

it('should throw an error if the object is not valid', function() {
assert.throws(function() {
jsonSchemaValidator.validate({ type: 'number' }, 'not a number');
}, 'string value found, but a number is required');
});

it('should throw an error if no schema is passed in', function() {
assert.throws(function() {
jsonSchemaValidator.validate();
}, sprintf(ERROR_MESSAGES.JSON_SCHEMA_EXPECTED, 'JSON_SCHEMA_VALIDATOR'));
jsonSchemaValidator.validate({});
});
});

it('should throw an error if no json object is passed in', function() {
assert.throws(function() {
jsonSchemaValidator.validate({ type: 'number' });
jsonSchemaValidator.validate();
}, sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, 'JSON_SCHEMA_VALIDATOR'));
});

it('should validate specified Optimizely datafile with the Optimizely datafile schema', function() {
assert.isTrue(jsonSchemaValidator.validate(projectConfigSchema, testData.getTestProjectConfig()));
it('should validate specified Optimizely datafile', function() {
assert.isTrue(jsonSchemaValidator.validate(testData.getTestProjectConfig()));
});
});
});
Expand Down