Class AbstractValidator

java.lang.Object
ch.tocco.nice2.validate.api.validators.AbstractValidator
All Implemented Interfaces:
Validator, Cloneable
Direct Known Subclasses:
AbstractGroupValidator, DecimalDigitsValidatorImpl, EmailValidatorImpl, IbanValidatorImpl, LengthValidatorImpl, MandatoryValidatorImpl, NumberRangeValidatorImpl, PhoneValidatorImpl, RegexValidatorImpl, SocialSecurityNrValidatorImpl, UrlValidatorImpl

public abstract class AbstractValidator extends Object implements Validator
Abstract base class for validators.
  • Field Details

    • name

      protected String name
    • negate

      protected boolean negate
    • doContinue

      protected boolean doContinue
    • level

      protected ErrorLevel level
    • runOn

      protected RunOn 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 interface Validator
      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.
      Specified by:
      doNegate in interface Validator
      Returns:
      .
    • setNegate

      public void setNegate(boolean negate)
      Specified by:
      setNegate in interface Validator
    • setContinue

      public void setContinue(boolean doContinue)
      Specified by:
      setContinue in interface Validator
    • setLevel

      public void setLevel(ErrorLevel level)
      Specified by:
      setLevel in interface Validator
    • getLevel

      public ErrorLevel getLevel()
      Specified by:
      getLevel in interface Validator
    • setRunOn

      public void setRunOn(RunOn runOn)
      Specified by:
      setRunOn in interface Validator
    • getRunOn

      public RunOn getRunOn()
      Specified by:
      getRunOn in interface Validator
    • hasName

      public boolean hasName()
      Specified by:
      hasName in interface Validator
      Returns:
      Tells if this validator has a name, otherwise it is null.
    • getName

      @Nullable public @Nullable String getName()
      Name of the validator
      Specified by:
      getName in interface Validator
    • setName

      public void setName(String name)
      Specified by:
      setName in interface Validator
    • 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 IllegalArgumentException
      All 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 interface Validator
      Parameters:
      value - the value to validate, non-null
      fields - (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

      protected AbstractValidationResult.Status getErrorStatusByLevel()
      The status code depends on the error level.
    • mergeConstraints

      public boolean mergeConstraints(Validator validator, Validator.MergeMode mode)
      Default implementation so that subclasses are only forced to override canMergeConstraints().
      Specified by:
      mergeConstraints in interface Validator
      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

      protected boolean isEmptySoIgnore(Object value)
      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 interface Validator
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException