Skip to content

Commit

Permalink
Merge branch 'master' into task/191
Browse files Browse the repository at this point in the history
Apparently I failed update master before creating branch
task/191. Sigh...
  • Loading branch information
andylowry committed Aug 22, 2018
2 parents b970421 + 24dce5c commit cdc9d3a
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.reprezen.kaizen.oasparser.val;

import com.reprezen.kaizen.oasparser.val.msg.Messages;

public enum BaseValidationMessages implements Messages {
WrongTypeJson("Incorrect JSON value type: %1; allowed types: %2"), //
BadRef("Reference '%1' could not be resolved: %2"), //
PatternMatchFail("Value '%1' does not match required pattern '%2'"), //
BadPattern("Pattern is not a valid Java Regular Expression but may be valid in ECMA 262: %1"), //
BadUrl("Invalid URL '%1': %2"), //
BadEmail("Invalid email address '%1': %2"), //
NumberConstraint("Value %1 must be %2"), //
EmptyList("List may not be empty"), //
DuplicateValue("Value at '%2' appeared already: %1"), //
MissingField("Required field '%1' is missing"), //
WrongTypeFormat("OpenAPI-defined format '%1' requires type '%3' but appears with missing or invalid type %2"), //
WrongTypeValue("Value is incompatible with schema type");

private String formatString;

BaseValidationMessages(String formatString) {
this.formatString = formatString;
}

@Override
public String getFormatString() {
return formatString;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
*******************************************************************************/
package com.reprezen.kaizen.oasparser.val;

import static com.reprezen.kaizen.oasparser.val.Messages.m;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.WrongTypeJson;
import static com.reprezen.kaizen.oasparser.val.msg.Messages.msg;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -50,11 +51,17 @@ public static void checkJsonType(Overlay<?> value, ValidationResults results) {
return;
}
}
results.addError(m.msg("WrongTypeJson|Property bound to incompatible JSON Node type", json.getNodeType(),
allowedJsonTypes), value);
String allowed = allowedJsonTypes.stream().map(type -> getJsonValueType(type))
.collect(Collectors.joining(", "));
results.addError(msg(WrongTypeJson, getJsonValueType(json.getClass()), allowed), value);
}
}

private static String getJsonValueType(Class<? extends JsonNode> node) {
String type = node.getSimpleName();
return type.endsWith("Node") ? type.substring(0, type.length() - 4) : type;
}

private static Map<Class<?>, List<Class<? extends JsonNode>>> allowedJsonTypes = null;

private static Collection<Class<? extends JsonNode>> getAllowedJsonTypes(Overlay<?> value) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*******************************************************************************/
package com.reprezen.kaizen.oasparser.val;

import static com.reprezen.kaizen.oasparser.val.Messages.m;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.BadRef;
import static com.reprezen.kaizen.oasparser.val.msg.Messages.msg;

import com.reprezen.jsonoverlay.ListOverlay;
import com.reprezen.jsonoverlay.MapOverlay;
Expand Down Expand Up @@ -51,9 +52,7 @@ public static void checkReferences(PropertiesOverlay<?> props, ValidationResults

public static void checkReference(Reference ref, ValidationResults results, Overlay<?> context) {
if (ref.isInvalid()) {
results.addError(
m.msg("BadRef|JSON reference node was not resolved", ref.getRefString(), ref.getInvalidReason()),
context);
results.addError(msg(BadRef, ref.getRefString(), ref.getInvalidReason()), context);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.reprezen.kaizen.oasparser.val;

import static com.reprezen.kaizen.oasparser.val.Messages.m;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.BadEmail;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.BadPattern;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.BadUrl;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.DuplicateValue;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.EmptyList;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.MissingField;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.NumberConstraint;
import static com.reprezen.kaizen.oasparser.val.BaseValidationMessages.PatternMatchFail;
import static com.reprezen.kaizen.oasparser.val.msg.Messages.msg;

import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -66,9 +74,7 @@ public final Overlay<String> validateStringField(String name, boolean required,

void checkPattern(Overlay<String> field, Pattern pattern) {
if (!pattern.matcher(field.get()).matches()) {
results.addError(
m.msg("PatternMatchFail|String value does not match required pattern", field.get(), pattern),
field);
results.addError(msg(PatternMatchFail, field.get(), pattern), field);
}
}

Expand All @@ -81,9 +87,7 @@ private void checkRegex(Overlay<String> field) {
try {
Pattern.compile(regex);
} catch (PatternSyntaxException e) {
results.addWarning(
m.msg("BadPattern|Pattern is not a valid Java Regular Expression but may be valid ECMA 262", regex),
field);
results.addWarning(msg(BadPattern, regex), field);
}
}

Expand Down Expand Up @@ -126,7 +130,7 @@ private void checkUrl(Overlay<String> overlay, boolean allowVars) {
try {
new URL(url);
} catch (MalformedURLException e) {
results.addError(m.msg("BadUrl|Invalid URL", origUrl, e.toString()), overlay);
results.addError(msg(BadUrl, origUrl, e.toString()), overlay);
}
}

Expand Down Expand Up @@ -166,7 +170,7 @@ private void checkEmail(Overlay<String> overlay) {
addr.setAddress(email);
addr.validate();
} catch (AddressException e) {
results.addError(m.msg("BadEmail|Invalid email address", email, e.toString()), overlay);
results.addError(msg(BadEmail, email, e.toString()), overlay);
}
}

Expand All @@ -185,7 +189,7 @@ public Overlay<Number> validateNumericField(String name, boolean required, Funct
if (field != null && field.isPresent() && test != null) {
Number n = field.get();
if (!test.apply(n)) {
results.addError(m.msg("ReqPositive|Value must be " + desc, n), field);
results.addError(msg(NumberConstraint, desc, n), field);
}
}
return field;
Expand Down Expand Up @@ -228,7 +232,7 @@ private <X> void checkListNotEmpty(Overlay<List<X>> list, boolean nonEmpty) {
ListOverlay<X> listOverlay = Overlay.getListOverlay(list);
if (list != null && !list.isPresent()) {
if (nonEmpty && listOverlay.size() == 0) {
results.addError(m.msg("EmptyList|List may not be empty"), list);
results.addError(msg(EmptyList), list);
}
}
}
Expand All @@ -241,8 +245,7 @@ private <X> void checkListUnique(Overlay<List<X>> list, boolean unique) {
for (int i = 0; i < listOverlay.size(); i++) {
X item = listOverlay.get(i);
if (seen.contains(item)) {
results.addError(m.msg("DuplicateValue|Value appeared already", item, i),
Overlay.of(listOverlay, i));
results.addError(msg(DuplicateValue, item, i), Overlay.of(listOverlay, i));
} else {
seen.add(item);
}
Expand Down Expand Up @@ -271,7 +274,7 @@ private <X> void checkMapNotEmpty(Overlay<Map<String, X>> list, boolean nonEmpty
MapOverlay<X> mapOverlay = Overlay.getMapOverlay(list);
if (list != null && !list.isPresent()) {
if (nonEmpty && mapOverlay.size() == 0) {
results.addError(m.msg("EmptyList|List may not be empty"), list);
results.addError(msg(EmptyList), list);
}
}
}
Expand All @@ -284,8 +287,7 @@ private <X> void checkMapUnique(Overlay<Map<String, X>> map, boolean unique) {
for (String key : mapOverlay.keySet()) {
X value = mapOverlay.get(key);
if (seen.contains(value)) {
results.addError(m.msg("DuplicateValue|Value appeared already", value, key),
Overlay.of(mapOverlay, key));
results.addError(msg(DuplicateValue, value, key), Overlay.of(mapOverlay, key));
} else {
seen.add(value);
}
Expand All @@ -295,7 +297,7 @@ private <X> void checkMapUnique(Overlay<Map<String, X>> map, boolean unique) {

void checkMissing(Overlay<?> field, boolean required) {
if (required && (field == null || !field.isPresent())) {
results.addError(m.msg("MissingField|required field is missing", field.getPathInParent()), value);
results.addError(msg(MissingField, field.getPathInParent()), value);
}
}

Expand Down Expand Up @@ -331,10 +333,7 @@ public Overlay<String> validateFormatField(String name, boolean required, String
}
if (normalType != null) {
if (type == null || !type.equals(normalType)) {
results.addWarning(
m.msg("WrongTypeFormat|OpenAPI-defined format used with nonstandard or missing type", field,
type, normalType),
field);
results.addWarning(msg(BaseValidationMessages.WrongTypeFormat, field, type, normalType), field);
}
}
}
Expand Down Expand Up @@ -366,8 +365,7 @@ public void checkDefault(Overlay<?> overlay, String type) {
break;
}
if (!ok) {
results.addError(m.msg("WrongTypeValue|Value is incompatible with schema type", type, defaultValue),
overlay);
results.addError(msg(BaseValidationMessages.WrongTypeValue, type, defaultValue), overlay);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.reprezen.kaizen.oasparser.val.msg;

import java.util.Locale;
import java.util.Properties;

public interface Messages {

// TODO: add a method to test a localization file for missing and invalid
// property names (compare all the property keys to the enum value names). This
// should be easily executed by a main method in the enum class. It would be
// great to also flag localized messages whose list of placeholder positions
// does not match that of the default message.
public String getFormatString();

public String name();

public default String getFormatString(Locale locale) {
Properties localeStrings = locale != null ? getFormatStrings(locale) : null;
String formatString = localeStrings != null ? localeStrings.getProperty(name()) : null;
return formatString != null ? formatString : getFormatString();
}

public default Properties getFormatStrings(Locale locale) {
return MessagesHelper.loadLocalizations(this.getClass(), locale);
}

public default String msg(Object... args) {
return msg(Locale.getDefault(), args);
}

public default String msgNoLocale(Object... args) {
return msg((Locale) null, args);
}

public default String msg(Locale locale, Object... args) {
return MessagesHelper.format(locale, this, args);
}

public static String msg(Messages instance, Object... args) {
return instance.msg(args);
}
}
Loading

0 comments on commit cdc9d3a

Please sign in to comment.