Class Rule<T>

java.lang.Object
com.jsgalactic.axiom.validation.Rule<T>
Type Parameters:
T - type of value checked

public final class Rule<T> extends Object
An immutable check of one value with a fixed violation code.

Rules are used with Rules to describe a type's constraints without annotations. Most rules accept null so that presence is stated explicitly with notNull(); notNull(), notBlank() and notEmpty() reject it. Codes are fixed, validated when the rule is created, and never derived from the checked value. Rules are thread-safe as long as their predicates are.

  • Field Details

    • DEFAULT_PATTERN_INPUT_LIMIT

      public static final int DEFAULT_PATTERN_INPUT_LIMIT
      Longest input pattern(String) hands to the regular expression engine.
      See Also:
  • Method Details

    • check

      public static <T> Rule<T> check(Predicate<? super T> test, String code)
      Creates a custom rule; null values pass without calling the predicate.
      Type Parameters:
      T - value type
      Parameters:
      test - predicate that returns true for valid, non-null values
      code - violation code, [a-z][a-z0-9_.-]{0,63}
      Returns:
      the rule
      Throws:
      IllegalArgumentException - if the code is not a safe code
    • checkNullable

      public static <T> Rule<T> checkNullable(Predicate<? super T> test, String code)
      Creates a custom rule whose predicate also decides whether null is valid.
      Type Parameters:
      T - value type
      Parameters:
      test - predicate that returns true for valid values, including null if allowed
      code - violation code, [a-z][a-z0-9_.-]{0,63}
      Returns:
      the rule
      Throws:
      IllegalArgumentException - if the code is not a safe code
    • withCode

      public Rule<T> withCode(String code)
      Returns this rule with another code.
      Parameters:
      code - violation code, [a-z][a-z0-9_.-]{0,63}
      Returns:
      a rule with the same check
    • code

      public String code()
      Returns the code reported when the check fails.
      Returns:
      safe machine-readable code
    • constraint

      public Optional<Constraint> constraint()
      Describes what this rule checks, for tools such as documentation generators.
      Returns:
      the constraint of a built-in rule, empty for rules made with check(java.util.function.Predicate<? super T>, java.lang.String) or checkNullable(java.util.function.Predicate<? super T>, java.lang.String), whose predicates cannot be described
    • test

      public boolean test(T value)
      Checks a value.
      Parameters:
      value - value to check
      Returns:
      true if the value is valid
    • notNull

      public static Rule<Object> notNull()
      Rejects null. Code not_null.
      Returns:
      the rule
    • notBlank

      public static Rule<CharSequence> notBlank()
      Rejects null and text that is empty or only whitespace. Code not_blank.
      Returns:
      the rule
    • notEmpty

      public static Rule<Object> notEmpty()
      Rejects null and empty text, collections, maps and arrays; other non-null values pass. Code not_empty.
      Returns:
      the rule
    • length

      public static Rule<CharSequence> length(int min, int max)
      Bounds text length in Unicode code points, inclusive. Code size.
      Parameters:
      min - minimum length, at least 0
      max - maximum length, at least min
      Returns:
      the rule
    • minLength

      public static Rule<CharSequence> minLength(int min)
      Requires at least min code points. Code size.
      Parameters:
      min - minimum length, at least 0
      Returns:
      the rule
    • maxLength

      public static Rule<CharSequence> maxLength(int max)
      Allows at most max code points. Code size.
      Parameters:
      max - maximum length, at least 0
      Returns:
      the rule
    • size

      public static Rule<Collection<?>> size(int min, int max)
      Bounds the number of elements in a collection, inclusive. Code size.
      Parameters:
      min - minimum size, at least 0
      max - maximum size, at least min
      Returns:
      the rule
    • min

      public static Rule<Number> min(long min)
      Requires a number of at least min, compared exactly; NaN fails. Code min.
      Parameters:
      min - inclusive lower bound
      Returns:
      the rule
    • max

      public static Rule<Number> max(long max)
      Requires a number of at most max, compared exactly; NaN fails. Code max.
      Parameters:
      max - inclusive upper bound
      Returns:
      the rule
    • range

      public static Rule<Number> range(long min, long max)
      Requires a number between min and max, inclusive; NaN fails. Code range.
      Parameters:
      min - inclusive lower bound
      max - inclusive upper bound, at least min
      Returns:
      the rule
    • pattern

      public static Rule<CharSequence> pattern(String regex)
      Requires the whole text to match a regular expression, refusing input longer than 1000<T> characters without matching it. Code pattern.
      Parameters:
      regex - Java regular expression, compiled once
      Returns:
      the rule
      Throws:
      IllegalArgumentException - if the expression is invalid
      See Also:
    • pattern

      public static Rule<CharSequence> pattern(String regex, int maxInputLength)
      Requires the whole text to match a regular expression. Code pattern.

      Java's regular expression engine backtracks, so expressions with nested or overlapping repetition such as (a+)+b can take exponential time on crafted input. The expression is compiled once; input longer than maxInputLength characters fails without reaching the engine. Prefer simple, anchored-by-construction expressions with a small limit and never build expressions from client input.

      Parameters:
      regex - Java regular expression, compiled once
      maxInputLength - longest input matched, at least 1
      Returns:
      the rule
      Throws:
      IllegalArgumentException - if the expression is invalid or the limit is below 1
    • email

      public static Rule<CharSequence> email()
      Checks e-mail address syntax only, without a regular expression: one @, a local part of 1 to 64 printable ASCII characters other than spaces and "(),:;<>@[\], and a domain of at least two dot-separated labels of ASCII letters, digits and inner hyphens; at most 254 characters. Quoted local parts, IP literals and internationalized addresses are rejected. It does not check that the address exists or can receive mail. Code email.
      Returns:
      the rule
    • oneOf

      public static Rule<CharSequence> oneOf(String... values)
      Requires text equal to one of the listed values. Code one_of.
      Parameters:
      values - allowed values, at least one
      Returns:
      the rule