All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.avaje.validation.adapter.ValidationContext Maven / Gradle / Ivy

package io.avaje.validation.adapter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.jspecify.annotations.Nullable;

/** Context used to lookup validation adapters and create validation requests. */
public interface ValidationContext {

  /**
   * Return the adapter for the given type.
   *
   * @param cls The class for which the adapter is requested
   * @param  The type this adapter validates
   * @return The validation adapter for the given type
   */
   ValidationAdapter adapter(Class cls);

  /**
   * Return the adapter for the given type.
   *
   * @param type The type for which the adapter is requested
   * @param  The type this adapter validates
   * @return The validation adapter for the given type
   */
   ValidationAdapter adapter(Type type);

  /**
   * Return the constraint adapter for the given annotation with attributes.
   *
   * @param cls The annotation class
   * @param attributes The attributes associated with the annotation
   * @param  The type this adapter validates
   * @return The validation adapter for the given annotation with attributes
   */
   ValidationAdapter adapter(Class cls, Map attributes);

  /**
   * Return the constraint adapter for the given annotation with attributes. Used for adapters that
   * combine multiple annotation adapters.
   *
   * @param cls The class representing the annotation type
   * @param groups The validation groups associated with the annotation
   * @param message The error message associated with the annotation
   * @param attributes The attributes associated with the annotation
   * @param  The type this adapter validates
   * @return The validation adapter for the given annotation with attributes
   */
   ValidationAdapter adapter(
      Class cls,
      Set> groups,
      String message,
      Map attributes);

  /**
   * Return a no-op adapter.
   *
   * @param  The type this adapter validates
   * @return The no-op validation adapter
   */
   ValidationAdapter noop();

  /**
   * Create a message object using the annotation attribute "message".
   *
   * @param attributes The attributes associated with the annotation
   * @return The message object
   */
  Message message(Map attributes);

  /**
   * Create a message object using the given string.
   *
   * @param message The error message
   * @param attributes The attributes associated with the annotation
   * @return The message object created using the given string and annotation attributes
   */
  Message message(String message, Map attributes);

  /**
   * Create a validation request with the specified locale and groups.
   *
   * @param locale The locale for the validation request
   * @param groups The validation groups for the request
   * @return The validation request with the specified locale and groups
   */
  ValidationRequest request(@Nullable Locale locale, List> groups);

  /** Represents a message object used in error message interpolation. */
  interface Message {

    /**
     * Get the template for the message. A lookup will be performed on the configured resource
     * bundles to interpolate the message
     *
     * @return The template for the message
     */
    String template();

    /**
     * Get the annotation attributes associated with the message.
     *
     * @return The annotation attributes associated with the message
     */
    Map attributes();

    /**
     * Get the lookup key for the message.
     *
     * @return The template for the message + a unique number for deduplication purposes.
     */
    String lookupkey();
  }
  /** Request to create a Validation Adapter. */
  interface AdapterCreateRequest {

    /** Return the context. */
    ValidationContext ctx();

    /** Return the validation annotation type. */
    Class annotationType();

    /** Return the groups this constraint is active for. */
    Set> groups();

    /** Return the annotation attributes. */
    Map attributes();

    /** Return the attribute for the given key. */
    T attribute(String key);

    /** Return the message to use */
    Message message();

    /** Build and return a message given the new message key and attributes */
    Message message(String key, Object... extraKeyValues);

    /** Return the target type */
    String targetType();

    /** Clone and return the request with a new value attribute */
    AdapterCreateRequest withValue(long value);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy