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.

@FunctionalInterface public interface Validator<T, A extends Annotation>

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 Classes
    Modifier and Type
    Interface
    Description
    static class 
    This context provides access to the InvocationContext of this interaction and has some utility methods useful for error messages.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    apply(T argument, A annotation, Validator.Context context)
    Validates an argument.
  • Method Details

    • apply

      void apply(T argument, A annotation, Validator.Context context)

      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 validate
      annotation - the corresponding annotation
      context - the corresponding InvocationContext