Skip to content
Permalink
Browse files

switch json schema validator libraries (#462)

  • Loading branch information
tsiq-karold committed Nov 28, 2019
1 parent b1f86fc commit 8bf2b9435283d8d1e0202eba85ee222920a74850
@@ -781,11 +781,4 @@
</build>
</profile>
</profiles>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>
@@ -50,15 +50,9 @@
</dependency>

<dependency>
<groupId>com.github.everit-org.json-schema</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>1.9.2</version>
<exclusions>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.28</version>
</dependency>

<!-- We don't use joda directly but it is a transitive dependency of a couple of libraries
@@ -1,32 +1,32 @@
package com.twosigma.webtau.schema.expectation;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.ValidationMessage;
import com.twosigma.webtau.expectation.ActualPath;
import com.twosigma.webtau.expectation.ValueMatcher;
import com.twosigma.webtau.http.datacoverage.DataNodeToMapOfValuesConverter;
import com.twosigma.webtau.http.datanode.DataNode;
import com.twosigma.webtau.schema.JsonSchemaConfig;
import com.twosigma.webtau.utils.FileUtils;
import com.twosigma.webtau.utils.JsonUtils;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class SchemaMatcher implements ValueMatcher {
private final String schemaFileName;
private final Schema schema;
private final JsonSchema schema;

public SchemaMatcher(String schemaFileName) {
this.schemaFileName = schemaFileName;

Path schemaFilePath = JsonSchemaConfig.getSchemasDir().resolve(schemaFileName);
JSONObject rawSchema = new JSONObject(new JSONTokener(FileUtils.fileTextContent(schemaFilePath)));
this.schema = SchemaLoader.load(rawSchema);
JsonSchemaFactory factory = JsonSchemaFactory.getInstance();
this.schema = factory.getSchema(FileUtils.fileTextContent(schemaFilePath));
}

@Override
@@ -59,13 +59,9 @@ public boolean matches(ActualPath actualPath, Object actual) {
actualObj = converter.convert((DataNode) actual);
}

JSONObject actualJsonObject = new JSONObject(JsonUtils.serialize(actualObj));
try {
schema.validate(actualJsonObject);
return Collections.emptyList();
} catch (ValidationException e) {
return e.getAllMessages();
}
JsonNode actualJsonObject = JsonUtils.convertToTree(actualObj);
Set<ValidationMessage> validationMessages = schema.validate(actualJsonObject);
return validationMessages.stream().map(ValidationMessage::toString).collect(Collectors.toList());
}

@Override
@@ -26,15 +26,15 @@ class SchemaMatcherTest {
code {
actual([name: "test"]).should(complyWithSchema(TEST_SCHEMA))
} should throwException('\n[value] expected to comply with schema test-schema.json\n' +
'[#: required key [val] not found]')
'[$.val: is missing but it is required]')
}

@Test
void "should throw exception when object has incorrect types"() {
code {
actual([name: "test", val: "foo"]).should(complyWithSchema(TEST_SCHEMA))
} should throwException('\n[value] expected to comply with schema test-schema.json\n' +
'[#/val: expected type: Number, found: String]')
'[$.val: string found, integer expected]')
}

@Test
@@ -17,6 +17,7 @@
package com.twosigma.webtau.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
@@ -60,7 +61,6 @@ public static String serializePrettyPrint(Object json) {
return null;
}

ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, Map.class);
} catch (IOException e) {
@@ -72,13 +72,11 @@ public static String serializePrettyPrint(Object json) {
return deserializeAsMap(FileUtils.fileTextContent(file));
}

@SuppressWarnings("unchecked")
public static List<?> deserializeAsList(String json) {
if (json == null) {
return null;
}

ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, List.class);
} catch (IOException e) {
@@ -95,11 +93,14 @@ public static Object deserialize(String json) {
return null;
}

ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, Object.class);
} catch (IOException e) {
throw new JsonParseException(e.getMessage());
}
}

public static JsonNode convertToTree(Object object) {
return mapper.valueToTree(object);
}
}

0 comments on commit 8bf2b94

Please sign in to comment.
You can’t perform that action at this time.