Example #1
0
 /**
  * Alrighty, now we get fancy! This examines the contents of the `rows` property, which is an array of data type
  * rows. Most Data Type has its own settings schema which we need to validate against. All this function does
  * is return whatever errors occurred; if everything checks out, it returns nothing.
  * @param $json
  * @return array
  */
 private function validateDataTypeSettings($json)
 {
     $dataTypes = DataTypePluginHelper::getDataTypeList(Core::$dataTypePlugins);
     $schemaFiles = DataTypePluginHelper::getSchemaFiles($dataTypes);
     $dataTypeFolders = DataTypePluginHelper::getDataTypeFolders(Core::$dataTypePlugins);
     $rows = $json->rows;
     $numRows = count($rows);
     for ($i = 0; $i < $numRows; $i++) {
         $dataType = $rows[$i]->type;
         // check the Data Type folder name is valid. Note: we just compare against the list of all possible
         // Data Types, not those that have schemas. Not all Data Types have a schema: some, like Email, are simple
         // and have no settings
         if (!in_array($dataType, $dataTypeFolders)) {
             return array("error" => ErrorCodes::API_UNKNOWN_DATA_TYPE, "error_details" => "invalid `type` attribute: `{$dataType}`", "location" => "index {$i} of the `rows` array");
         }
         // check the Data Type has a title
         if (!property_exists($rows[$i], "title")) {
             return array("error" => ErrorCodes::API_MISSING_DATA_TYPE_TITLE, "error_details" => "Missing `title` attribute", "location" => "index {$i} of the `rows` array");
         }
         // if the schema file exists for the Data Type, validate against it
         if (array_key_exists($dataType, $schemaFiles)) {
             $schema = json_decode($schemaFiles[$dataType]);
             $json = property_exists($rows[$i], "settings") ? $rows[$i]->settings : json_decode("{}");
             // verify the settings for this data type
             $result = Jsv4::validate($json, $schema);
             if ($result->valid) {
                 continue;
             }
             // ladies and gentleman, we have an error! Return as much user friendly information to the user
             // to help them locate the problem
             return array("error" => ErrorCodes::API_INVALID_DATA_TYPE_JSON, "error_details" => "Invalid Data Type JSON `settings` content passed", "validation_error" => $result->errors[0]->message, "location" => "index {$i} of the `rows` array", "data_type" => $dataType);
         }
     }
 }