-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from mobidata-bw/region_id_rule
feature: new rule to check that region_id in station_information exists
- Loading branch information
Showing
6 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...entur/gbfs/validation/validator/rules/NoInvalidReferenceToRegionInStationInformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters