Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support property value match rule
  • Loading branch information
benfortuna committed Mar 6, 2022
1 parent bc5f2f8 commit febd795
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/main/java/net/fortuna/ical4j/validate/PropertyRuleSet.java
Expand Up @@ -39,16 +39,20 @@
import java.util.Collections;
import java.util.List;

public class PropertyRuleSet extends AbstractValidationRuleSet<Property> {
public class PropertyRuleSet<T extends Property> extends AbstractValidationRuleSet<T> {

public PropertyRuleSet(String context, ValidationRule... rules) {
super(context, rules);
public PropertyRuleSet(ValidationRule... rules) {
super(rules);
}

@Override
public List<ValidationEntry> apply(Property target) {
public List<ValidationEntry> apply(String context, Property target) {
List<ValidationEntry> results = new ArrayList<>();
for (ValidationRule rule: rules) {
// predicate rule hack..
if (rule.getType() == null) {
continue;
}
List<String> matches = Collections.emptyList();
int total = rule.getInstances().stream().mapToInt(s -> target.getParameters(s).size()).sum();
switch (rule.getType()) {
Expand All @@ -72,12 +76,15 @@ public List<ValidationEntry> apply(Property target) {
break;
case AllOrNone:
if (total > 0 && total != rule.getInstances().size()) {
results.add(new ValidationEntry(rule, context));
results.add(new ValidationEntry(rule, target.getName()));
}
break;
case ValueMatch:
matches = matches(rule.getInstances(), s -> !target.getValue().matches(s));
break;
}
if (!matches.isEmpty()) {
results.add(new ValidationEntry(rule, context, matches.toArray(new String[0])));
results.add(new ValidationEntry(rule, target.getName(), matches.toArray(new String[0])));
}
}
return results;
Expand Down
Expand Up @@ -21,7 +21,8 @@ public enum ValidationType {
OneOrLess("The following are OPTIONAL, but MUST NOT occur more than once."),
OneOrMore("The following are OPTIONAL, and MAY occur more than once."),
OneExclusive("If one is present, ALL others MUST NOT be present."),
AllOrNone("If one is present, ALL must be present.");
AllOrNone("If one is present, ALL must be present."),
ValueMatch("Value MUST match expression.");

private final String description;

Expand Down

0 comments on commit febd795

Please sign in to comment.