Skip to content

Commit

Permalink
Merge pull request #105 from mobidata-bw/region_id_rule
Browse files Browse the repository at this point in the history
feature: new rule to check that region_id in station_information exists
  • Loading branch information
testower authored Dec 27, 2024
2 parents 44cc9cb + b04577d commit 725cf4f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ The raw schema along with a map of the data feeds is passed to this method. The

List of additional rules:

* `NoInvalidReferenceToPricingPlansInVehicleStatus`
* `NoInvalidReferenceToPricingPlansInVehicleTypes`
* `NoInvalidReferenceToRegionInStationInformation`
* `NoInvalidReferenceToVehicleTypesInStationStatus`
* `NoMissingVehicleTypesAvailableWhenVehicleTypesExists`
* `NoMissingVehicleTypeIdInVehicleStatusWhenVehicleTypesExist`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
*
* *
* *
* * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
* * * the European Commission - subsequent versions of the EUPL (the "Licence");
* * * You may not use this work except in compliance with the Licence.
* * * You may obtain a copy of the Licence at:
* * *
* * * https://joinup.ec.europa.eu/software/page/eupl
* * *
* * * Unless required by applicable law or agreed to in writing, software
* * * distributed under the Licence is distributed on an "AS IS" basis,
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * See the Licence for the specific language governing permissions and
* * * limitations under the Licence.
* *
*
*/

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;

import java.util.Map;

/**
* References to regions in station_information must exist in the system's system_regions file
*/
public class NoInvalidReferenceToRegionInStationInformation
implements CustomRuleSchemaPatcher {

public static final String REGION_IDS_SCHEMA_PATH =
"$.properties.data.properties.stations.items.properties.region_id";

/**
* Adds an enum to the region_id schema of stations.region_id with the region ids from system_regions.json
*/
@Override
public DocumentContext addRule(
DocumentContext rawSchemaDocumentContext,
Map<String, JSONObject> feeds
) {
JSONObject systemRegionsFeed = feeds.get("system_regions");
JSONObject regionIdSchema = rawSchemaDocumentContext.read(
REGION_IDS_SCHEMA_PATH
);

if (systemRegionsFeed != null) {
JSONArray regionIds = JsonPath
.parse(systemRegionsFeed)
.read("$.data.regions[*].region_id");
regionIdSchema.put("enum", regionIds);
}

return rawSchemaDocumentContext
.set(
REGION_IDS_SCHEMA_PATH,
regionIdSchema
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.entur.gbfs.validation.validator.versions;

import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToRegionInStationInformation;
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.CustomRuleSchemaPatcher;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
Expand Down Expand Up @@ -59,6 +60,9 @@ public class Version21 extends AbstractVersion {
),
"system_information", List.of(
new NoMissingStoreUriInSystemInformation("free_bike_status")
),
"station_information", List.of(
new NoInvalidReferenceToRegionInStationInformation()
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.entur.gbfs.validation.validator.rules.CustomRuleSchemaPatcher;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleStatus;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToRegionInStationInformation;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
Expand Down Expand Up @@ -61,6 +62,9 @@ public class Version22 extends AbstractVersion {
),
"system_information", List.of(
new NoMissingStoreUriInSystemInformation("free_bike_status")
),
"station_information", List.of(
new NoInvalidReferenceToRegionInStationInformation()
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.entur.gbfs.validation.validator.rules.CustomRuleSchemaPatcher;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleStatus;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleTypes;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToRegionInStationInformation;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class Version23 extends AbstractVersion {
),
"system_information", List.of(
new NoMissingStoreUriInSystemInformation("free_bike_status")
),
"station_information", List.of(
new NoInvalidReferenceToRegionInStationInformation()
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.entur.gbfs.validation.validator.rules.CustomRuleSchemaPatcher;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleStatus;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleTypes;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToRegionInStationInformation;
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
import org.entur.gbfs.validation.validator.rules.NoMissingStoreUriInSystemInformation;
Expand Down Expand Up @@ -64,6 +65,9 @@ public class Version30 extends AbstractVersion {
),
"system_information", List.of(
new NoMissingStoreUriInSystemInformation("vehicle_status")
),
"station_information", List.of(
new NoInvalidReferenceToRegionInStationInformation()
)
);

Expand Down

0 comments on commit 725cf4f

Please sign in to comment.