Skip to content

Commit

Permalink
fix: apply json schema validation correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Pfeil committed Jan 8, 2025
1 parent 69b0d57 commit aaba94d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.kit.datamanager.pit.typeregistry.schema.SchemaInfo;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.json.JSONObject;

import java.util.Collection;
import java.util.Objects;
Expand All @@ -29,8 +30,12 @@ public boolean validate(String value) {
}

private boolean validate(Schema schema, String value) {
Object toValidate = value;
if (value.startsWith("{")) {
toValidate = new JSONObject(value);
}
try {
schema.validate(value);
schema.validate(toValidate);
} catch (ValidationException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.stream.Collectors;

public class SchemaSetGenerator {
private final Set<SchemaGenerator> GENERATORS;
private final AsyncLoadingCache<String, Set<SchemaInfo>> CACHE;
protected final Set<SchemaGenerator> GENERATORS;
protected final AsyncLoadingCache<String, Set<SchemaInfo>> CACHE;

public SchemaSetGenerator(ApplicationProperties props) {
GENERATORS = Set.of(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.kit.datamanager.pit.typeregistry.schema;

import edu.kit.datamanager.pit.configuration.ApplicationProperties;
import edu.kit.datamanager.pit.typeregistry.AttributeInfo;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

class SchemaSetGeneratorTest {

static ApplicationProperties properties;
static SchemaSetGenerator generator;

@BeforeAll
static void setup() throws Exception {
properties = new ApplicationProperties();
properties.setExpireAfterWrite(10);
properties.setMaximumSize(1000);
properties.setTypeRegistryUri(new URI("https://typeapi.lab.pidconsortium.net").toURL());
properties.setHandleBaseUri(new URI("https://hdl.handle.net").toURL());
generator = new SchemaSetGenerator(properties);
}

/**
* @throws ValidationException if a schema fails to validate
* @throws NoSuchElementException if no schema is found
*/
@Test
void testChecksumValidation() throws ValidationException, NoSuchElementException {
// generated test for this error message: Reason:\nAttribute 21.T11148/92e200311a56800b3e47 has a non-complying value { \"sha256sum\": \"sha256 c50624fd5ddd2b9652b72e2d2eabcb31a54b777718ab6fb7e44b582c20239a7c\" }
Set<SchemaInfo> schemaInfos = generator.generateFor("21.T11148/92e200311a56800b3e47").join();
AttributeInfo attributeInfo = new AttributeInfo("21.T11148/92e200311a56800b3e47", "name", "typeName", schemaInfos);
assertTrue(attributeInfo.validate("{ \"sha256sum\": \"sha256 c50624fd5ddd2b9652b72e2d2eabcb31a54b777718ab6fb7e44b582c20239a7c\" }"));
// This is currently not supported, but would be nice to have:
assertFalse(attributeInfo.validate("\"sha256 c50624fd5ddd2b9652b72e2d2eabcb31a54b777718ab6fb7e44b582c20239a7c\""));
}
}

0 comments on commit aaba94d

Please sign in to comment.