Skip to content

Commit 2936180

Browse files
committed
fix: adjust code and schemas to the new update (wip)
1 parent 0388ee7 commit 2936180

4 files changed

Lines changed: 65 additions & 36 deletions

File tree

src/main/java/edu/kit/datamanager/ro_crate/entities/validation/JsonSchemaValidation.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.JsonNode;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.networknt.schema.JsonSchema;
7-
import com.networknt.schema.JsonSchemaFactory;
8-
import com.networknt.schema.SpecVersion;
9-
import com.networknt.schema.ValidationMessage;
10-
6+
import com.networknt.schema.AbsoluteIri;
7+
import com.networknt.schema.Error;
8+
import com.networknt.schema.Schema;
9+
import com.networknt.schema.SchemaLocation;
10+
import com.networknt.schema.SchemaRegistry;
11+
import com.networknt.schema.dialect.DialectId;
1112
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
1213

13-
import java.net.URISyntaxException;
14+
import java.io.IOException;
15+
import java.io.InputStream;
1416
import java.net.URL;
17+
import java.util.Collection;
1518
import java.util.Objects;
16-
import java.util.Set;
1719

1820
/**
1921
* Implementation of the entity validation strategy that uses json schema.
@@ -27,19 +29,22 @@ public class JsonSchemaValidation implements EntityValidationStrategy {
2729
= Objects.requireNonNull(JsonSchemaValidation.class.getClassLoader()
2830
.getResource("json_schemas/entity_field_structure_schema.json"));
2931

30-
private JsonSchema entitySchema;
31-
private JsonSchema entityFieldSchema;
32+
private Schema entitySchema;
33+
private Schema entityFieldSchema;
3234

3335
/**
3436
* Default constructor that uses the default schemas.
3537
*/
3638
public JsonSchemaValidation() {
37-
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
38-
try {
39-
this.entitySchema = factory.getSchema(entitySchemaDefault.toURI());
40-
this.entityFieldSchema = factory.getSchema(fieldSchemaDefault.toURI());
41-
} catch (URISyntaxException e) {
42-
e.printStackTrace();
39+
SchemaRegistry factory = new SchemaRegistry.Builder()
40+
.defaultDialectId(DialectId.DRAFT_2019_09)
41+
.build();
42+
43+
try (InputStream entitySchemaStream = entitySchemaDefault.openStream(); InputStream fieldSchemaStream = fieldSchemaDefault.openStream()) {
44+
this.entitySchema = factory.getSchema(entitySchemaStream);
45+
this.entityFieldSchema = factory.getSchema(fieldSchemaStream);
46+
} catch (IOException e) {
47+
throw new RuntimeException(e);
4348
}
4449
}
4550

@@ -50,16 +55,20 @@ public JsonSchemaValidation() {
5055
* @param fieldSchema schema for the field validation.
5156
*/
5257
public JsonSchemaValidation(JsonNode entitySchema, JsonNode fieldSchema) {
53-
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
58+
SchemaRegistry factory = new SchemaRegistry.Builder()
59+
.defaultDialectId(DialectId.DRAFT_2019_09)
60+
.build();
5461
this.entitySchema = factory.getSchema(entitySchema);
5562
this.entityFieldSchema = factory.getSchema(fieldSchema);
5663
}
5764

5865
@Override
5966
public boolean validateEntity(JsonNode entity) {
60-
Set<ValidationMessage> errors = this.entitySchema.validate(entity);
61-
if (errors.size() != 0) {
62-
System.err.println("This entity does not comply to the basic RO-Crate entity structure.");
67+
Collection<Error> errors = this.entitySchema.validate(entity);
68+
if (!errors.isEmpty()) {
69+
System.err.println(
70+
"This entity does not comply to the basic RO-Crate entity structure."
71+
);
6372
errors.forEach(error -> System.err.println(error.getMessage()));
6473
return false;
6574
}
@@ -68,17 +77,23 @@ public boolean validateEntity(JsonNode entity) {
6877

6978
@Override
7079
public boolean validateFieldOfEntity(JsonNode field) {
71-
Set<ValidationMessage> errors = this.entityFieldSchema.validate(field);
80+
Collection<Error> errors = this.entityFieldSchema.validate(field);
7281
if (!errors.isEmpty()) {
7382
ObjectMapper objectMapper = MyObjectMapper.getMapper();
7483
System.err.println("The property: ");
7584
try {
76-
System.err.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(field));
85+
System.err.println(
86+
objectMapper
87+
.writerWithDefaultPrettyPrinter()
88+
.writeValueAsString(field)
89+
);
7790
} catch (JsonProcessingException e) {
7891
e.printStackTrace();
7992
}
80-
System.err.println("does not comply with the flattened structure"
81-
+ " of the RO-Crate json document.");
93+
System.err.println(
94+
"does not comply with the flattened structure" +
95+
" of the RO-Crate json document."
96+
);
8297
return false;
8398
}
8499
return true;

src/main/java/edu/kit/datamanager/ro_crate/validation/JsonSchemaValidation.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,44 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.networknt.schema.JsonSchema;
6-
import com.networknt.schema.JsonSchemaFactory;
7-
import com.networknt.schema.SpecVersion;
8-
import com.networknt.schema.ValidationMessage;
5+
import com.networknt.schema.AbsoluteIri;
6+
import com.networknt.schema.Error;
7+
import com.networknt.schema.Schema;
8+
import com.networknt.schema.SchemaLocation;
9+
import com.networknt.schema.SchemaRegistry;
910

11+
import com.networknt.schema.dialect.DialectId;
1012
import edu.kit.datamanager.ro_crate.Crate;
1113
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
1214

1315
import java.io.File;
1416
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.net.MalformedURLException;
1519
import java.net.URI;
1620
import java.net.URISyntaxException;
21+
import java.util.Collection;
1722
import java.util.Objects;
18-
import java.util.Set;
1923

2024
/**
2125
* Validation of the crate metadata using JSON-schema.
2226
*/
2327
public class JsonSchemaValidation implements ValidatorStrategy {
2428

2529
private static final String defaultSchema = "json_schemas/default.json";
26-
private JsonSchema schema;
30+
private Schema schema;
2731

2832
private void getSchema(URI schemaUri) {
29-
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
30-
this.schema = factory.getSchema(schemaUri);
33+
SchemaRegistry factory = new SchemaRegistry.Builder()
34+
.defaultDialectId(DialectId.DRAFT_2019_09)
35+
.build();
36+
try (InputStream stream = schemaUri.toURL().openStream()) {
37+
this.schema = factory.getSchema(stream);
38+
} catch (MalformedURLException e) {
39+
throw new RuntimeException(e);
40+
} catch (IOException e) {
41+
throw new RuntimeException(e);
42+
}
3143
}
3244

3345
/**
@@ -53,7 +65,9 @@ public JsonSchemaValidation(String schema) {
5365
}
5466

5567
public JsonSchemaValidation(JsonNode schema) {
56-
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
68+
SchemaRegistry factory = new SchemaRegistry.Builder()
69+
.defaultDialectId(DialectId.DRAFT_2019_09)
70+
.build();
5771
this.schema = factory.getSchema(schema);
5872
}
5973

@@ -62,8 +76,8 @@ public boolean validate(Crate crate) {
6276
ObjectMapper objectMapper = MyObjectMapper.getMapper();
6377
try {
6478
final JsonNode good = objectMapper.readTree(crate.getJsonMetadata());
65-
Set<ValidationMessage> errors = this.schema.validate(good);
66-
if (errors.size() == 0) {
79+
Collection<Error> errors = this.schema.validate(good);
80+
if (errors.isEmpty()) {
6781
return true;
6882
} else {
6983
System.err.println("This crate does not validate against the this schema."

src/main/resources/json_schemas/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"type": "object",
1212
"patternProperties": {
1313
".*": {
14-
"$ref": "entity_field_structure_schema.json"
14+
"$dynamicRef": "entity_field_structure_schema.json"
1515
}
1616
}
1717
}

src/main/resources/json_schemas/entity_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "object",
55
"patternProperties": {
66
".*": {
7-
"$ref": "entity_field_structure_schema.json"
7+
"$dynamicRef": "entity_field_structure_schema.json"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)