@@ -3,9 +3,166 @@ A rule based validator developed for easy use with the Spring Boot framework.
3
3
4
4
- * Extendable* : You can always add custom rules
5
5
6
+ ## Adding dependency
7
+ To use the validator add the following dependency to your ` pom.xml ` .
8
+ ``` xml
9
+ <dependency >
10
+ <groupId >dev.ditsche</groupId >
11
+ <artifactId >validator</artifactId >
12
+ <version >2.2.0</version >
13
+ </dependency >
14
+ ```
15
+
6
16
## Usage
7
- The Validator is generic and can be used to validate multiple objects.
17
+
18
+ Easiest way to use the validator in a Spring Boot project is to setup a ` ValidatorConfig ` class. There you can
19
+ define a bean for each validator you need. Or you can create a validator in the controller method, do it, as you like.
20
+
21
+ A validator ist built using static helper methods. Here is a working example:
22
+
23
+ ``` java
24
+ Validator validator = Validator . fromRules(
25
+ string(" name" ). required(). trim(). alphanum(). max(50 ),
26
+ string(" gender" ). optional(). defaultValue(" m" ). max(1 ),
27
+ number(" age" ). required(). min(18 ),
28
+ object(" tickets" ). fields(
29
+ string(" event" ). required(). trim()
30
+ )
31
+ );
32
+ ```
33
+
34
+ This snippet validates an incoming DTO for the given fields and the nested object ` tickets ` .
35
+
36
+ To validate an object, just call the validate method of the validator. Pass in the object and the optional ` abortEarly `
37
+ parameter. This is a boolean indicating, if the ` ValidationException ` should be thrown after the first validation error
38
+ or at the end if there are any.
39
+
40
+ Make sure, that if you use altering rules like ` defaul() ` or ` trim() ` you reassign the input object with the result of
41
+ the validate method. That way you get the validated and updated input DTO for further operations.
42
+
43
+ ``` java
44
+ dto = validator. validate(dto);
45
+ ```
46
+
47
+ ## Internationalization
48
+ Unfortunately it is not possible to change the outputted language to something different than english.
49
+ If you need to provide other languages as well, you can make use of the error type. When
50
+ you return the result of the ` getErrors() ` method of the ErrorBag that you got from the thrown
51
+ ` ValidationException ` you have the error type available and can show messages based on this
52
+ unique key.
53
+
54
+ ## Builders and Methods
55
+ As said, to generate a validator you should make use of the static helper methods.
56
+ The following types are supported at the moment:
57
+ - string
58
+ - number (int, long, float, ...)
59
+ - object
60
+ - array
61
+
62
+ In the following sections you learn which methods and rules are available by default and how
63
+ to use them.
64
+
65
+ ### ` string ` rules
66
+
67
+ #### Usage
68
+ The following snippet returns an instance of the ` StringRuleBuilder ` class.
69
+ ``` java
70
+ string(field)
71
+ ```
72
+
73
+ #### Available rules
74
+
75
+ - ##### ` required() `
76
+ Marks the field as * required* meaning it cannot be null or empty.
77
+
78
+ - ##### ` optional() `
79
+ Marks the field as * optional* . All rules behind this rule can fail.
80
+
81
+ - ##### ` trim() `
82
+ * Trims the input and alters* the value to the trimmed string.
83
+
84
+ - ##### ` length(int) `
85
+ Checks if the provided string has the * exact same length* as the provided parameter ` length ` .
86
+
87
+ - ##### ` between(int, int) `
88
+ Checks if the * length of the string is between* the first and second parameter integers.
89
+
90
+ - ##### ` min(int) `
91
+ Checks if the length of the string is * greater or equal* to the given parameter.
92
+
93
+ - ##### ` max(int) `
94
+ Checks if the length of the string is * smaller or equal* to the given parameter.
95
+
96
+ - ##### ` email() `
97
+ Checks if the fields value is a * valid email address* .
98
+
99
+ - ##### ` url() `
100
+ Checks if the fields value is a * valid url* .
101
+
102
+ - ##### ` pattern(String) `
103
+ Checks if the fields value * matches the given Regex* pattern. The
104
+ ` pattern ` parameter should be the ` String ` representation of a Regex.
105
+
106
+ - ##### ` alphanum() `
107
+ Checks if the fields value is * alphanumeric* .
108
+
109
+ - ##### ` ip() `
110
+ Checks if the fields value is a * valid ipv4 address* .
111
+
112
+ - ##### ` creditCard() `
113
+ Checks if the fields value is a * valid credit card number* .
114
+
115
+ - ##### ` defaultValue(String) `
116
+ * Sets the fields value to the given string* , if the fields value is null or empty.
117
+
118
+ - ##### ` custom(Rule) `
119
+ Registers a custom defined rule.
120
+
121
+ ---
122
+
123
+
124
+
125
+ ## Custom rules
126
+ You can easily extend the functionality of the validator by defining custom rules. If you need a specific Regex and don't
127
+ want to use the ` PatternRule ` over and over again you can create your own rule.
128
+
129
+ To do this, implement the ` Rule ` interface and add an instance of your rule to the validator like shown below:
130
+
131
+ ``` java
132
+ import dev.ditsche.validator.rule.RuleResult ;public class MyRule implements Rule {
133
+ @Override
134
+ public RuleResult test (Object value ) {
135
+
136
+ // Your validation logic
137
+
138
+
139
+ // You have the following methods to generate a result:
140
+ // RuleResult.reject() -> Rejects the rule and marks it as not passed.
141
+ // RuleResult.resolve() -> Resolve the rule and mark it passed.
142
+ // RuleResult.resolve(Object) -> Resolve the rule and update the value of the field.
143
+ // RuleResult.passes(boolean) -> Resolves or rejects based on the given boolean or expression.
144
+ return RuleResult . resolve();
145
+ }
146
+
147
+ @Override
148
+ public String message (String field ) {
149
+ // The error message
150
+ return String . format(" The field \" %s\" is a custom error" , field);
151
+ }
152
+
153
+ @Override
154
+ public String getType () {
155
+ // The error type, to make internationalization possible
156
+ return RULE_TYPE_PREFIX + " my.rule" ;
157
+ }
158
+ }
159
+ ```
160
+
161
+ And use that rule in a validator:
162
+
8
163
``` java
9
- Validator<User > validator = new Validator<> ();
10
- validator. addRule()
11
- ```
164
+ Validator . fromRules(
165
+ ... ,
166
+ string(" myfield" ). custom(new MyRule ())
167
+ );
168
+ ```
0 commit comments