Skip to content

Commit

Permalink
Implement common error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkokar committed Apr 26, 2019
1 parent bc47c90 commit a251d3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ private static List<String> push(List<String> currentList, String element) {
return newList;
}

private static String message(List<String> parents, String self, JsonNode value) {
String fullName = Joiner.on(".").join(parents);
return format("Unexpected field type. Field '%s' should be %s, but it is %s", fullName, self, value.getNodeType());
}


/**
* Represents a schema field type.
Expand Down Expand Up @@ -200,12 +205,7 @@ public Optional<String> validate(List<String> parents, JsonNode parent, JsonNode
}

// Failed
return message(parents, value);
}

private Optional<String> message(List<String> parents, JsonNode tree) {
String fullName = Joiner.on(".").join(parents);
return Optional.of(format("Unexpected field type. Field '%s' should be %s, but it is %s", fullName, describe(), tree.getNodeType()));
return Optional.of(message(parents, describe(), value));
}

@Override
Expand All @@ -220,20 +220,14 @@ public String describe() {
public static class IntegerField implements FieldType {
@Override
public Optional<String> validate(List<String> parents, JsonNode parent, JsonNode value, Function<String, FieldType> typeExtensions) {
return (value.isInt() || canParseAsInteger(value)) ? Optional.empty() : message(parents, value);
return (value.isInt() || canParseAsInteger(value)) ? Optional.empty() : Optional.of(message(parents, describe(), value));
}

@Override
public String describe() {
return "INTEGER";
}

private Optional<String> message(List<String> parents, JsonNode tree) {
String fullName = Joiner.on(".").join(parents);

return Optional.of(format("Unexpected field type. Field '%s' should be INTEGER, but it is %s", fullName, tree.getNodeType()));
}

private static boolean canParseAsInteger(JsonNode value) {
try {
parseInt(value.textValue());
Expand All @@ -250,19 +244,14 @@ private static boolean canParseAsInteger(JsonNode value) {
public static class StringField implements FieldType {
@Override
public Optional<String> validate(List<String> parents, JsonNode parent, JsonNode value, Function<String, FieldType> typeExtensions) {
return value.isTextual() ? Optional.empty() : message(parents, value);
return value.isTextual() ? Optional.empty() : Optional.of(message(parents, describe(), value));
}

@Override
public String describe() {
return "STRING";
}

private Optional<String> message(List<String> parents, JsonNode tree) {
String fullName = Joiner.on(".").join(parents);

return Optional.of(format("Unexpected field type. Field '%s' should be STRING, but it is %s", fullName, tree.getNodeType()));
}
}

/**
Expand All @@ -273,19 +262,14 @@ public static class BoolField implements FieldType {

@Override
public Optional<String> validate(List<String> parents, JsonNode parent, JsonNode value, Function<String, FieldType> typeExtensions) {
return (value.isBoolean() || canParseAsBoolean(value)) ? Optional.empty() : message(parents, value);
return (value.isBoolean() || canParseAsBoolean(value)) ? Optional.empty() : Optional.of(message(parents, describe(), value));
}

@Override
public String describe() {
return "BOOLEAN";
}

private Optional<String> message(List<String> parents, JsonNode tree) {
String fullName = Joiner.on(".").join(parents);
return Optional.of(format("Unexpected field type. Field '%s' should be BOOLEAN, but it is %s", fullName, tree.getNodeType()));
}

private static boolean canParseAsBoolean(JsonNode value) {
return YAML_BOOLEAN_VALUES.matcher(value.asText()).matches();
}
Expand All @@ -307,19 +291,14 @@ public FieldType elementType() {

@Override
public Optional<String> validate(List<String> parents, JsonNode parent, JsonNode value, Function<String, FieldType> typeExtensions) {
return (value.isArray()) ? validateList(parents, parent, value, typeExtensions) : message(parents, value);
return (value.isArray()) ? validateList(parents, parent, value, typeExtensions) : Optional.of(message(parents, describe(), value));
}

@Override
public String describe() {
return format("LIST (%s)", elementType.describe());
}

private Optional<String> message(List<String> parents, JsonNode tree) {
String fullName = Joiner.on(".").join(parents);
return Optional.of(format("Unexpected field type. Field '%s' should be LIST (%s), but it is %s", fullName, elementType.describe(), tree.getNodeType()));
}

private Optional<String> validateList(List<String> parents, JsonNode parent, JsonNode list, Function<String, FieldType> lookup) {
for (int i = 0; i < list.size(); i++) {
JsonNode entry = list.get(i);
Expand Down Expand Up @@ -349,7 +328,7 @@ public FieldType elementType() {

@Override
public Optional<String> validate(List<String> parents, JsonNode parent, JsonNode value, Function<String, FieldType> typeExtensions) {
return value.isObject() ? validateMap(parents, parent, value, typeExtensions) : message(parents, value);
return value.isObject() ? validateMap(parents, parent, value, typeExtensions) : Optional.of(message(parents, describe(), value));
}

@Override
Expand All @@ -371,11 +350,6 @@ private Optional<String> validateMap(List<String> parents, JsonNode parent, Json
return validationError.get();
}

private Optional<String> message(List<String> parents, JsonNode tree) {
String fullName = Joiner.on(".").join(parents);
return Optional.of(format("Unexpected field type. Field '%s' should be MAP ('%s'), but it is %s", fullName, this.elementType().describe(), tree.getNodeType()));
}

}

private static void assertNoUnknownFields(String prefix, Schema schema, List<String> fieldsPresent) {
Expand Down Expand Up @@ -404,19 +378,14 @@ public Schema schema() {

@Override
public Optional<String> validate(List<String> parents, JsonNode parent, JsonNode value, Function<String, FieldType> typeExtensions) {
return value.isObject() ? validateObject(parents, parent, value, typeExtensions) : message(parents, value);
return value.isObject() ? validateObject(parents, parent, value, typeExtensions) : Optional.of(message(parents, describe(), value));
}

@Override
public String describe() {
return format("OBJECT (%s)", schema.name());
}

private Optional<String> message(List<String> parents, JsonNode tree) {
String fullName = Joiner.on(".").join(parents);
return Optional.of(format("Unexpected field type. Field '%s' should be OBJECT ('%s'), but it is %s", fullName, this.schema.name(), tree.getNodeType()));
}

private Optional<String> validateObject(List<String> parents, JsonNode parent, JsonNode tree, Function<String, FieldType> lookup) {

if (schema.ignore()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void checksListsOfSubObjects() throws Exception {
}

@Test(expectedExceptions = SchemaValidationException.class,
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.myList.\\[1\\]' should be OBJECT \\('x, y'\\), but it is STRING")
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.myList.\\[1\\]' should be OBJECT \\(x, y\\), but it is STRING")
public void checksListsOfSubObjects_shouldBeSubobjectButIsString() throws Exception {
JsonNode rootObject = YAML_MAPPER.readTree(""
+ "parent: \n"
Expand Down Expand Up @@ -401,7 +401,7 @@ public void expectingListOfObjectButIsString() throws Exception {
}

@Test(expectedExceptions = SchemaValidationException.class,
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.myChild' should be OBJECT \\('age'\\), but it is NUMBER")
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.myChild' should be OBJECT \\(age\\), but it is NUMBER")
public void checksSubObjectFieldTypes() throws Exception {
JsonNode rootObject = YAML_MAPPER.readTree(""
+ "parent: \n"
Expand Down Expand Up @@ -617,7 +617,7 @@ public void ignoresOpaqueSubobjectValidation() throws Exception {
}

@Test(expectedExceptions = SchemaValidationException.class,
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent' should be MAP \\('STRING'\\), but it is STRING")
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent' should be MAP \\(STRING\\), but it is STRING")
public void rejectsInvalidMaps() throws Exception {
JsonNode node2 = YAML_MAPPER.readTree(
"parent: x\n"
Expand All @@ -634,7 +634,7 @@ public void rejectsInvalidMaps() throws Exception {
}

@Test(expectedExceptions = SchemaValidationException.class,
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent' should be OBJECT \\('parent'\\), but it is STRING")
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent' should be OBJECT \\(parent\\), but it is STRING")
public void rejectsInvalidObjects() throws Exception {
JsonNode node2 = YAML_MAPPER.readTree(
"parent: x\n"
Expand Down Expand Up @@ -677,7 +677,7 @@ public void acceptsMapOfObjects() throws Exception {
}

@Test(expectedExceptions = SchemaValidationException.class,
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.key2' should be OBJECT \\('x, y'\\), but it is NUMBER")
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.key2' should be OBJECT \\(x, y\\), but it is NUMBER")
public void validatesMapOfObjects() throws Exception {
JsonNode node2 = YAML_MAPPER.readTree(
"parent: \n"
Expand Down Expand Up @@ -884,7 +884,7 @@ public void acceptssMapOfListOfObjects() throws Exception {
}

@Test(expectedExceptions = SchemaValidationException.class,
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.key1.\\[0\\]' should be OBJECT \\('x, y'\\), but it is STRING")
expectedExceptionsMessageRegExp = "Unexpected field type. Field 'parent.key1.\\[0\\]' should be OBJECT \\(x, y\\), but it is STRING")
public void validatesMapOfListOfObjects() throws Exception {
JsonNode node2 = YAML_MAPPER.readTree(
"parent: \n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class ServerConfigSchemaTest : DescribeSpec({
validateServerConfiguration(yamlConfig(minimalConfig + """
httpHandlers: x
""".trimIndent())) shouldBe Optional.of(
"Unexpected field type. Field 'httpHandlers' should be MAP ('OBJECT (name, type, tags, config)'), but it is STRING")
"Unexpected field type. Field 'httpHandlers' should be MAP (OBJECT (name, type, tags, config)), but it is STRING")
}

it("Validates nested HTTP handlers") {
Expand Down

0 comments on commit a251d3b

Please sign in to comment.