Some tweaks to the contraints checking for releases.

This commit is contained in:
Kris De Volder
2017-07-14 16:52:57 -07:00
parent 1e920da6f8
commit 7e53e6b366
5 changed files with 90 additions and 8 deletions

View File

@@ -116,6 +116,18 @@ public class NodeUtil {
return null;
}
public static NodeTuple getPropertyTuple(Node node, String propName) {
if (node instanceof MappingNode) {
for (NodeTuple entry : ((MappingNode)node).getValue()) {
String key = NodeUtil.asScalar(entry.getKeyNode());
if (propName.equals(key)) {
return entry;
}
}
}
return null;
}
public static Node getProperty(Node node, String propName) {
if (node instanceof MappingNode) {
for (NodeTuple entry : ((MappingNode)node).getValue()) {

View File

@@ -49,17 +49,28 @@ public class Constraints {
public static Constraint requireAtMostOneOf(String... properties) {
return new RequireOneOf(properties).allowFewer(true);
}
public static Constraint requireAtLeastOneOf(String... properties) {
return new RequireOneOf(properties).allowMultiple(true);
}
static private class RequireOneOf implements Constraint {
private final String[] _requiredProps;
private boolean allowFewer = false;
private boolean allowMultiple = false;
public RequireOneOf(String[] properties) {
Assert.isLegal(properties.length>1);
this._requiredProps = properties;
}
public Constraint allowMultiple(boolean b) {
this.allowMultiple = b;
return this;
}
public Constraint allowFewer(boolean b) {
this.allowFewer = b;
return this;
@@ -80,7 +91,7 @@ public class Constraints {
problems.accept(missingProperty(
"One of "+requiredProps+" is required for '"+type+"'", doc, parent, map));
}
} else if (foundPropsCount>1) {
} else if (foundPropsCount>1 && !allowMultiple) {
//Mark each of the found keys as a violation:
for (NodeTuple entry : map.getValue()) {
String key = NodeUtil.asScalar(entry.getKeyNode());
@@ -144,4 +155,5 @@ public class Constraints {
}
};
}
}