Interface ParameterValidator<T>

Type Parameters:
T - The parameter handler

public interface ParameterValidator<T>
A validator for a specific parameter type. These validators can do extra checks on parameters after they are resolved from ValueResolver or ContextResolvers.

Validators work on subclasses as well. For example, we can write a validator to validate a custom @Range(min, max) annotation for numbers:


 public enum RangeValidator implements ParameterValidator<Number> {
     INSTANCE;

     @Override public void validate(Number value, @NotNull CommandParameter parameter, @NotNull CommandActor actor) throws Throwable {
         Range range = parameter.getAnnotation(Range.class);
         if (range == null) return;
         double d = value.doubleValue();
         if (d < range.min())
             throw new CommandErrorException(actor, "Specified value (" + d + ") is less than minimum " + range.min());
         if (d > range.max())
             throw new CommandErrorException(actor, "Specified value (" + d + ") is greater than maximum " + range.max());
     }
 }
 

These can be registered through CommandHandler.registerParameterValidator(Class, ParameterValidator)

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    validate(T value, @NotNull CommandParameter parameter, @NotNull CommandActor actor)
    Validates the specified value that was passed to a parameter.
  • Method Details

    • validate

      void validate(T value, @NotNull @NotNull CommandParameter parameter, @NotNull @NotNull CommandActor actor)
      Validates the specified value that was passed to a parameter.

      Ideally, a validator will want to throw an exception when the parameter is not valid, and then further handled with CommandExceptionHandler.

      Parameters:
      value - The parameter value. May or may not be null, depending on the resolver.
      parameter - The parameter that will take this value
      actor - The command actor