Backstopper is a framework-agnostic API error handling and (optional) model validation solution for Java 17 and greater.
(NOTE: The Backstopper 1.x branch contains a version of
Backstopper for Java 7+, and for the javax
ecosystem. The current Backstopper supports Java 17+ and the jakarta
ecosystem.)
This library contains JSR 303 Bean Validation annotations that have proven to be useful and reusable. These are entirely optional. They are also largely dependency free so this library is usable in non-Backstopper projects that utilize JSR 303 validations.
StringConvertsToClassType
- Validates that the annotated element (of type String) can be converted to the desiredclassType
. TheclassType
can be any of the following:- Any boxed primitive class type (e.g.
Integer.class
). - Any raw primitive class type (e.g.
int.class
). String.class
- a String can always be converted to a String, so this validator will always return true in this case.- Any enum class type - validation is done by comparing the string value to
Enum.name()
. The value of theallowCaseInsensitiveEnumMatch()
constraint property determines if the validation is done in a case sensitive or case insensitive manner. null
is always considered valid - if you need to enforce non-null then you should place an additional@NotNull
constraint on the field as well.- More information and usage instructions can be found in the javadocs for
StringConvertsToClassType
, but here's an example showing how you would mark a model field that you wanted to guarantee was convertible to aRgbColor
enum after passing JSR 303 validation:
- Any boxed primitive class type (e.g.
@StringConvertsToClassType(
message = "NOT_RGB_COLOR_ENUM", classType = RgbColor.class, allowCaseInsensitiveEnumMatch = true
)
public final String rgb_color;
See the base project README.md, User Guide, and Backstopper repository source code and javadocs for all further information.
Backstopper is released under the Apache License, Version 2.0