8000 feat: Convert config_validator to TS (#556) · rserrano-eSW/javascript-sdk@80b4108 · GitHub
[go: up one dir, main page]

Skip to content

Commit 80b4108

Browse files
authored
feat: Convert config_validator to TS (optimizely#556)
* Convert config_validator to TS * Import whole config_validator module * Try to Remove stubbing of configValidate * Change to default export object * Change export functions as const
1 parent d8d9f71 commit 80b4108

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

packages/optimizely-sdk/lib/index.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor';
2525

2626
import { assign } from './utils/fns';
27-
import configValidator from './utils/config_validator';
27+
import * as configValidator from './utils/config_validator';
2828
import defaultErrorHandler from './plugins/error_handler';
2929
import defaultEventDispatcher from './plugins/event_dispatcher/index.browser';
3030
import * as enums from './utils/enums';

packages/optimizely-sdk/lib/utils/config_validator/index.js renamed to packages/optimizely-sdk/lib/utils/config_validator/index.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,68 +20,67 @@ import {
2020
DATAFILE_VERSIONS,
2121
} from '../enums';
2222

23-
var MODULE_NAME = 'CONFIG_VALIDATOR';
24-
var SUPPORTED_VERSIONS = [DATAFILE_VERSIONS.V2, DATAFILE_VERSIONS.V3, DATAFILE_VERSIONS.V4];
23+
const MODULE_NAME = 'CONFIG_VALIDATOR';
24+
const SUPPORTED_VERSIONS = [DATAFILE_VERSIONS.V2, DATAFILE_VERSIONS.V3, DATAFILE_VERSIONS.V4];
2525

2626
/**
2727
* Validates the given config options
28-
* @param {Object} config
29-
* @param {Object} config.errorHandler
30-
* @param {Object} config.eventDispatcher
31-
* @param {Object} config.logger
32-
* @return {Boolean} True if the config options are valid
28+
* @param {unknown} config
29+
* @param {object} config.errorHandler
30+
* @param {object} config.eventDispatcher
31+
* @param {object} config.logger
32+
* @return {boolean} true if the config options are valid
3333
* @throws If any of the config options are not valid
3434
*/
35-
export var validate = function(config) {
36-
if (config.errorHandler && typeof config.errorHandler.handleError !== 'function') {
37-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME));
38-
}
39-
40-
if (config.eventDispatcher && typeof config.eventDispatcher.dispatchEvent !== 'function') {
41-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME));
42-
}
43-
44-
if (config.logger && typeof config.logger.log !== 'function') {
45-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME));
35+
export const validate = function(config: unknown): boolean {
36+
if (typeof config === 'object' && config !== null) {
37+
if (config['errorHandler'] && typeof config['errorHandler'].handleError !== 'function') {
38+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME));
39+
}
40+
if (config['eventDispatcher'] && typeof config['eventDispatcher'].dispatchEvent !== 'function') {
41+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME));
42+
}
43+
if (config['logger'] && typeof config['logger'].log !== 'function') {
44+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME));
45+
}
46+
return true;
4647
}
47-
48-
return true;
49-
};
48+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_CONFIG, MODULE_NAME));
49+
}
5050

5151
/**
5252
* Validates the datafile
5353
* @param {string} datafile
54-
* @return {Boolean} True if the datafile is valid
54+
* @return {boolean} true if the datafile is valid
5555
* @throws If the datafile is not valid for any of the following reasons:
5656
- The datafile string is undefined
5757
- The datafile string cannot be parsed as a JSON object
5858
- The datafile version is not supported
5959
*/
60-
export var validateDatafile = function(datafile) {
60+
export const validateDatafile = function(datafile: unknown): boolean {
6161
if (!datafile) {
6262
throw new Error(sprintf(ERROR_MESSAGES.NO_DATAFILE_SPECIFIED, MODULE_NAME));
6363
}
64-
65-
if (typeof datafile === 'string' || datafile instanceof String) {
64+
if (typeof datafile === 'string') {
6665
// Attempt to parse the datafile string
6766
try {
6867
datafile = JSON.parse(datafile);
6968
} catch (ex) {
7069
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_MALFORMED, MODULE_NAME));
7170
}
7271
}
73-
74-
if (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) {
75-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version));
72+
if (typeof datafile === 'object' && !Array.isArray(datafile) && datafile !== null) {
73+
if (SUPPORTED_VERSIONS.indexOf(datafile['version']) === -1) {
74+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile['version']));
75+
}
7676
}
77-
7877
return true;
79-
};
78+
}
8079

8180
/**
8281
* Provides utility methods for validating that the configuration options are valid
8382
*/
8483
export default {
8584
validate: validate,
8685
validateDatafile: validateDatafile,
87-
};
86+
}

packages/optimizely-sdk/lib/utils/enums/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const ERROR_MESSAGES = {
3737
INVALID_BUCKETING_ID: '%s: Unable to generate hash for bucketing ID %s: %s',
3838
INVALID_DATAFILE: '%s: Datafile is invalid - property %s: %s',
3939
INVALID_DATAFILE_MALFORMED: '%s: Datafile is invalid because it is malformed.',
40+
INVALID_CONFIG: '%s: Provided Optimizely config is in an invalid format.',
4041
INVALID_JSON: '%s: JSON object is not valid.',
4142
INVALID_ERROR_HANDLER: '%s: Provided "errorHandler" is in an invalid format.',
4243
INVALID_EVENT_DISPATCHER: '%s: Provided "eventDispatcher" is in an invalid format.',

0 commit comments

Comments
 (0)
0