Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: check vehicle_status for valid vehicle_type_id reference #114

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ List of additional rules:
* `NoInvalidReferenceToRegionInStationInformation`
* `NoInvalidReferenceToVehicleTypesInStationStatus`
* `NoMissingVehicleTypesAvailableWhenVehicleTypesExists`
* `NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist`
* `NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist`
* `NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles`
* `NoMissingStoreUriInSystemInformation`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.entur.gbfs.validation.validator.rules;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.json.JSONArray;
import org.json.JSONObject;

Expand All @@ -29,31 +30,33 @@
/**
* Bikes / vehicles must refer to a vehicle type when vehicle_types exists
*/
public class NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist implements CustomRuleSchemaPatcher {
public class NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist implements CustomRuleSchemaPatcher {

private final String fileName;

public NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist(String fileName) {
public NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist(String fileName) {
this.fileName = fileName;
}

public static final String BIKE_ITEMS_REQUIRED = "$.properties.data.properties.bikes.items.required";
public static final String VEHICLE_ITEMS_REQUIRED = "$.properties.data.properties.vehicles.items.required";
private static final String BIKE_ITEMS_SCHEMA_PATH = "$.properties.data.properties.bikes.items";
private static final String VEHICLE_ITEMS_SCHEMA_PATH = "$.properties.data.properties.vehicles.items";
@Override
public DocumentContext addRule(DocumentContext rawSchemaDocumentContext, Map<String, JSONObject> feeds) {
JSONObject vehicleTypesFeed = feeds.get("vehicle_types");

String requiredPath = VEHICLE_ITEMS_REQUIRED;
String requiredPath = VEHICLE_ITEMS_SCHEMA_PATH;

// backwards compatibility
if (fileName.equals("free_bike_status")) {
requiredPath = BIKE_ITEMS_REQUIRED;
requiredPath = BIKE_ITEMS_SCHEMA_PATH;
}

JSONArray vehicleItemsRequiredSchema = rawSchemaDocumentContext.read(requiredPath);
JSONObject vehicleItemsSchema = rawSchemaDocumentContext.read(requiredPath);
if (vehicleTypesFeed != null) {
vehicleItemsRequiredSchema.put("vehicle_type_id");
vehicleItemsSchema.append("required", "vehicle_type_id");
JSONArray vehicleTypeIds = JsonPath.parse(vehicleTypesFeed).read("$.data.vehicle_types[*].vehicle_type_id");
vehicleItemsSchema.getJSONObject( "properties").getJSONObject("vehicle_type_id").put("enum", vehicleTypeIds);
}
return rawSchemaDocumentContext.set(requiredPath, vehicleItemsRequiredSchema);
return rawSchemaDocumentContext.set(requiredPath, vehicleItemsSchema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.CustomRuleSchemaPatcher;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypesAvailableWhenVehicleTypesExists;

Expand Down Expand Up @@ -55,7 +55,7 @@ public class Version21 extends AbstractVersion {
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists()
),
"free_bike_status", List.of(
new NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("free_bike_status"),
new NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("free_bike_status"),
new NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles("free_bike_status")
),
"system_information", List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypesAvailableWhenVehicleTypesExists;

import java.util.Arrays;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class Version22 extends AbstractVersion {
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists()
),
"free_bike_status", List.of(
new NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("free_bike_status"),
new NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("free_bike_status"),
new NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles("free_bike_status"),
new NoInvalidReferenceToPricingPlansInVehicleStatus("free_bike_status")
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypesAvailableWhenVehicleTypesExists;

import java.util.Arrays;
Expand Down Expand Up @@ -60,7 +60,7 @@ public class Version23 extends AbstractVersion {
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists()
),
"free_bike_status", List.of(
new NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("free_bike_status"),
new NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("free_bike_status"),
new NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles("free_bike_status"),
new NoInvalidReferenceToPricingPlansInVehicleStatus("free_bike_status")
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
import org.entur.gbfs.validation.validator.rules.NoMissingVehicleTypesAvailableWhenVehicleTypesExists;

import java.util.Arrays;
Expand Down Expand Up @@ -59,7 +59,7 @@ public class Version30 extends AbstractVersion {
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists()
),
"vehicle_status", List.of(
new NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("vehicle_status"),
new NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist("vehicle_status"),
new NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles("vehicle_status"),
new NoInvalidReferenceToPricingPlansInVehicleStatus("vehicle_status")
),
Expand Down
Loading