Skip to content

refactor: Convert lib/utils to ES module (Part 1/2) #451

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 8 commits into from
Apr 10, 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
4 changes: 2 additions & 2 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { sprintf, objectValues } from '@optimizely/js-sdk-utils';
import * as eventProcessor from '@optimizely/js-sdk-event-processor';

import fns from '../utils/fns'
import attributesValidator from '../utils/attributes_validator';
import { validate } from '../utils/attributes_validator';
import decisionService from '../core/decision_service';
import enums from '../utils/enums';
import eventBuilder from '../core/event_builder/index.js';
Expand Down Expand Up @@ -499,7 +499,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
}
}
if (userAttributes) {
attributesValidator.validate(userAttributes);
validate(userAttributes);
}
if (eventTags) {
eventTagsValidator.validate(eventTags);
Expand Down
72 changes: 37 additions & 35 deletions packages/optimizely-sdk/lib/utils/attributes_validator/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, 2018-2019, Optimizely
* Copyright 2016, 2018-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,43 +13,45 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { sprintf } from '@optimizely/js-sdk-utils';

/**
* Provides utility method for validating that the attributes user has provided are valid
*/
import fns from '../../utils/fns';
import { ERROR_MESSAGES } from '../enums';

var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var fns = require('../../utils/fns');

var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
var MODULE_NAME = 'ATTRIBUTES_VALIDATOR';

module.exports = {
/**
* Validates user's provided attributes
* @param {Object} attributes
* @return {boolean} True if the attributes are valid
* @throws If the attributes are not valid
*/
validate: function(attributes) {
if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) {
Object.keys(attributes).forEach(function(key) {
if (typeof attributes[key] === 'undefined') {
throw new Error(sprintf(ERROR_MESSAGES.UNDEFINED_ATTRIBUTE, MODULE_NAME, key));
}
});
return true;
} else {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME));
}
},
/**
* Validates user's provided attributes
* @param {Object} attributes
* @return {boolean} True if the attributes are valid
* @throws If the attributes are not valid
*/
export var validate = function(attributes) {
if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) {
Object.keys(attributes).forEach(function(key) {
if (typeof attributes[key] === 'undefined') {
throw new Error(sprintf(ERROR_MESSAGES.UNDEFINED_ATTRIBUTE, MODULE_NAME, key));
}
});
return true;
} else {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME));
}
};

export var isAttributeValid = function(attributeKey, attributeValue) {
return (
typeof attributeKey === 'string' &&
(typeof attributeValue === 'string' ||
typeof attributeValue === 'boolean' ||
(fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue)))
);
};

isAttributeValid: function(attributeKey, attributeValue) {
return (
typeof attributeKey === 'string' &&
(typeof attributeValue === 'string' ||
typeof attributeValue === 'boolean' ||
(fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue)))
);
},
/**
* Provides utility method for validating that the attributes user has provided are valid
*/
export default {
validate: validate,
isAttributeValid: isAttributeValid,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, 2018-2019, Optimizely
* Copyright 2016, 2018-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,13 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var chai = require('chai');
var assert = chai.assert;
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var attributesValidator = require('./');
var fns = require('./../fns/');
import { assert } from 'chai';
import { sprintf } from '@optimizely/js-sdk-utils';

var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
import attributesValidator from './';
import { ERROR_MESSAGES } from '../enums';

describe('lib/utils/attributes_validator', function() {
describe('APIs', function() {
Expand Down
111 changes: 58 additions & 53 deletions packages/optimizely-sdk/lib/utils/config_validator/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, 2018, 2019 Optimizely
* Copyright 2016, 2018-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,70 +13,75 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
import { sprintf } from '@optimizely/js-sdk-utils';

var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
var MODULE_NAME = 'CONFIG_VALIDATOR';
var DATAFILE_VERSIONS = require('../enums').DATAFILE_VERSIONS;
import {
ERROR_MESSAGES,
DATAFILE_VERSIONS,
} from '../enums';

var MODULE_NAME = 'CONFIG_VALIDATOR';
var SUPPORTED_VERSIONS = [DATAFILE_VERSIONS.V2, DATAFILE_VERSIONS.V3, DATAFILE_VERSIONS.V4];

/**
* Provides utility methods for validating that the configuration options are valid
* Validates the given config options
* @param {Object} config
* @param {Object} config.errorHandler
* @param {Object} config.eventDispatcher
* @param {Object} config.logger
* @return {Boolean} True if the config options are valid
* @throws If any of the config options are not valid
*/
module.exports = {
/**
* Validates the given config options
* @param {Object} config
* @param {Object} config.errorHandler
* @param {Object} config.eventDispatcher
* @param {Object} config.logger
* @return {Boolean} True if the config options are valid
* @throws If any of the config options are not valid
*/
validate: function(config) {
if (config.errorHandler && typeof config.errorHandler.handleError !== 'function') {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME));
}
export var validate = function(config) {
if (config.errorHandler && typeof config.errorHandler.handleError !== 'function') {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME));
}

if (config.eventDispatcher && typeof config.eventDispatcher.dispatchEvent !== 'function') {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME));
}
if (config.eventDispatcher && typeof config.eventDispatcher.dispatchEvent !== 'function') {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME));
}

if (config.logger && typeof config.logger.log !== 'function') {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME));
}
if (config.logger && typeof config.logger.log !== 'function') {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME));
}

return true;
},
return true;
};

/**
* Validates the datafile
* @param {string} datafile
* @return {Boolean} True if the datafile is valid
* @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
*/
validateDatafile: function(datafile) {
if (!datafile) {
throw new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME));
}
/**
* Validates the datafile
* @param {string} datafile
* @return {Boolean} True if the datafile is valid
* @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
*/
export var validateDatafile = function(datafile) {
if (!datafile) {
throw new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME));
}

if (typeof datafile === 'string' || datafile instanceof String) {
// Attempt to parse the datafile string
try {
datafile = JSON.parse(datafile);
} catch (ex) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME));
}
if (typeof datafile === 'string' || datafile instanceof String) {
// Attempt to parse the datafile string
try {
datafile = JSON.parse(datafile);
} catch (ex) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME));
}
}

if (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version));
}
if (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version));
}

return true;
};

return true;
},
/**
* Provides utility methods for validating that the configuration options are valid
*/
export default {
validate: validate,
validateDatafile: validateDatafile,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, 2018, 2019 Optimizely
* Copyright 2016, 2018-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var chai = require('chai');
var assert = chai.assert;
var configValidator = require('./');
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var testData = require('../../tests/test_data');
import { assert } from 'chai';
import { sprintf } from '@optimizely/js-sdk-utils';

var ERROR_MESSAGES = require('../enums').ERROR_MESSAGES;
import configValidator from './';
import { ERROR_MESSAGES } from '../enums';
import testData from '../../tests/test_data';

describe('lib/utils/config_validator', function() {
describe('APIs', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,28 +13,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var fns = require('../fns');
import { isSafeInteger } from '../fns';

/**
* Return true if the argument is a valid event batch size, false otherwise
* @param {*} eventBatchSize
* @returns boolean
*/
function validateEventBatchSize(eventBatchSize) {
return fns.isSafeInteger(eventBatchSize) && eventBatchSize >= 1;
}
export var validateEventBatchSize = function(eventBatchSize) {
return isSafeInteger(eventBatchSize) && eventBatchSize >= 1;
};

/**
* Return true if the argument is a valid event flush interval, false otherwise
* @param {*} eventFlushInterval
* @returns boolean
*/
function validateEventFlushInterval(eventFlushInterval) {
return fns.isSafeInteger(eventFlushInterval) && eventFlushInterval > 0;
}
export var validateEventFlushInterval = function(eventFlushInterval) {
return isSafeInteger(eventFlushInterval) && eventFlushInterval > 0;
};

module.exports = {
export default {
validateEventBatchSize: validateEventBatchSize,
validateEventFlushInterval: validateEventFlushInterval,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,11 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from 'chai';

var chai = require('chai');
var eventProcessorConfigValidator = require('./index');

var assert = chai.assert;
import eventProcessorConfigValidator from './index';

describe('utils/event_processor_config_validator', function() {
describe('validateEventFlushInterval', function() {
Expand Down
Loading