Skip to content

Commit aecc388

Browse files
committed
feat: add reference validation for station_ids
1 parent 04fc84f commit aecc388

File tree

5 files changed

+92
-6
lines changed

5 files changed

+92
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ List of additional rules:
6262
* `NoInvalidReferenceToPricingPlansInVehicleStatus`
6363
* `NoInvalidReferenceToPricingPlansInVehicleTypes`
6464
* `NoInvalidReferenceToRegionInStationInformation`
65+
* `NoInvalidReferenceToStation`
6566
* `NoInvalidReferenceToVehicleTypesInStationStatus`
6667
* `NoMissingVehicleTypesAvailableWhenVehicleTypesExists`
6768
* `NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist`
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
*
3+
* *
4+
* *
5+
* * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
6+
* * * the European Commission - subsequent versions of the EUPL (the "Licence");
7+
* * * You may not use this work except in compliance with the Licence.
8+
* * * You may obtain a copy of the Licence at:
9+
* * *
10+
* * * https://joinup.ec.europa.eu/software/page/eupl
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the Licence is distributed on an "AS IS" basis,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the Licence for the specific language governing permissions and
16+
* * * limitations under the Licence.
17+
* *
18+
*
19+
*/
20+
21+
package org.entur.gbfs.validation.validator.rules;
22+
23+
import com.jayway.jsonpath.DocumentContext;
24+
import com.jayway.jsonpath.JsonPath;
25+
import java.util.Map;
26+
import org.json.JSONArray;
27+
import org.json.JSONObject;
28+
29+
/**
30+
* References to stations in station_information must exist in station_status file and vice versa.
31+
*/
32+
public class NoInvalidReferenceToStation implements CustomRuleSchemaPatcher {
33+
34+
public static final String STATION_IDS_SCHEMA_PATH =
35+
"$.properties.data.properties.stations.items.properties.station_id";
36+
37+
private final String stationReferenceFileName;
38+
39+
public NoInvalidReferenceToStation(String stationReferenceFileName) {
40+
this.stationReferenceFileName = stationReferenceFileName;
41+
}
42+
43+
/**
44+
* Adds an enum to the station_id schema of stations.station_id with the station ids from station_status.json
45+
*/
46+
@Override
47+
public DocumentContext addRule(
48+
DocumentContext rawSchemaDocumentContext,
49+
Map<String, JSONObject> feeds
50+
) {
51+
JSONObject stationReferenceFeed = feeds.get(stationReferenceFileName);
52+
53+
JSONObject stationIdSchema = rawSchemaDocumentContext.read(
54+
STATION_IDS_SCHEMA_PATH
55+
);
56+
57+
JSONArray stationIds = stationReferenceFeed != null
58+
? JsonPath
59+
.parse(stationReferenceFeed)
60+
.read("$.data.stations[*].station_id")
61+
: new JSONArray();
62+
63+
stationIdSchema.put("enum", stationIds);
64+
65+
return rawSchemaDocumentContext.set(
66+
STATION_IDS_SCHEMA_PATH,
67+
stationIdSchema
68+
);
69+
}
70+
}

gbfs-validator-java/src/main/java/org/entur/gbfs/validation/validator/versions/Version22.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.entur.gbfs.validation.validator.rules.CustomRuleSchemaPatcher;
2525
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleStatus;
2626
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToRegionInStationInformation;
27+
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToStation;
2728
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
2829
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
2930
import org.entur.gbfs.validation.validator.rules.NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
@@ -55,7 +56,8 @@ public class Version22 extends AbstractVersion {
5556
"station_status",
5657
List.of(
5758
new NoInvalidReferenceToVehicleTypesInStationStatus(),
58-
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists()
59+
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists(),
60+
new NoInvalidReferenceToStation("station_information")
5961
),
6062
"free_bike_status",
6163
List.of(
@@ -70,7 +72,10 @@ public class Version22 extends AbstractVersion {
7072
"system_information",
7173
List.of(new NoMissingStoreUriInSystemInformation("free_bike_status")),
7274
"station_information",
73-
List.of(new NoInvalidReferenceToRegionInStationInformation())
75+
List.of(
76+
new NoInvalidReferenceToRegionInStationInformation(),
77+
new NoInvalidReferenceToStation("station_status")
78+
)
7479
);
7580

7681
protected Version22() {

gbfs-validator-java/src/main/java/org/entur/gbfs/validation/validator/versions/Version23.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleStatus;
2626
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleTypes;
2727
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToRegionInStationInformation;
28+
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToStation;
2829
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
2930
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
3031
import org.entur.gbfs.validation.validator.rules.NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
@@ -58,7 +59,8 @@ public class Version23 extends AbstractVersion {
5859
"station_status",
5960
List.of(
6061
new NoInvalidReferenceToVehicleTypesInStationStatus(),
61-
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists()
62+
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists(),
63+
new NoInvalidReferenceToStation("station_information")
6264
),
6365
"free_bike_status",
6466
List.of(
@@ -73,7 +75,10 @@ public class Version23 extends AbstractVersion {
7375
"system_information",
7476
List.of(new NoMissingStoreUriInSystemInformation("free_bike_status")),
7577
"station_information",
76-
List.of(new NoInvalidReferenceToRegionInStationInformation())
78+
List.of(
79+
new NoInvalidReferenceToRegionInStationInformation(),
80+
new NoInvalidReferenceToStation("station_status")
81+
)
7782
);
7883

7984
protected Version23() {

gbfs-validator-java/src/main/java/org/entur/gbfs/validation/validator/versions/Version30.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleStatus;
2626
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToPricingPlansInVehicleTypes;
2727
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToRegionInStationInformation;
28+
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToStation;
2829
import org.entur.gbfs.validation.validator.rules.NoInvalidReferenceToVehicleTypesInStationStatus;
2930
import org.entur.gbfs.validation.validator.rules.NoMissingCurrentRangeMetersInVehicleStatusForMotorizedVehicles;
3031
import org.entur.gbfs.validation.validator.rules.NoMissingOrInvalidVehicleTypeIdInVehicleStatusWhenVehicleTypesExist;
@@ -57,7 +58,8 @@ public class Version30 extends AbstractVersion {
5758
"station_status",
5859
List.of(
5960
new NoInvalidReferenceToVehicleTypesInStationStatus(),
60-
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists()
61+
new NoMissingVehicleTypesAvailableWhenVehicleTypesExists(),
62+
new NoInvalidReferenceToStation("station_information")
6163
),
6264
"vehicle_status",
6365
List.of(
@@ -72,7 +74,10 @@ public class Version30 extends AbstractVersion {
7274
"system_information",
7375
List.of(new NoMissingStoreUriInSystemInformation("vehicle_status")),
7476
"station_information",
75-
List.of(new NoInvalidReferenceToRegionInStationInformation())
77+
List.of(
78+
new NoInvalidReferenceToRegionInStationInformation(),
79+
new NoInvalidReferenceToStation("station_status")
80+
)
7681
);
7782

7883
protected Version30() {

0 commit comments

Comments
 (0)