类 ErrorCodeValidator

java.lang.Object
io.github.tcq1007.valid.errorcode.util.ErrorCodeValidator

public class ErrorCodeValidator extends Object
Utility class for validating error code format and rules.

This class provides functionality to validate error codes according to specified rules, including format validation, length checking, prefix validation, and uniqueness verification across the codebase.

All methods in this class are static as it serves as a utility class.

  • 方法详细资料

    • validateErrorCodeFormat

      public static void validateErrorCodeFormat(Element element, int code, String prefix, int totalLength, String fieldName, Messager messager)
      Validates the format of an error code according to specified rules.

      This method performs two main validations:

      • Checks if the error code has the correct length
      • Verifies that the error code starts with the specified prefix

      参数:
      element - The element to report errors on, typically an enum constant
      code - The numeric error code value to validate
      prefix - The required prefix that the error code must start with
      totalLength - The required total length of the error code
      fieldName - The name of the error code field for error reporting
      messager - The messager instance for reporting validation errors
    • shouldExcludeFromValidation

      public static boolean shouldExcludeFromValidation(Integer value, int[] excludeValues)
      Determines if a value should be excluded from validation.

      This method checks if the given value matches any of the values in the exclude list. This is useful for special cases like success codes (0) that might not need to follow the standard validation rules.

      参数:
      value - The numeric value to check
      excludeValues - Array of values that should be excluded from validation
      返回:
      true if the value should be excluded from validation, false if it should be validated
    • reportError

      public static void reportError(Element element, String message, Messager messager)
      Report a general error message
      参数:
      element - The element to report the error on
      message - The error message
      messager - The messager for reporting errors
    • validateErrorCodeUniqueness

      public static boolean validateErrorCodeUniqueness(String errorCode, Element element, Messager messager)
      Validates the uniqueness of an error code.

      This method checks if the given error code is unique across all processed enum constants. If a duplicate is found, it reports an error message indicating where the error code was previously defined.

      参数:
      errorCode - The error code to validate
      element - The element containing the error code
      messager - The messager for reporting errors
      返回:
      true if the error code is unique, false if a duplicate was found