From 1fd858355ce815e309fe902d4e8df1c03d7bfc26 Mon Sep 17 00:00:00 2001 From: Holger Bruch Date: Wed, 18 Dec 2024 13:32:39 +0100 Subject: [PATCH 1/3] feature: new rule to check that region_id in station_information exists in system_regions --- ...ReferenceToRegionInStationInformation.java | 65 +++++++++++++++++++ .../validator/versions/Version21.java | 4 ++ .../validator/versions/Version22.java | 4 ++ .../validator/versions/Version23.java | 4 ++ .../validator/versions/Version30.java | 4 ++ 5 files changed, 81 insertions(+) create mode 100644 src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java diff --git a/src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java b/src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java new file mode 100644 index 0000000..f91b893 --- /dev/null +++ b/src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java @@ -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 feeds + ) { + JSONObject systemRegionsFeed = feeds.get("system_regions"); + JSONObject regionIdSchema = rawSchemaDocumentContext.read( + REGION_IDS_SCHEMA_PATH + ); + + if (systemRegionsFeed != null) { + JSONArray vehicleTypeIds = JsonPath + .parse(systemRegionsFeed) + .read("$.data.regions[*].region_id"); + regionIdSchema.put("enum", vehicleTypeIds); + } + + return rawSchemaDocumentContext + .set( + REGION_IDS_SCHEMA_PATH, + regionIdSchema + ); + } +} diff --git a/src/main/java/org/entur/gbfs/validation/validator/versions/Version21.java b/src/main/java/org/entur/gbfs/validation/validator/versions/Version21.java index e815efd..777a77d 100644 --- a/src/main/java/org/entur/gbfs/validation/validator/versions/Version21.java +++ b/src/main/java/org/entur/gbfs/validation/validator/versions/Version21.java @@ -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; @@ -59,6 +60,9 @@ public class Version21 extends AbstractVersion { ), "system_information", List.of( new NoMissingStoreUriInSystemInformation("free_bike_status") + ), + "station_information", List.of( + new NoInvalidReferenceToRegionInStationInformation() ) ); diff --git a/src/main/java/org/entur/gbfs/validation/validator/versions/Version22.java b/src/main/java/org/entur/gbfs/validation/validator/versions/Version22.java index 0d60d8b..f53c459 100644 --- a/src/main/java/org/entur/gbfs/validation/validator/versions/Version22.java +++ b/src/main/java/org/entur/gbfs/validation/validator/versions/Version22.java @@ -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; @@ -61,6 +62,9 @@ public class Version22 extends AbstractVersion { ), "system_information", List.of( new NoMissingStoreUriInSystemInformation("free_bike_status") + ), + "station_information", List.of( + new NoInvalidReferenceToRegionInStationInformation() ) ); diff --git a/src/main/java/org/entur/gbfs/validation/validator/versions/Version23.java b/src/main/java/org/entur/gbfs/validation/validator/versions/Version23.java index 2a83198..9a2ac74 100644 --- a/src/main/java/org/entur/gbfs/validation/validator/versions/Version23.java +++ b/src/main/java/org/entur/gbfs/validation/validator/versions/Version23.java @@ -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; @@ -65,6 +66,9 @@ public class Version23 extends AbstractVersion { ), "system_information", List.of( new NoMissingStoreUriInSystemInformation("free_bike_status") + ), + "station_information", List.of( + new NoInvalidReferenceToRegionInStationInformation() ) ); diff --git a/src/main/java/org/entur/gbfs/validation/validator/versions/Version30.java b/src/main/java/org/entur/gbfs/validation/validator/versions/Version30.java index 4a56e57..72531ad 100644 --- a/src/main/java/org/entur/gbfs/validation/validator/versions/Version30.java +++ b/src/main/java/org/entur/gbfs/validation/validator/versions/Version30.java @@ -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; @@ -64,6 +65,9 @@ public class Version30 extends AbstractVersion { ), "system_information", List.of( new NoMissingStoreUriInSystemInformation("vehicle_status") + ), + "station_information", List.of( + new NoInvalidReferenceToRegionInStationInformation() ) ); From 4a5a3e679f847199dcd282df4eb1e0a21c58f9bd Mon Sep 17 00:00:00 2001 From: Holger Bruch Date: Wed, 18 Dec 2024 16:26:17 +0100 Subject: [PATCH 2/3] fix copy-paste error --- .../rules/NoInvalidReferenceToRegionInStationInformation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java b/src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java index f91b893..91926fd 100644 --- a/src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java +++ b/src/main/java/org/entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java @@ -50,10 +50,10 @@ public DocumentContext addRule( ); if (systemRegionsFeed != null) { - JSONArray vehicleTypeIds = JsonPath + JSONArray regionIds = JsonPath .parse(systemRegionsFeed) .read("$.data.regions[*].region_id"); - regionIdSchema.put("enum", vehicleTypeIds); + regionIdSchema.put("enum", regionIds); } return rawSchemaDocumentContext From b04577d761220f5381fdeda4693d4f7329e79015 Mon Sep 17 00:00:00 2001 From: Holger Bruch Date: Fri, 20 Dec 2024 10:27:02 +0100 Subject: [PATCH 3/3] Add missing additional rules to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9a00494..6850e55 100644 --- a/README.md +++ b/README.md @@ -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`