Package com.jsgalactic.axiom.validation
Class Rule<T>
java.lang.Object
com.jsgalactic.axiom.validation.Rule<T>
- Type Parameters:
T- type of value checked
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intLongest inputpattern(String)hands to the regular expression engine. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Rule<T> Creates a custom rule;nullvalues pass without calling the predicate.static <T> Rule<T> checkNullable(Predicate<? super T> test, String code) Creates a custom rule whose predicate also decides whethernullis valid.code()Returns the code reported when the check fails.Describes what this rule checks, for tools such as documentation generators.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.static Rule<CharSequence> length(int min, int max) Bounds text length in Unicode code points, inclusive.max(long max) Requires a number of at mostmax, compared exactly; NaN fails.static Rule<CharSequence> maxLength(int max) Allows at mostmaxcode points.min(long min) Requires a number of at leastmin, compared exactly; NaN fails.static Rule<CharSequence> minLength(int min) Requires at leastmincode points.static Rule<CharSequence> notBlank()Rejectsnulland text that is empty or only whitespace.notEmpty()Rejectsnulland empty text, collections, maps and arrays; other non-null values pass.notNull()Rejectsnull.static Rule<CharSequence> Requires text equal to one of the listed values.static Rule<CharSequence> static Rule<CharSequence> Requires the whole text to match a regular expression.range(long min, long max) Requires a number betweenminandmax, inclusive; NaN fails.static Rule<Collection<?>> size(int min, int max) Bounds the number of elements in a collection, inclusive.booleanChecks a value.Returns this rule with another code.
-
Field Details
-
DEFAULT_PATTERN_INPUT_LIMIT
public static final int DEFAULT_PATTERN_INPUT_LIMITLongest inputpattern(String)hands to the regular expression engine.- See Also:
-
-
Method Details
-
check
Creates a custom rule;nullvalues pass without calling the predicate.- Type Parameters:
T- value type- Parameters:
test- predicate that returns true for valid, non-null valuescode- violation code,[a-z][a-z0-9_.-]{0,63}- Returns:
- the rule
- Throws:
IllegalArgumentException- if the code is not a safe code
-
checkNullable
Creates a custom rule whose predicate also decides whethernullis valid.- Type Parameters:
T- value type- Parameters:
test- predicate that returns true for valid values, including null if allowedcode- violation code,[a-z][a-z0-9_.-]{0,63}- Returns:
- the rule
- Throws:
IllegalArgumentException- if the code is not a safe code
-
withCode
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
Returns the code reported when the check fails.- Returns:
- safe machine-readable code
-
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)orcheckNullable(java.util.function.Predicate<? super T>, java.lang.String), whose predicates cannot be described
-
test
Checks a value.- Parameters:
value- value to check- Returns:
- true if the value is valid
-
notNull
Rejectsnull. Codenot_null.- Returns:
- the rule
-
notBlank
Rejectsnulland text that is empty or only whitespace. Codenot_blank.- Returns:
- the rule
-
notEmpty
Rejectsnulland empty text, collections, maps and arrays; other non-null values pass. Codenot_empty.- Returns:
- the rule
-
length
Bounds text length in Unicode code points, inclusive. Codesize.- Parameters:
min- minimum length, at least 0max- maximum length, at leastmin- Returns:
- the rule
-
minLength
Requires at leastmincode points. Codesize.- Parameters:
min- minimum length, at least 0- Returns:
- the rule
-
maxLength
Allows at mostmaxcode points. Codesize.- Parameters:
max- maximum length, at least 0- Returns:
- the rule
-
size
Bounds the number of elements in a collection, inclusive. Codesize.- Parameters:
min- minimum size, at least 0max- maximum size, at leastmin- Returns:
- the rule
-
min
Requires a number of at leastmin, compared exactly; NaN fails. Codemin.- Parameters:
min- inclusive lower bound- Returns:
- the rule
-
max
Requires a number of at mostmax, compared exactly; NaN fails. Codemax.- Parameters:
max- inclusive upper bound- Returns:
- the rule
-
range
Requires a number betweenminandmax, inclusive; NaN fails. Coderange.- Parameters:
min- inclusive lower boundmax- inclusive upper bound, at leastmin- Returns:
- the rule
-
pattern
Requires the whole text to match a regular expression, refusing input longer than 1000<T> characters without matching it. Codepattern.- Parameters:
regex- Java regular expression, compiled once- Returns:
- the rule
- Throws:
IllegalArgumentException- if the expression is invalid- See Also:
-
pattern
Requires the whole text to match a regular expression. Codepattern.Java's regular expression engine backtracks, so expressions with nested or overlapping repetition such as
(a+)+bcan take exponential time on crafted input. The expression is compiled once; input longer thanmaxInputLengthcharacters 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 oncemaxInputLength- longest input matched, at least 1- Returns:
- the rule
- Throws:
IllegalArgumentException- if the expression is invalid or the limit is below 1
-
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. Codeemail.- Returns:
- the rule
-
oneOf
Requires text equal to one of the listed values. Codeone_of.- Parameters:
values- allowed values, at least one- Returns:
- the rule
-