类 ErrorCodeValidator
java.lang.Object
io.github.tcq1007.valid.errorcode.util.ErrorCodeValidator
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.
-
方法概要
修饰符和类型方法说明static voidreportError(Element element, String message, Messager messager) Report a general error messagestatic booleanshouldExcludeFromValidation(Integer value, int[] excludeValues) Determines if a value should be excluded from validation.static voidvalidateErrorCodeFormat(Element element, int code, String prefix, int totalLength, String fieldName, Messager messager) Validates the format of an error code according to specified rules.static booleanvalidateErrorCodeUniqueness(String errorCode, Element element, Messager messager) Validates the uniqueness of an error code.
-
方法详细资料
-
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 constantcode- The numeric error code value to validateprefix- The required prefix that the error code must start withtotalLength- The required total length of the error codefieldName- The name of the error code field for error reportingmessager- The messager instance for reporting validation errors
-
shouldExcludeFromValidation
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 checkexcludeValues- Array of values that should be excluded from validation- 返回:
trueif the value should be excluded from validation,falseif it should be validated
-
reportError
Report a general error message- 参数:
element- The element to report the error onmessage- The error messagemessager- 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 validateelement- The element containing the error codemessager- The messager for reporting errors- 返回:
- true if the error code is unique, false if a duplicate was found
-