You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SlimValidator is a Java library for providing object validation through annotations. It is inspired by the Java Bean Validation specification but is not a implementation at all.
@@ -153,6 +199,20 @@ reference type must be or String or Collection<String> (max 3 items).
153
199
privateList<Project> projects;
154
200
```
155
201
202
+
### @Extension
203
+
-**Description**:Checks that the file extension is one of an expected list.
204
+
-**Applies to**:Fields of type:java.nio.file.Path, java.io.File.
205
+
-**Parameters**:
206
+
- _value_:Array of expected extensions. Mandatory.
207
+
-**Error messages**:
208
+
-If file extension is not any of the _value_ array:
209
+
- _extension must be one of {value}._
210
+
-**Example**:
211
+
```java
212
+
@Extension({"doc", "xls", "txt"})
213
+
privatePath evidenceFile;
214
+
```
215
+
156
216
### @ObjectType
157
217
-**Description**:Checks that the type of an object is one of a list of candidate types.
158
218
-**Applies to**:Fields of the Object type, including Collection of objects or Collection of Collection of objects. Collection can be any subinterfacesuch as: List, Set, etc.
@@ -196,27 +256,70 @@ reference type must be or String or Collection<String> (max 3 items).
196
256
privateAddress mainAddress;
197
257
```
198
258
199
-
## 🛠️ Installation
259
+
## 🪝 CreateNewConstraint
260
+
For creating a new constraint you need to create both a new constraint annotation and a new validator class:
200
261
201
-
You can install this library by adding the following dependency to your Maven project:
262
+
### New Constraint Annotation
263
+
Create a new annotation `YourNewConstraint` with the following template:
264
+
```java
265
+
@Documented
266
+
@Constraint(validatedBy =YourNewValidator.class)
267
+
@Target({ ElementType.FIELD })
268
+
@Retention(RetentionPolicy.RUNTIME)
269
+
public@interfaceYourNewConstraint {
202
270
203
-
```xml
204
-
<dependency>
205
-
<groupId>io.github.sashirestela</groupId>
206
-
<artifactId>slimvalidator</artifactId>
207
-
<version>[latest version]</version>
208
-
</dependency>
209
-
```
271
+
Stringmessage() default "<your custom message when validation fails>.";
210
272
211
-
Or alternatively using Gradle:
273
+
// Add any other annotation methods needed by your new constraint.
- Use the `@Constraint` annotation to link `YourNewConstraint` annotation to `YourNewValidator` class.
278
+
- Define at least the `message()` method. This is the message to show when validation fails. Here you can use optionally:
279
+
- Curly brackets to reference other annotation methods. For example: `some text with {max} value.`. The message includes the value of the annotation method `max()`.
280
+
- Conditional segments based on the value of some annotation method. For example: `#if(max)some text with {max} value.#endif`. The message includes the value of the annotation method `max()` and it will be shown only if the `max()` is not empty. In this context, "empty" depends on the the annotation method type:
281
+
- If boolean, empty means the value is false.
282
+
- If String, empty means the text is empty.
283
+
- If double, empty means the number is Double.MIN_VALUE or Double.MAX_VALUE.
284
+
- If int, empty means the number is zero or Integer.MAX_VALUE.
285
+
- If Class, empty means the class is equals to javax.lang.model.type.NullType.
286
+
- If array, empty means the array has no elements.
287
+
- Loop segments for constraint annotations defined as arrays. For example: `type must be#for(value) or {message}#endfor.`. That message will concatenate the `message()` of each constraint in the constraint array. The argument `value` is not meaningful.
288
+
- Add any other annotation methods needed by your new constraint.
289
+
290
+
### New Validator Class
291
+
Create a new class `YourNewValidator` with the following template:
- Implement the `ConstraintValidator<A, T>` interface, where A represents YourNewConstraint and T represents the class of the objects to validate, in this case, you can use `Object` if your validations applies to more than one class.
320
+
- Create as field members as annotation methods you have in YourNewConstraint, excluding message().
321
+
- Overrides the `initialize()` method to capture the annotation method values in your field members.
322
+
- Overrides the `isValid()` method to do the validation logic. Your first validation step must return true if the object to validate is null, because we have the annotation `@Required` to validate that condition, we don't want to evaluate that nullity here.
220
323
221
324
## 💼 Contributing
222
325
Please read our [Contributing](CONTRIBUTING.md) guide to learn and understand how to contribute to this project.
0 commit comments