Skip to content

Commit

Permalink
Improved Schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesJander committed Dec 8, 2022
1 parent cc37f86 commit 1974f6a
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,34 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class ValidationException extends TableSchemaException {

List<ValidationMessage> validationMessages = new ArrayList<>();

public ValidationException(String msg) {
super(msg);
}

public ValidationException(Exception ex) {
super(ex);
String message = ex.getClass()+": "+ex.getMessage();
}

public ValidationException(JsonSchema schema, Collection<ValidationMessage> message) {
this(String.format("%s: %s", schema, "validation failed"));
this.validationMessages.addAll(validationMessages);
public ValidationException(JsonSchema schema, Collection<ValidationMessage> messages) {
this(String.format("%s: %s", schema.getName(), "validation failed"));
this.validationMessages.addAll(messages);
}

public ValidationException(String name, Collection<ValidationException> errors) {
super(String.format("%s: %s", name, "validation failed: "+errors.stream().map(Throwable::getMessage).collect(Collectors.joining("\n"))));
errors.forEach((ex) -> {
this.validationMessages.addAll(ex.validationMessages);
});

}

public List<ValidationMessage> getValidationMessages() {
return validationMessages;
}
}
12 changes: 6 additions & 6 deletions src/main/java/io/frictionlessdata/tableschema/fk/ForeignKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ public ForeignKey(String json, boolean strict) throws ForeignKeyException{

if(fkJsonObject.has(JSON_KEY_FIELDS)){
if(fkJsonObject.get(JSON_KEY_FIELDS).isArray()) {
this.fields = fkJsonObject.get(JSON_KEY_FIELDS);
fields = fkJsonObject.get(JSON_KEY_FIELDS);
} else {
this.fields = fkJsonObject.get(JSON_KEY_FIELDS).asText();
fields = fkJsonObject.get(JSON_KEY_FIELDS).asText();
}
}

if(fkJsonObject.has(JSON_KEY_REFERENCE)){
JsonNode refJsonObject = fkJsonObject.get(JSON_KEY_REFERENCE);
this.reference = new Reference(refJsonObject.toString(), strict);
reference = new Reference(refJsonObject.toString(), strict);
}

this.validate();
validate();
}

public void setFields(Object fields){
Expand Down Expand Up @@ -101,7 +101,7 @@ public final void validate() throws ForeignKeyException{
if(this.strictValidation){
throw fke;
}else{
this.getErrors().add(fke);
errors.add(fke);
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@ public String getJson(){
}

public List<Exception> getErrors(){
return this.errors;
return errors;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class JsonSchema {
Expand Down Expand Up @@ -52,10 +54,14 @@ public Set<ValidationMessage> validate(JsonNode json) {
log.warn(msg);
throw new ValidationException(this, errors);
} else {
log.info(msg);
log.warn(msg);
return errors;
}
}
}

public String getName() {
return (null == jsonSchema) ? null : jsonSchema.getSchemaNode().get("title").asText();
}

}
Loading

0 comments on commit 1974f6a

Please sign in to comment.