Package com.jsgalactic.axiom.validation
Class Rules<T>
java.lang.Object
com.jsgalactic.axiom.validation.Rules<T>
- Type Parameters:
T- type of value checked
- All Implemented Interfaces:
BodyValidator<T>,Validator<T>
An immutable, annotation-free validator built from field rules.
import static com.jsgalactic.axiom.validation.Rule.*;
static final Validator<CreateUser> CREATE_USER = Rules.of(CreateUser.class)
.field("name", CreateUser::name, notBlank(), maxLength(80))
.field("email", CreateUser::email, notNull(), email())
.field("age", CreateUser::age, range(0, 150))
.nested("address", CreateUser::address, ADDRESS)
.eachNested("items", CreateUser::items, ITEM)
.check("end", user -> !user.end().isBefore(user.start()), "before_start");
Every method returns a new instance, so partially built rule sets can be shared and extended;
instances are thread-safe as long as the supplied accessors, predicates and validators are. Steps
run in declaration order. Each field reports at most the first rule it fails. Nested validators
and collection elements extend field paths, for example items[2].name, and null
nested objects, lists and list elements are skipped by nested, each and
eachNested, so presence is stated with Rule.notNull(). Validation stops once Validation.MAX_VIOLATIONS violations are collected. Field names and codes are checked when the
rules are defined. Exceptions thrown by accessors, predicates or nested validators propagate
unchanged.
-
Method Summary
Modifier and TypeMethodDescriptionAdds a check of the whole value, such as a cross-field rule, reported atfield.Adds a check of the whole value, reported at the whole value (FieldPath.ROOT).Describes the built-in rules declared withfield(java.lang.String, java.util.function.Function<? super T, ? extends V>, com.jsgalactic.axiom.validation.Rule<? super V>...)andeach(java.lang.String, java.util.function.Function<? super T, ? extends java.util.List<? extends E>>, com.jsgalactic.axiom.validation.Rule<? super E>...), for tools that document the rule set, such as an OpenAPI generator.each(String name, Function<? super T, ? extends List<? extends E>> accessor, Rule<? super E>... rules) Applies rules to every element of a list, reporting atname[index]; skips anulllist but checksnullelements.eachNested(String name, Function<? super T, ? extends List<? extends E>> accessor, Validator<? super E> validator) Validates every non-null element of a list, prefixing violations withname[index]; skips anulllist andnullelements.Adds rules for one field, reporting the first rule it fails atname.Runs another validator on the whole value, keeping its violation fields.Validates a nested object, prefixing its violations withname; skipsnull.static <T> Rules<T> Starts an empty rule set; it accepts every non-null value.
-
Method Details
-
of
Starts an empty rule set; it accepts every non-null value.- Type Parameters:
T- type of value checked- Parameters:
type- type of value checked, used for type inference only- Returns:
- empty rule set
-
field
@SafeVarargs public final <V> Rules<T> field(String name, Function<? super T, ? extends V> accessor, Rule<? super V>... rules) Adds rules for one field, reporting the first rule it fails atname.- Type Parameters:
V- field type- Parameters:
name- property name, seeFieldPath.isPropertyName(String)accessor- reads the fieldrules- rules checked in order- Returns:
- extended rule set
-
nested
public <V> Rules<T> nested(String name, Function<? super T, ? extends V> accessor, Validator<? super V> validator) Validates a nested object, prefixing its violations withname; skipsnull.- Type Parameters:
V- nested type- Parameters:
name- property nameaccessor- reads the nested objectvalidator- validator for the nested object- Returns:
- extended rule set
-
each
@SafeVarargs public final <E> Rules<T> each(String name, Function<? super T, ? extends List<? extends E>> accessor, Rule<? super E>... rules) Applies rules to every element of a list, reporting atname[index]; skips anulllist but checksnullelements.- Type Parameters:
E- element type- Parameters:
name- property nameaccessor- reads the listrules- rules checked in order for each element- Returns:
- extended rule set
-
eachNested
public <E> Rules<T> eachNested(String name, Function<? super T, ? extends List<? extends E>> accessor, Validator<? super E> validator) Validates every non-null element of a list, prefixing violations withname[index]; skips anulllist andnullelements.- Type Parameters:
E- element type- Parameters:
name- property nameaccessor- reads the listvalidator- validator for each element- Returns:
- extended rule set
-
check
Adds a check of the whole value, reported at the whole value (FieldPath.ROOT).- Parameters:
test- predicate returning true for valid values; never called with nullcode- violation code- Returns:
- extended rule set
-
check
Adds a check of the whole value, such as a cross-field rule, reported atfield.- Parameters:
field- violation field path, for exampleperiod.endtest- predicate returning true for valid values; never called with nullcode- violation code- Returns:
- extended rule set
-
include
Runs another validator on the whole value, keeping its violation fields.- Parameters:
validator- validator to include, for example a Jakarta adapter- Returns:
- extended rule set
-
constraints
Describes the built-in rules declared withfield(java.lang.String, java.util.function.Function<? super T, ? extends V>, com.jsgalactic.axiom.validation.Rule<? super V>...)andeach(java.lang.String, java.util.function.Function<? super T, ? extends java.util.List<? extends E>>, com.jsgalactic.axiom.validation.Rule<? super E>...), for tools that document the rule set, such as an OpenAPI generator. Keys are the property name, or the name followed by[]for rules applied to each list element; values are the constraints of the rules in declaration order, with custom rules left out. Rules ofnested(java.lang.String, java.util.function.Function<? super T, ? extends V>, com.jsgalactic.axiom.validation.Validator<? super V>),eachNested(java.lang.String, java.util.function.Function<? super T, ? extends java.util.List<? extends E>>, com.jsgalactic.axiom.validation.Validator<? super E>),check(java.util.function.Predicate<? super T>, java.lang.String)andinclude(com.jsgalactic.axiom.validation.Validator<? super T>)steps are not part of the description; describe the nested validator itself. Properties declared more than once are merged in declaration order.- Returns:
- immutable constraints by property, in declaration order
-
validate
- Specified by:
validatein interfaceBodyValidator<T>- Specified by:
validatein interfaceValidator<T>- Parameters:
value- value to check- Returns:
- immutable violations, at most
Validation.MAX_VIOLATIONS
-