Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.5.9</version>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package ch.ethz.library.darc.model;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.Error;
import com.networknt.schema.InputFormat;
import com.networknt.schema.Schema;
import com.networknt.schema.SchemaLocation;
import com.networknt.schema.SchemaRegistry;
import com.networknt.schema.SpecificationVersion;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -28,8 +26,7 @@
public class JsonSchemaValidationTest {

private static final String SCHEMAS_DIR = "schemas/data-archive";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


/**
* Validates all JSON schema files in the schemas/data-archive directory against
* the official JSON Schema draft specification.
Expand All @@ -39,39 +36,38 @@ public class JsonSchemaValidationTest {
public void validateAllJsonSchemas() throws IOException {
// Get all JSON files in the schemas directory (excluding _shared directory)
Set<Path> schemaFiles = findJsonFiles(SCHEMAS_DIR);

// Create a JSON Schema validator for Draft 2020-12
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);

// Load the official meta-schema using URL instead of string
URL metaSchemaUrl = new URL("https://json-schema.org/draft/2020-12/schema");
InputStream metaSchemaStream = metaSchemaUrl.openStream();
JsonSchema metaSchema = factory.getSchema(metaSchemaStream);


// Create a schema registry for Draft 2020-12
SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);

// Load the bundled meta-schema (no network fetch needed)
Schema metaSchema = registry.getSchema(
SchemaLocation.of("https://json-schema.org/draft/2020-12/schema"));

// Track if any validation errors occurred
boolean allSchemasValid = true;
StringBuilder errorMessages = new StringBuilder();
// Validate each schema file

// Validate each schema file using string-based validation
for (Path schemaPath : schemaFiles) {
JsonNode schemaNode = OBJECT_MAPPER.readTree(schemaPath.toFile());
Set<ValidationMessage> validationMessages = metaSchema.validate(schemaNode);
if (!validationMessages.isEmpty()) {
String schemaContent = Files.readString(schemaPath);
List<Error> errors = metaSchema.validate(schemaContent, InputFormat.JSON);

if (!errors.isEmpty()) {
allSchemasValid = false;
errorMessages.append("Validation errors in ").append(schemaPath).append(":\n");
for (ValidationMessage message : validationMessages) {
errorMessages.append(" - ").append(message).append("\n");
for (Error error : errors) {
errorMessages.append(" - ").append(error).append("\n");
}
}
}

// Assert that all schemas are valid
assertTrue(allSchemasValid,
"Some JSON schemas are not valid against the official draft schema:\n" +
assertTrue(allSchemasValid,
"Some JSON schemas are not valid against the official draft schema:\n" +
errorMessages);
}

/**
* Finds all JSON files in the specified directory and its subdirectories,
* excluding files in the _shared directory.
Expand All @@ -85,4 +81,4 @@ private Set<Path> findJsonFiles(String directory) throws IOException {
.collect(Collectors.toSet());
}
}
}
}
Loading