Skip to content

Commit 335349f

Browse files
authored
Merge pull request #81 from entur/cleanup
Cleanup
2 parents 629883c + 8294e09 commit 335349f

16 files changed

+291
-260
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,47 @@ Validate GBFS feeds. Intended as Java native alternative to https://github.com/M
66

77
Uses the official json schema to validate files.
88

9+
## Usage
10+
11+
Create an instance of `GbfsValidator`:
12+
13+
GbfsValidator gbfsValidator = GbfsValidatorFactory.getGbfsJsonValidator();
14+
15+
The `GbfsValidator` interface has two methods:
16+
17+
### Validate a set of GBFS Files
18+
19+
Validate a set of GBFS files by providing a map of InputStreams, keyed by filename.
20+
The input streams maybe come from an HTTP response or from files. This validation
21+
method will apply custom rules (see below), by dynamically patching the static JSON
22+
schemas using data from the files themselves.
23+
24+
gbfsValidator.validate(
25+
Map.of(
26+
"gbfs", gbfsInputStream,
27+
"system_information", systemInformationInputStream
28+
...
29+
)
30+
);
31+
32+
33+
### Validate a single GBFS file
34+
35+
Validate a single GBFS file by providing a filename and an InputStream. This validation
36+
method will not apply any custom rules, but will validate only using the static JSON
37+
schemas.
38+
39+
40+
gbfsValidator.validate(
41+
"system_information", systemInformationInputStream
42+
);
43+
44+
### Using the validation results
45+
46+
The validation methods above will return the `ValidationResult` record. This will contain a summary of the
47+
validation process, as well as a map of validation results per file. See javadocs in `model` for details.
48+
49+
950
## Additional validation rules
1051

1152
The interface `CustomRuleSchemaPatcher` enables adding additional rules dynamically by schema patching:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org.entur.gbfs</groupId>
66
<artifactId>gbfs-validator-java</artifactId>
7-
<version>1.1.4-SNAPSHOT</version>
7+
<version>2.0.0-SNAPSHOT</version>
88

99
<name>gbfs-validator-java</name>
1010
<description>Validate GBFS feeds</description>

src/main/java/org/entur/gbfs/validation/GbfsValidator.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,25 @@
2424
import java.io.InputStream;
2525
import java.util.Map;
2626

27+
/**
28+
* Represents a validator of GBFS files
29+
*/
2730
public interface GbfsValidator {
31+
32+
/**
33+
* Validate all files in the map of GBFS files, keyed by the name of the file. Will validate using
34+
* custom rules in addition to the static schema
35+
* @param fileMap
36+
* @return
37+
*/
2838
ValidationResult validate(Map<String, InputStream> fileMap);
39+
40+
/**
41+
* Validate the GBFS file with the given name and the file itself as an InputStream. Will not apply
42+
* custom rules, but only validate using the static schema
43+
* @param fileName
44+
* @param file
45+
* @return
46+
*/
2947
FileValidationResult validateFile(String fileName, InputStream file);
3048
}

src/main/java/org/entur/gbfs/validation/GbfsValidatorFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020

2121
import org.entur.gbfs.validation.validator.GbfsJsonValidator;
2222

23+
/**
24+
* Main library entrypoint
25+
*/
2326
public class GbfsValidatorFactory {
2427
private GbfsValidatorFactory() {}
2528

29+
/**
30+
* Get a GbfsValidator instance
31+
*/
2632
public static GbfsValidator getGbfsJsonValidator() {
2733
return new GbfsJsonValidator();
2834
}

src/main/java/org/entur/gbfs/validation/model/FileValidationError.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,17 @@
2020

2121
import java.util.Objects;
2222

23-
public class FileValidationError implements ValidationResultComponentIdentity<FileValidationError> {
24-
private String schemaPath;
25-
private String violationPath;
26-
private String message;
27-
28-
public String getSchemaPath() {
29-
return schemaPath;
30-
}
31-
32-
public void setSchemaPath(String schemaPath) {
33-
this.schemaPath = schemaPath;
34-
}
35-
36-
public String getViolationPath() {
37-
return violationPath;
38-
}
39-
40-
public void setViolationPath(String violationPath) {
41-
this.violationPath = violationPath;
42-
}
43-
44-
public String getMessage() {
45-
return message;
46-
}
47-
48-
public void setMessage(String message) {
49-
this.message = message;
50-
}
23+
/**
24+
* Representing a single validation error in a GBFS file
25+
* @param schemaPath The path in the schema that was violated
26+
* @param violationPath The path in the file containing the error
27+
* @param message An error message
28+
*/
29+
public record FileValidationError(
30+
String schemaPath,
31+
String violationPath,
32+
String message
33+
) implements ValidationResultComponentIdentity<FileValidationError> {
5134

5235
@Override
5336
public String toString() {

src/main/java/org/entur/gbfs/validation/model/FileValidationResult.java

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,84 +18,31 @@
1818

1919
package org.entur.gbfs.validation.model;
2020

21-
import java.util.Collections;
2221
import java.util.List;
2322
import java.util.Objects;
2423
import java.util.stream.IntStream;
2524

26-
public class FileValidationResult implements ValidationResultComponentIdentity<FileValidationResult> {
27-
private String file;
28-
private boolean required;
29-
private boolean exists;
30-
private int errorsCount;
31-
private String schema;
32-
private String fileContents;
33-
private String version;
34-
private List<FileValidationError> errors = Collections.emptyList();
35-
36-
public String getFile() {
37-
return file;
38-
}
39-
40-
public void setFile(String file) {
41-
this.file = file;
42-
}
43-
44-
public boolean isRequired() {
45-
return required;
46-
}
47-
48-
public void setRequired(boolean required) {
49-
this.required = required;
50-
}
51-
52-
public boolean isExists() {
53-
return exists;
54-
}
55-
56-
public void setExists(boolean exists) {
57-
this.exists = exists;
58-
}
59-
60-
public int getErrorsCount() {
61-
return errorsCount;
62-
}
63-
64-
public void setErrorsCount(int errorsCount) {
65-
this.errorsCount = errorsCount;
66-
}
67-
68-
public String getSchema() {
69-
return schema;
70-
}
71-
72-
public void setSchema(String schema) {
73-
this.schema = schema;
74-
}
75-
76-
public String getFileContents() {
77-
return fileContents;
78-
}
79-
80-
public void setFileContents(String fileContents) {
81-
this.fileContents = fileContents;
82-
}
83-
84-
public String getVersion() {
85-
return version;
86-
}
87-
88-
public void setVersion(String version) {
89-
this.version = version;
90-
}
91-
92-
public List<FileValidationError> getErrors() {
93-
return errors;
94-
}
95-
96-
public void setErrors(List<FileValidationError> errors) {
97-
this.errors = errors;
98-
}
25+
/**
26+
* The result of validating a single GBFS file
27+
* @param file The name of the file that was validated
28+
* @param required Whether the file is required in the given version of GBFS
29+
* @param exists Whether the file existed in the validation input
30+
* @param errorsCount The number of errors found while validating the file
31+
* @param schema The schema used to validate the file
32+
* @param fileContents The contents of the file
33+
* @param version The version of the file
34+
* @param errors A list of errors encountered while validating the file
35+
*/
36+
public record FileValidationResult(
37+
String file,
38+
boolean required,
39+
boolean exists,
40+
int errorsCount,
41+
String schema,
42+
String fileContents,
43+
String version,
44+
List<FileValidationError> errors
45+
) implements ValidationResultComponentIdentity<FileValidationResult> {
9946

10047
@Override
10148
public String toString() {

src/main/java/org/entur/gbfs/validation/model/ValidationResult.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,17 @@
1818

1919
package org.entur.gbfs.validation.model;
2020

21-
import java.util.HashMap;
2221
import java.util.Map;
2322

24-
public class ValidationResult implements ValidationResultComponentIdentity<ValidationResult> {
25-
private ValidationSummary summary = new ValidationSummary();
26-
private Map<String, FileValidationResult> files = new HashMap<>();
27-
28-
public ValidationSummary getSummary() {
29-
return summary;
30-
}
31-
32-
public void setSummary(ValidationSummary summary) {
33-
this.summary = summary;
34-
}
35-
36-
public Map<String, FileValidationResult> getFiles() {
37-
return files;
38-
}
39-
40-
public void setFiles(Map<String, FileValidationResult> files) {
41-
this.files = files;
42-
}
43-
23+
/**
24+
* This record contains the result of validating a set of GBFS files
25+
* @param summary A summary of the validation
26+
* @param files A map of files that were validated
27+
*/
28+
public record ValidationResult(
29+
ValidationSummary summary,
30+
Map<String, FileValidationResult> files
31+
) implements ValidationResultComponentIdentity<ValidationResult> {
4432
@Override
4533
public String toString() {
4634
return "ValidationResult{" +
@@ -52,7 +40,7 @@ public String toString() {
5240
@Override
5341
public boolean sameAs(ValidationResult other) {
5442
if (other == null) return false;
55-
if (!summary.sameAs(other.getSummary())) return false;
43+
if (!summary.sameAs(other.summary())) return false;
5644
return files.entrySet().stream().allMatch(entry -> other.files.get(entry.getKey()).sameAs(entry.getValue()));
5745
}
5846
}

src/main/java/org/entur/gbfs/validation/model/ValidationSummary.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,17 @@
2020

2121
import java.util.Objects;
2222

23-
public class ValidationSummary implements ValidationResultComponentIdentity<ValidationSummary> {
24-
private String version;
25-
private long timestamp;
26-
private int errorsCount;
27-
28-
public String getVersion() {
29-
return version;
30-
}
31-
32-
public void setVersion(String version) {
33-
this.version = version;
34-
}
35-
36-
public long getTimestamp() {
37-
return timestamp;
38-
}
39-
40-
public void setTimestamp(long timestamp) {
41-
this.timestamp = timestamp;
42-
}
43-
44-
public int getErrorsCount() {
45-
return errorsCount;
46-
}
47-
48-
public void setErrorsCount(int errorsCount) {
49-
this.errorsCount = errorsCount;
50-
}
23+
/**
24+
* A summary of the validation of a set of GBFS files
25+
* @param version The version the files were validated against
26+
* @param timestamp The time when validation was performed
27+
* @param errorsCount The total amount of errors encountered during validation
28+
*/
29+
public record ValidationSummary(
30+
String version,
31+
long timestamp,
32+
int errorsCount
33+
) implements ValidationResultComponentIdentity<ValidationSummary> {
5134

5235
@Override
5336
public String toString() {

0 commit comments

Comments
 (0)