Class Validator

java.lang.Object
io.mangoo.routing.bindings.Validator
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Form, Request

public class Validator extends Object implements Serializable
Author:
svenkubiak
See Also:
  • Constructor Details

    • Validator

      public Validator()
  • Method Details

    • hasError

      public boolean hasError(String name)
      Checks if a give field has a validation error
      Parameters:
      name - The field to check
      Returns:
      True if the field has a validation error, false otherwise
    • getError

      public String getError(String name)
      Retrieves the error message for a given field
      Parameters:
      name - The field to check
      Returns:
      The error message for the field, or an empty string if no error is found
    • expectValue

      public void expectValue(String name)
      Validates a given field to be present with a value
      Parameters:
      name - The field to check
    • expectValue

      public void expectValue(String name, String message)
      Validates a given field to be present with a value
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
    • expectMinValue

      public void expectMinValue(String name, double minValue)
      Validates a given field to have a minimum value
      Parameters:
      name - The field to check
      minValue - The minimum value
    • expectMinValue

      public void expectMinValue(String name, double minValue, String message)
      Validates a given field to have a minimum value
      Parameters:
      name - The field to check
      minValue - The minimum value
      message - A custom error message instead of the default one
    • expectMinLength

      public void expectMinLength(String name, double minLength)
      Validates a given field to have a minimum length
      Parameters:
      name - The field to check
      minLength - The minimum length
    • expectMinLength

      public void expectMinLength(String name, double minLength, String message)
      Validates a given field to have a minimum length
      Parameters:
      name - The field to check
      minLength - The minimum length
      message - A custom error message instead of the default one
    • expectMaxValue

      public void expectMaxValue(String name, double maxValue)
      Validates a given field to have a maximum value
      Parameters:
      maxValue - The maximum value
      name - The field to check
    • expectMaxLength

      public void expectMaxLength(String name, double maxLength)
      Validates a given field to have a maximum length
      Parameters:
      maxLength - The maximum length
      name - The field to check
    • expectNumeric

      public void expectNumeric(String name)
      Validates that a given field has a numeric value
      Parameters:
      name - The field to check
    • expectNumeric

      public void expectNumeric(String name, String message)
      Validates that a given field has a numeric value
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
    • expectMaxLength

      public void expectMaxLength(String name, double maxLength, String message)
      Validates a given field to have a maximum length
      Parameters:
      name - The field to check
      maxLength - The maximum length
      message - A custom error message instead of the default one
    • expectMaxValue

      public void expectMaxValue(String name, double maxValue, String message)
      Validates a given field to have a maximum value
      Parameters:
      name - The field to check
      maxValue - The maximum value
      message - A custom error message instead of the default one
    • expectExactMatch

      public void expectExactMatch(String name, String anotherName)
      Validates two fields to exactly (case-sensitive) match
      Parameters:
      name - The field to check
      anotherName - The other field to check against
    • expectExactMatch

      public void expectExactMatch(String name, String anotherName, String message)
      Validates two fields to exactly (case-sensitive) match
      Parameters:
      name - The field to check
      anotherName - The other field to check against
      message - A custom error message instead of the default one
    • expectMatch

      public void expectMatch(String name, String anotherName)
      Validates two fields to (case-insensitive) match
      Parameters:
      name - The field to check
      anotherName - The field to check against
    • expectMatch

      public void expectMatch(String name, String anotherName, String message)
      Validates two fields to (case-insensitive) match
      Parameters:
      name - The field to check
      anotherName - The field to check against
      message - A custom error message instead of the default one
    • expectMatch

      public void expectMatch(String name, List<String> values)
      Validates a list of given values to (case-sensitive) match
      Parameters:
      name - The field to check
      values - A list of given values to check against
    • expectMatch

      public void expectMatch(String name, String message, List<String> values)
      Validates a list of value to (case-sensitive) match
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
      values - A list of given values to check against
    • expectEmail

      public void expectEmail(String name)
      Validates a field to be a valid email address
      Parameters:
      name - The field to check
    • expectEmail

      public void expectEmail(String name, String message)
      Validates a field to be a valid email address
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
    • expectIpv4

      public void expectIpv4(String name)
      Validates a field to be a valid IPv4 address
      Parameters:
      name - The field to check
    • expectIpv4

      public void expectIpv4(String name, String message)
      Validates a field to be a valid IPv4 address
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
    • expectDomainName

      public void expectDomainName(String name)
      Validates a field to be a valid Domain name
      Parameters:
      name - The field to check
    • expectDomainName

      public void expectDomainName(String name, String message)
      Validates a field to be a valid Domain name
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
    • expectIpv6

      public void expectIpv6(String name)
      Validates a field to be a valid IPv6 address
      Parameters:
      name - The field to check
    • expectIpv6

      public void expectIpv6(String name, String message)
      Validates a field to be a valid IPv6 address
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
    • expectRangeLength

      public void expectRangeLength(String name, int minLength, int maxLength)
      Validates a field to be in a certain range length
      Parameters:
      name - The field to check
      minLength - The minimum length
      maxLength - The maximum length
    • expectRangeValue

      public void expectRangeValue(String name, int minValue, int maxValue)
      Validates a field to be in a certain range value
      Parameters:
      name - The field to check
      minValue - The minimum value
      maxValue - The maximum value
    • expectRangeValue

      public void expectRangeValue(String name, int minValue, int maxValue, String message)
      Validates a field to be in a certain range value
      Parameters:
      name - The field to check
      minValue - The minimum value
      maxValue - The maximum value
      message - A custom error message instead of the default one
    • expectRangeLength

      public void expectRangeLength(String name, int minLength, int maxLength, String message)
      Validates a field to be in a certain range length
      Parameters:
      name - The field to check
      minLength - The minimum length
      maxLength - The maximum length
      message - A custom error message instead of the default one
    • expectRegex

      public void expectRegex(String name, com.google.re2j.Pattern pattern)
      Validates a field by a given regular expression pattern It is required to pass a pre-compiled pattern, e.g. Pattern pattern = Pattern.compile("[a-Z,0-9]")
      Parameters:
      name - The field to check
      pattern - The pre-compiled pattern
    • expectRegex

      public void expectRegex(String name, com.google.re2j.Pattern pattern, String message)
      Validates a field by a given regular expression pattern It is required to pass a pre-compiled pattern, e.g. Pattern pattern = Pattern.compile("[a-Z,0-9]")
      Parameters:
      pattern - The pre-compiled pattern
      name - The field to check
      message - A custom error message instead of the default one
    • expectUrl

      public void expectUrl(String name)
      Validates field to be a valid URL
      Parameters:
      name - The field to check
    • expectUrl

      public void expectUrl(String name, String message)
      Validates field to be a valid URL
      Parameters:
      name - The field to check
      message - A custom error message instead of the default one
    • expectTrue

      public void expectTrue(String name, boolean value, String message)
      Validates a given value to be true
      Parameters:
      name - The name of the field to display the error message
      value - The value to check
      message - A custom error message instead of the default one
    • expectTrue

      public void expectTrue(String name, boolean value)
      Validates a given value to be true
      Parameters:
      name - The name of the field to display the error message
      value - The value to check
    • expectFalse

      public void expectFalse(String name, boolean value, String message)
      Validates a given value to be false
      Parameters:
      name - The name of the field to display the error message
      value - The value to check
      message - A custom error message instead of the default one
    • expectFalse

      public void expectFalse(String name, boolean value)
      Validates a given value to be false
      Parameters:
      name - The name of the field to display the error message
      value - The value to check
    • expectNotNull

      public void expectNotNull(String name, Object object, String message)
      Validates a given object to be not null
      Parameters:
      name - The name of the field to display the error message
      object - The object to check
      message - A custom error message instead of the default one
    • expectNotNull

      public void expectNotNull(String name, Object object)
      Validates a given object to be not null
      Parameters:
      name - The name of the field to display the error message
      object - The object to check
    • expectNull

      public void expectNull(String name, Object object, String message)
      Validates a given object to be null
      Parameters:
      name - The name of the field to display the error message
      object - The object to check
      message - A custom error message instead of the default one
    • expectNull

      public void expectNull(String name, Object object)
      Validates a given object to be null
      Parameters:
      name - The name of the field to display the error message
      object - The object to check
    • hasErrors

      public boolean hasErrors()
      Checks if any field in the validation has an error
      Returns:
      True if at least one field has an error, false otherwise
    • get

      public String get(String key)
      Retrieves a form value corresponding to the name of the form element
      Parameters:
      key - The name of the form element
      Returns:
      The value of the form or null if not present
    • getErrors

      public Map<String,String> getErrors()
    • setValues

      public void setValues(Map<String,String> values)
    • addValue

      public void addValue(String key, String value)
    • isValid

      public boolean isValid()
    • invalidate

      public void invalidate()