Class AbstractValidator
java.lang.Object
ch.tocco.nice2.validate.api.validators.AbstractValidator
- Direct Known Subclasses:
AbstractGroupValidator
,DecimalDigitsValidatorImpl
,EmailValidatorImpl
,IbanValidatorImpl
,LengthValidatorImpl
,MandatoryValidatorImpl
,NumberRangeValidatorImpl
,PhoneValidatorImpl
,RegexValidatorImpl
,SocialSecurityNrValidatorImpl
,UrlValidatorImpl
Abstract base class for validators.
-
Nested Class Summary
Nested classes/interfaces inherited from interface ch.tocco.nice2.validate.api.validators.Validator
Validator.MergeMode
-
Field Summary
Modifier and TypeFieldDescriptionprotected boolean
protected ErrorLevel
protected String
protected boolean
protected RunOn
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract FieldValidationResult
clone()
boolean
If the validation process should continue on this level with next validator when this one "fails".boolean
doNegate()
Tells if the result of the validation is being negated (internally).protected AbstractValidationResult.Status
The status code depends on the error level.getLevel()
@Nullable String
getName()
Name of the validatorgetRunOn()
boolean
hasName()
protected boolean
If subclass overwrites and returns true then the validate() method won't do it anymore.protected boolean
isEmptySoIgnore
(Object value) Subclasses may override this to define what is considered empty, and thus disables the validator.boolean
mergeConstraints
(Validator validator, Validator.MergeMode mode) Default implementation so that subclasses are only forced to override canMergeConstraints().protected boolean
negateResultIfNeeded
(boolean result) If the negate=true flag was set (in xml) then this inverts the boolean.void
setContinue
(boolean doContinue) void
setLevel
(ErrorLevel level) void
void
setNegate
(boolean negate) void
final FieldValidationResult
All subclasses need to implement _validate() which will be called from here.Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface ch.tocco.nice2.validate.api.validators.Validator
canMergeConstraints, getType
-
Field Details
-
name
-
negate
protected boolean negate -
doContinue
protected boolean doContinue -
level
-
runOn
-
-
Constructor Details
-
AbstractValidator
public AbstractValidator()
-
-
Method Details
-
doContinueOnFailure
public boolean doContinueOnFailure()Description copied from interface:Validator
If the validation process should continue on this level with next validator when this one "fails". Failing means a negative result, how negative depends on ghe error level, an INFO counts as failed too (the test failed, it's just not that important).- Specified by:
doContinueOnFailure
in interfaceValidator
- Returns:
- .
-
doNegate
public boolean doNegate()Description copied from interface:Validator
Tells if the result of the validation is being negated (internally). This means that the properties of specific validator implementations like a number range have negated meaning in the end. -
setNegate
public void setNegate(boolean negate) -
setContinue
public void setContinue(boolean doContinue) - Specified by:
setContinue
in interfaceValidator
-
setLevel
-
getLevel
-
setRunOn
-
getRunOn
-
hasName
public boolean hasName() -
getName
Name of the validator -
setName
-
negateResultIfNeeded
protected boolean negateResultIfNeeded(boolean result) If the negate=true flag was set (in xml) then this inverts the boolean. All subclasses need to call this! -
iNegateMyself
protected boolean iNegateMyself()If subclass overwrites and returns true then the validate() method won't do it anymore. That's preferred for implementing validators (not groups).- See Also:
-
validate
public final FieldValidationResult validate(Object value, Map<String, Object> fields) throws IllegalArgumentExceptionAll subclasses need to implement _validate() which will be called from here. This method takes care of negating the result if necessary - so that no implementation forgets that important small flag.- Specified by:
validate
in interfaceValidator
- Parameters:
value
- the value to validate, non-nullfields
- (all fields, key=name, value=value plus type)- Throws:
IllegalArgumentException
- Wrong data type.
-
_validate
protected abstract FieldValidationResult _validate(Object value, Map<String, Object> fields) throws IllegalArgumentException- Throws:
IllegalArgumentException
-
getErrorStatusByLevel
The status code depends on the error level. -
mergeConstraints
Default implementation so that subclasses are only forced to override canMergeConstraints().- Specified by:
mergeConstraints
in interfaceValidator
- Parameters:
validator
- The validator to merge in.mode
- How to merge.- Returns:
true
if it was done. This should only fail if canMergeConstraints() says false.- See Also:
-
isEmptySoIgnore
Subclasses may override this to define what is considered empty, and thus disables the validator.An example is the string field: an empty input string means "no value given".
Imagine a zipcode field that may be empty (if unknown). A length validator (let's say 4 to 10 characters) must be ignored if no input (empty string) was given.
- Parameters:
value
- The object value as given to validate.- Returns:
- True to disable the validator, false to go on.
-
clone
- Specified by:
clone
in interfaceValidator
- Overrides:
clone
in classObject
- Throws:
CloneNotSupportedException
-