Interface Validator<T, A extends Annotation>
- All Known Implementing Classes:
NotPermissionValidator, PermissionValidator
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Validators check if a command option fulfills the given constraint.
Register them at the [JDACBuilder#validator(Class, Validator)()] or use the
@Implementation.Validator
annotation of the guice extension.
Example
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(String.class)
public @interface MaxString {
int value();
}
public class MaxStringLengthValidator implements Validator<String, MaxString> {
@Override
public boolean apply(String argument, MaxString annotation, Context ctx) {
if (argument.length() < maxString.value()) {
context.fail("The given String is too long");
}
}
}
Perm and NotPerm Validators localization
The fail messages of the two default Validators for Perm and NotPerm
can be localized with the localization keys validator.noperm.fail and
validator.perm.fail respectively.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classThis context provides access to theInvocationContextof this interaction and has some utility methods useful for error messages. -
Method Summary
Modifier and TypeMethodDescriptionvoidapply(T argument, A annotation, Validator.Context context) Validates an argument.
-
Method Details
-
apply
Validates an argument.
If the parameter doesn't pass the validation, you can cancel this interaction by invoking
Validator.Context.fail(String, Entry...)with an appropriated error message.- Parameters:
argument- the argument to validateannotation- the corresponding annotationcontext- the correspondingInvocationContext
-