com.memority.toolkit.rule.api.validation.ExpectedRuleDefinition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toolkit-rule-api Show documentation
Show all versions of toolkit-rule-api Show documentation
This artifact provides the API classes that are necessary to implement the contracts of Memority configuration Rules.
/*
* Copyright (c) 2016-2023 Memority. All Rights Reserved.
*
* This file is part of Memority Toolkit API , a Memority project.
*
* This file is released under the Memority Public Artifacts End-User License Agreement,
* see
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
package com.memority.toolkit.rule.api.validation;
import org.springframework.beans.factory.annotation.Autowired;
import com.memority.toolkit.rule.api.RuleDefinition;
import com.memority.toolkit.rule.api.RuleType;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;
import java.util.List;
/**
* This validation annotation makes sure that the configured {@link RuleDefinition} respects the expected type and category,
* and also performs additional validation depending on the type of engine.
*
* Unless strict
is explicitly set to true
, the type and category may be null
: they
* will automatically be setup with the ones configured on the annotation if this is the case. This is because, although
* a type and category are necessary on a RuleDefinition, their value is fixed and should be possibly omitted from the input data.
*
*
To ensure that this mechanism is run before other validations, it is customary to setup this validation to
* run on the {Normalize.class} validation group, and to reconfigure the parent class with a {@link javax.validation.GroupSequence @GroupSequence}
* that will make sure normalize is run first.
*
* Example:
*
* {@literal @GroupSequence}({Normalize.class, Section.class})
* public class Section implements FeatureUIElement<Section>, ValueObject, Serializable {
*
* {@literal @ExpectedRuleDefinition}(type = RuleType.CONDITION, category = CitadelRuleCategory.FEATURE_NAME, groups = Normalize.class)
* {@literal @Valid}
* private RuleDefinition displayCondition;
*
* }
*
*/
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.TYPE_USE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {
ExpectedRuleDefinition.Validator.class,
ExpectedRuleDefinition.CollectionValidator.class
})
public @interface ExpectedRuleDefinition {
/**
* @return the expected type for the configured rule.
*/
RuleType type();
/**
* @return the expected category or categories for the configured rules. Typically a single value is
* set, but several may be given if the Rule may be run in a dynamic context.
*/
String[] category();
String MESSAGE_TEMPLATE_PREFIX = "com.memority.toolkit.rule.api.ExpectedRuleDefinition";
String message() default "{" + MESSAGE_TEMPLATE_PREFIX + ".message}";
String TYPE_CANNOT_BE_NULL = "typeNotNull";
String CATEGORY_CANNOT_BE_NULL = "categoryNotNull";
String MISMATCH_EXPECTED_TYPE = "mismatchExpectedType";
String UNACCEPTABLE_CATEGORY = "unacceptableCategory";
String LABEL_CANNOT_BE_NULL = "labelCannotBeNull";
String UNKNOWN_RULE = "unknownRule";
String UNKNOWN_CLASS_LABEL = "unknownClassLabel";
String MISSING_SPEC = "missingSpec";
Class>[] groups() default {};
Class extends Payload>[] payload() default {};
interface ValidatorDelegate extends ConstraintValidator {
}
interface CollectionValidatorDelegate extends ConstraintValidator> {
}
class Validator implements ConstraintValidator {
@Autowired(required = false)
ValidatorDelegate delegate;
private ExpectedRuleDefinition constraintAnnotation;
@Override
public void initialize(ExpectedRuleDefinition constraintAnnotation) {
this.constraintAnnotation = constraintAnnotation;
if (delegate != null) {
delegate.initialize(constraintAnnotation);
}
}
@Override
public boolean isValid(RuleDefinition definition, ConstraintValidatorContext constraintValidatorContext) {
if (definition == null) {
return true;
}
if (delegate == null) {
return true;
}
return delegate.isValid(definition, constraintValidatorContext);
}
}
class CollectionValidator implements ConstraintValidator> {
@Autowired(required = false)
CollectionValidatorDelegate delegate;
@Override
public void initialize(ExpectedRuleDefinition constraintAnnotation) {
if (delegate != null) {
delegate.initialize(constraintAnnotation);
}
}
@Override
public boolean isValid(List ruleDefinitions, ConstraintValidatorContext constraintValidatorContext) {
if (delegate == null) {
return true;
}
return delegate.isValid(ruleDefinitions, constraintValidatorContext);
}
}
}