Add validation to importers to fix invalid/empty columns #222

This commit is contained in:
Nabeel Shahzad
2018-03-30 17:27:29 -05:00
parent bd30b1f900
commit 63544088cd
21 changed files with 124 additions and 68 deletions

View File

@@ -53,7 +53,7 @@ class ExportService extends Service
$writer = $this->openCsv($path);
// Write out the header first
$writer->insertOne($exporter->getColumns());
$writer->insertOne(array_keys($exporter->getColumns()));
// Write the rest of the rows
foreach ($collection as $row) {

View File

@@ -21,7 +21,7 @@ class AircraftExporter extends ImportExport
*/
public function __construct()
{
self::$columns = AircraftImporter::$columns;
self::$columns = array_keys(AircraftImporter::$columns);
}
/**

View File

@@ -22,11 +22,11 @@ class AircraftImporter extends ImportExport
* Should match the database fields, for the most part
*/
public static $columns = [
'subfleet',
'name',
'registration',
'hex_code',
'status',
'subfleet' => 'required',
'name' => 'required',
'registration' => 'required',
'hex_code' => 'nullable',
'status' => 'nullable',
];
/**

View File

@@ -19,7 +19,7 @@ class AirportExporter extends ImportExport
*/
public function __construct()
{
self::$columns = AirportImporter::$columns;
self::$columns = array_keys(AirportImporter::$columns);
}
/**

View File

@@ -18,15 +18,15 @@ class AirportImporter extends ImportExport
* Should match the database fields, for the most part
*/
public static $columns = [
'icao',
'iata',
'name',
'location',
'country',
'timezone',
'hub',
'lat',
'lon',
'icao' => 'required',
'iata' => 'required',
'name' => 'required',
'location' => 'nullable',
'country' => 'nullable',
'timezone' => 'nullable',
'hub' => 'nullable|boolean',
'lat' => 'required|numeric',
'lon' => 'required|numeric',
];
/**

View File

@@ -22,7 +22,7 @@ class ExpenseExporter extends ImportExport
*/
public function __construct()
{
self::$columns = ExpenseImporter::$columns;
self::$columns = array_keys(ExpenseImporter::$columns);
}
/**

View File

@@ -23,15 +23,15 @@ class ExpenseImporter extends ImportExport
* Should match the database fields, for the most part
*/
public static $columns = [
'airline',
'name',
'amount',
'type',
'charge_to_user',
'multiplier',
'active',
'ref_class',
'ref_class_id',
'airline' => 'nullable',
'name' => 'required',
'amount' => 'required|numeric',
'type' => 'required',
'charge_to_user' => 'nullable|boolean',
'multiplier' => 'nullable|numeric',
'active' => 'nullable|boolean',
'ref_class' => 'nullable',
'ref_class_id' => 'nullable',
];
/**

View File

@@ -19,7 +19,7 @@ class FareExporter extends ImportExport
*/
public function __construct()
{
self::$columns = FareImporter::$columns;
self::$columns = array_keys(FareImporter::$columns);
}
/**

View File

@@ -18,13 +18,13 @@ class FareImporter extends ImportExport
* Should match the database fields, for the most part
*/
public static $columns = [
'code',
'name',
'price',
'cost',
'capacity',
'notes',
'active',
'code' => 'required',
'name' => 'required',
'price' => 'nullable|numeric',
'cost' => 'nullable|numeric',
'capacity' => 'required|integer',
'notes' => 'nullable',
'active' => 'nullable|boolean',
];
/**

View File

@@ -21,7 +21,7 @@ class FlightExporter extends ImportExport
*/
public function __construct()
{
self::$columns = FlightImporter::$columns;
self::$columns = array_keys(FlightImporter::$columns);
}
/**

View File

@@ -27,26 +27,26 @@ class FlightImporter extends ImportExport
* Should match the database fields, for the most part
*/
public static $columns = [
'airline',
'flight_number',
'route_code',
'route_leg',
'dpt_airport',
'arr_airport',
'alt_airport',
'days',
'dpt_time',
'arr_time',
'level',
'distance',
'flight_time',
'flight_type',
'route',
'notes',
'active',
'subfleets',
'fares',
'fields',
'airline' => 'required',
'flight_number' => 'required',
'route_code' => 'nullable',
'route_leg' => 'nullable',
'dpt_airport' => 'required',
'arr_airport' => 'required',
'alt_airport' => 'nullable',
'days' => 'nullable',
'dpt_time' => 'nullable',
'arr_time' => 'nullable',
'level' => 'nullable|integer',
'distance' => 'required|numeric',
'flight_time' => 'required|integer',
'flight_type' => 'required|alpha',
'route' => 'nullable',
'notes' => 'nullable',
'active' => 'nullable|boolean',
'subfleets' => 'nullable',
'fares' => 'nullable',
'fields' => 'nullable',
];
/**

View File

@@ -21,7 +21,7 @@ class SubfleetExporter extends ImportExport
*/
public function __construct()
{
self::$columns = SubfleetImporter::$columns;
self::$columns = array_keys(SubfleetImporter::$columns);
}
/**

View File

@@ -20,10 +20,10 @@ class SubfleetImporter extends ImportExport
* Should match the database fields, for the most part
*/
public static $columns = [
'airline',
'type',
'name',
'fares',
'airline' => 'required',
'type' => 'required',
'name' => 'required',
'fares' => 'nullable',
];
private $fareSvc;

View File

@@ -83,7 +83,7 @@ class ImportService extends Service
{
$reader = $this->openCsv($file_path);
$cols = $importer->getColumns();
$cols = array_keys($importer->getColumns());
$first_header = $cols[0];
$first = true;
@@ -111,6 +111,14 @@ class ImportService extends Service
return trim($val);
})->toArray();
# Try to validate
$validator = Validator::make($row, $importer->getColumns());
if($validator->fails()) {
$errors = 'Error in row '.$offset.','.implode(';', $validator->errors()->all());
$importer->errorLog($errors);
continue;
}
$importer->import($row, $offset);
}