@@ -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 */
8483export default {
8584 validate : validate ,
8685 validateDatafile : validateDatafile ,
87- } ;
86+ }
0 commit comments