public abstract class Verifiable extends Object implements Serializable
This Class contains checks for different fields that is used as part of the input and output Objects. Reason for having this, is because it is important that all data is 100% reliable when it is coming in so it can be processed correctly without any errors occurring.
Although all Classes and Fields are annotated with the necessary checks, this is not a guarantee that the data will also correctly be passed through, since different frameworks may choose to discard the Annotated requirements or have flaws. Hence, this simple PoJo approach will act as a last line of defense before data is being processed.
Constructor and Description |
---|
Verifiable() |
Modifier and Type | Method and Description |
---|---|
protected static void |
checkIntegerWithMax(Map<String,String> errors,
String field,
int value,
int max,
String message)
Checks the given Integer value, to ensure that it is valid, i.e.
|
protected static void |
checkNotNull(Map<String,String> errors,
String field,
Object value,
String message)
Checks that the given Object is not null, if so - then the given
message will be added to the error map, using the field as key.
|
protected static void |
checkNotNullAndValidId(Map<String,String> errors,
String field,
String value,
String message)
Checks that the given value is neither null, nor an invalid Id.
|
protected static void |
checkNotNullEmptyOrTooLong(Map<String,String> errors,
String field,
String value,
int maxLength,
String message)
Checks that the given String is neither null, empty, nor too long.
|
protected static void |
checkNotNullOrEmpty(Map<String,String> errors,
String field,
byte[] value,
String message)
Checks a byte array, to ensure that that it is neither null nor empty,
if the validation failed, then the given message will be added to the
error map with the field as key.
|
protected static void |
checkNotNullOrEmpty(Map<String,String> errors,
String field,
String value,
String message)
Checks if the given String is neither null nor empty, if the check
failed, then the given message will be added to the error map using the
field as key.
|
protected static void |
checkNotTooLong(Map<String,String> errors,
String field,
String value,
int maxLength,
String message)
Checks that the given String is neither null, nor that the trimmed
version of the String exceeds the given maximum length.
|
protected static void |
checkUrl(Map<String,String> errors,
String value)
Checks if a URL is valid or not, it uses the URL methods to see if it
is valid, and if not - the given error map is extended with the error
information from the Exception thrown.
|
protected static void |
checkValidId(Map<String,String> errors,
String field,
String value,
String message)
Checks if the given Id is valid, i.e.
|
static boolean |
isEmpty(String value)
Method added as per PMD rule InefficientEmptyStringCheck.
|
abstract Map<String,String> |
validate()
Simple Validation method, which checks if the required values are usable
or not.
|
public abstract Map<String,String> validate()
protected static void checkNotNull(Map<String,String> errors, String field, Object value, String message)
Checks that the given Object is not null, if so - then the given message will be added to the error map, using the field as key.
errors
- Error Mapfield
- Key for the Error Mapvalue
- The value Object to checkmessage
- Message to add to Error Map, if validation failedprotected static void checkNotNullOrEmpty(Map<String,String> errors, String field, byte[] value, String message)
Checks a byte array, to ensure that that it is neither null nor empty, if the validation failed, then the given message will be added to the error map with the field as key.
errors
- Error Mapfield
- Key for the Error Mapvalue
- The byte array to checkmessage
- Message to add to Error Map, if validation failedprotected static void checkNotNullOrEmpty(Map<String,String> errors, String field, String value, String message)
Checks if the given String is neither null nor empty, if the check failed, then the given message will be added to the error map using the field as key.
errors
- Error Mapfield
- Key for the Error Mapvalue
- The String to check that it is neither null nor emptymessage
- Message to add to Error Map, if validation failedisEmpty(String)
protected static void checkNotNullEmptyOrTooLong(Map<String,String> errors, String field, String value, int maxLength, String message)
Checks that the given String is neither null, empty, nor too long. The
method is a shorthand method invoking the
checkNotNullOrEmpty(Map, String, String, String)
&
checkNotTooLong(Map, String, String, int, String)
methods.
errors
- Error Mapfield
- Key for the Error Mapvalue
- The value Object to checkmaxLength
- The maximum length the given String may havemessage
- Message to add to Error Map, if validation failedcheckNotNullOrEmpty(Map, String, String, String)
,
checkNotTooLong(Map, String, String, int, String)
protected static void checkNotTooLong(Map<String,String> errors, String field, String value, int maxLength, String message)
Checks that the given String is neither null, nor that the trimmed version of the String exceeds the given maximum length. If the validation failed, then the message will be added to the error map, using the field as key.
errors
- Error Mapfield
- Key for the Error Mapvalue
- The String to checkmaxLength
- The max length for the given String valuemessage
- Message to add to Error Map, if validation failedprotected static void checkValidId(Map<String,String> errors, String field, String value, String message)
Checks if the given Id is valid, i.e. that it matches the standard UUID regular expression. The method takes an error map which it will add the given field to as a key with the message provided, if the id (value) it invalid.
errors
- Map to store the error information in, if id is invalidfield
- Name of the field from the request/dto holding the valuevalue
- The value to check if is a valid Id (UUID)message
- The error message to add to the error mapConstants.ID_PATTERN_REGEX
protected static void checkNotNullAndValidId(Map<String,String> errors, String field, String value, String message)
Checks that the given value is neither null, nor an invalid Id. The
method is a shorthand method invoking the
checkNotNull(Map, String, Object, String)
&
checkValidId(Map, String, String, String)
methods.
errors
- Error Mapfield
- Key for the Error Mapvalue
- The value Object to checkmessage
- Message to add to Error Map, if validation failedcheckNotNull(Map, String, Object, String)
,
checkValidId(Map, String, String, String)
protected static void checkIntegerWithMax(Map<String,String> errors, String field, int value, int max, String message)
Checks the given Integer value, to ensure that it is valid, i.e. within the values 0 (zero) and the given max. If not, then the message is added to the error map with the given field as key.
errors
- Error Mapfield
- Key for the Error Mapvalue
- The Integer to checkmax
- The maximum allowed value for the checked Integermessage
- Message to add to Error Map, if validation failedprotected static void checkUrl(Map<String,String> errors, String value)
Checks if a URL is valid or not, it uses the URL methods to see if it is valid, and if not - the given error map is extended with the error information from the Exception thrown. The method is thus not logging ot re-throwing the exception, as the method is only intended as a pre-processing check.
The key for the error map, is the Constants.FIELD_URL
, which
is the only CWS field holding a URL, and thus it is not a parameter to
this method, although it is for the other methods.
errors
- Map to store the error information in, if URL is invalidvalue
- the URL to checkConstants.FIELD_URL
public static boolean isEmpty(String value)
Method added as per PMD rule InefficientEmptyStringCheck. It checks the given value to see if it contains any non-whitespace characters. If so, then it returns false - if the value has zero length or only whitespace characters, then it returns true.
value
- Nullable value to check if is emptyCopyright © 2020 JavaDog.io. All rights reserved.