@@ -26,6 +26,7 @@ var projectConfigSchema = require('./project_config_schema');
2626var sprintf = require ( 'sprintf-js' ) . sprintf ;
2727var userProfileServiceValidator = require ( '../utils/user_profile_service_validator' ) ;
2828var stringValidator = require ( '../utils/string_value_validator' ) ;
29+ var configValidator = require ( '../utils/config_validator' ) ;
2930
3031var ERROR_MESSAGES = enums . ERROR_MESSAGES ;
3132var LOG_LEVEL = enums . LOG_LEVEL ;
@@ -47,74 +48,62 @@ var FEATURE_VARIABLE_TYPES = enums.FEATURE_VARIABLE_TYPES;
4748 * @param {Object } config.userProfileService
4849 */
4950function Optimizely ( config ) {
50- var clientEngine = config . clientEngine ;
51- if ( clientEngine !== enums . NODE_CLIENT_ENGINE && clientEngine !== enums . JAVASCRIPT_CLIENT_ENGINE ) {
52- config . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . INVALID_CLIENT_ENGINE , MODULE_NAME , clientEngine ) ) ;
53- clientEngine = enums . NODE_CLIENT_ENGINE ;
54- }
51+ var clientEngine = config . clientEngine ;
52+ if ( clientEngine !== enums . NODE_CLIENT_ENGINE && clientEngine !== enums . JAVASCRIPT_CLIENT_ENGINE ) {
53+ config . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . INVALID_CLIENT_ENGINE , MODULE_NAME , clientEngine ) ) ;
54+ clientEngine = enums . NODE_CLIENT_ENGINE ;
55+ }
5556
56- this . clientEngine = clientEngine ;
57- this . clientVersion = config . clientVersion || enums . NODE_CLIENT_VERSION ;
58- this . errorHandler = config . errorHandler ;
59- this . eventDispatcher = config . eventDispatcher ;
60- this . isValidInstance = config . isValidInstance ;
61- this . logger = config . logger ;
62-
63- if ( ! config . datafile ) {
64- this . logger . log ( LOG_LEVEL . ERROR , sprintf ( ERROR_MESSAGES . NO_DATAFILE_SPECIFIED , MODULE_NAME ) ) ;
65- this . errorHandler . handleError ( new Error ( sprintf ( ERROR_MESSAGES . NO_DATAFILE_SPECIFIED , MODULE_NAME ) ) ) ;
66- this . isValidInstance = false ;
67- } else {
68- if ( typeof config . datafile === 'string' || config . datafile instanceof String ) {
69- // Attempt to parse the datafile string
70- try {
71- config . datafile = JSON . parse ( config . datafile ) ;
72- } catch ( ex ) {
73- this . isValidInstance = false ;
74- this . logger . log ( LOG_LEVEL . ERROR , sprintf ( ERROR_MESSAGES . INVALID_DATAFILE_MALFORMED , MODULE_NAME ) ) ;
75- return ;
76- }
77- }
57+ this . clientEngine = clientEngine ;
58+ this . clientVersion = config . clientVersion || enums . NODE_CLIENT_VERSION ;
59+ this . errorHandler = config . errorHandler ;
60+ this . eventDispatcher = config . eventDispatcher ;
61+ this . isValidInstance = config . isValidInstance ;
62+ this . logger = config . logger ;
7863
79- if ( config . skipJSONValidation === true ) {
64+ try {
65+ configValidator . validateDatafile ( config . datafile ) ;
66+ if ( typeof config . datafile === 'string' || config . datafile instanceof String ) {
67+ config . datafile = JSON . parse ( config . datafile ) ;
68+ }
69+
70+ if ( config . skipJSONValidation === true ) {
71+ this . configObj = projectConfig . createProjectConfig ( config . datafile ) ;
72+ this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . SKIPPING_JSON_VALIDATION , MODULE_NAME ) ) ;
73+ } else {
74+ if ( config . jsonSchemaValidator . validate ( projectConfigSchema , config . datafile ) ) {
8075 this . configObj = projectConfig . createProjectConfig ( config . datafile ) ;
81- this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . SKIPPING_JSON_VALIDATION , MODULE_NAME ) ) ;
82- } else {
83- try {
84- if ( config . jsonSchemaValidator . validate ( projectConfigSchema , config . datafile ) ) {
85- this . configObj = projectConfig . createProjectConfig ( config . datafile ) ;
86- this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . VALID_DATAFILE , MODULE_NAME ) ) ;
87- }
88- } catch ( ex ) {
89- this . isValidInstance = false ;
90- this . logger . log ( LOG_LEVEL . ERROR , ex . message ) ;
91- this . errorHandler . handleError ( ex ) ;
92- }
76+ this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . VALID_DATAFILE , MODULE_NAME ) ) ;
9377 }
78+ }
79+ } catch ( ex ) {
80+ this . isValidInstance = false ;
81+ this . logger . log ( LOG_LEVEL . ERROR , ex . message ) ;
82+ this . errorHandler . handleError ( ex ) ;
83+ }
9484
95- var userProfileService = null ;
96- if ( config . userProfileService ) {
97- try {
98- if ( userProfileServiceValidator . validate ( config . userProfileService ) ) {
99- userProfileService = config . userProfileService ;
100- this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . VALID_USER_PROFILE_SERVICE , MODULE_NAME ) ) ;
101- }
102- } catch ( ex ) {
103- this . logger . log ( LOG_LEVEL . WARNING , ex . message ) ;
104- }
85+ var userProfileService = null ;
86+ if ( config . userProfileService ) {
87+ try {
88+ if ( userProfileServiceValidator . validate ( config . userProfileService ) ) {
89+ userProfileService = config . userProfileService ;
90+ this . logger . log ( LOG_LEVEL . INFO , sprintf ( LOG_MESSAGES . VALID_USER_PROFILE_SERVICE , MODULE_NAME ) ) ;
10591 }
92+ } catch ( ex ) {
93+ this . logger . log ( LOG_LEVEL . WARNING , ex . message ) ;
94+ }
95+ }
10696
107- this . decisionService = decisionService . createDecisionService ( {
108- configObj : this . configObj ,
109- userProfileService : userProfileService ,
110- logger : this . logger ,
111- } ) ;
97+ this . decisionService = decisionService . createDecisionService ( {
98+ configObj : this . configObj ,
99+ userProfileService : userProfileService ,
100+ logger : this . logger ,
101+ } ) ;
112102
113- this . notificationCenter = notificationCenter . createNotificationCenter ( {
114- logger : this . logger ,
115- errorHandler : this . errorHandler
116- } ) ;
117- }
103+ this . notificationCenter = notificationCenter . createNotificationCenter ( {
104+ logger : this . logger ,
105+ errorHandler : this . errorHandler
106+ } ) ;
118107}
119108
120109/**
0 commit comments