packages/optimizely-sdk/lib/utils/config_validator/index.js
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 (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) {
      throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version));
    }

    return true;
  }
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 (SUPPORTED_VERSIONS.indexOf(datafile.version) === -1) {
      throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile.version));
    }

    return true;
  }
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 ( SUPPORTED_VERSIONS . indexOf ( datafile . version ) === - 1 ) { throw new Error ( sprintf ( ERROR_MESSAGES . INVALID_DATAFILE_VERSION , MODULE_NAME , datafile . version ) ) ; } 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
Validates the datafile