@@ -20,68 +20,67 @@ import {
20
20
DATAFILE_VERSIONS ,
21
21
} from '../enums' ;
22
22
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 ] ;
25
25
26
26
/**
27
27
* 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
33
33
* @throws If any of the config options are not valid
34
34
*/
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 ;
46
47
}
47
-
48
- return true ;
49
- } ;
48
+ throw new Error ( sprintf ( ERROR_MESSAGES . INVALID_CONFIG , MODULE_NAME ) ) ;
49
+ }
50
50
51
51
/**
52
52
* Validates the datafile
53
53
* @param {string } datafile
54
- * @return {Boolean } True if the datafile is valid
54
+ * @return {boolean } true if the datafile is valid
55
55
* @throws If the datafile is not valid for any of the following reasons:
56
56
- The datafile string is undefined
57
57
- The datafile string cannot be parsed as a JSON object
58
58
- The datafile version is not supported
59
59
*/
60
- export var validateDatafile = function ( datafile ) {
60
+ export const validateDatafile = function ( datafile : unknown ) : boolean {
61
61
if ( ! datafile ) {
62
62
throw new Error ( sprintf ( ERROR_MESSAGES . NO_DATAFILE_SPECIFIED , MODULE_NAME ) ) ;
63
63
}
64
-
65
- if ( typeof datafile === 'string' || datafile instanceof String ) {
64
+ if ( typeof datafile === 'string' ) {
66
65
// Attempt to parse the datafile string
67
66
try {
68
67
datafile = JSON . parse ( datafile ) ;
69
68
} catch ( ex ) {
70
69
throw new Error ( sprintf ( ERROR_MESSAGES . INVALID_DATAFILE_MALFORMED , MODULE_NAME ) ) ;
71
70
}
72
71
}
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
+ }
76
76
}
77
-
78
77
return true ;
79
- } ;
78
+ }
80
79
81
80
/**
82
81
* Provides utility methods for validating that the configuration options are valid
83
82
*/
84
83
export default {
85
84
validate : validate ,
86
85
validateDatafile : validateDatafile ,
87
- } ;
86
+ }
0 commit comments