-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
liblab SDK update for version v0.9.0-alpha.3
- Loading branch information
1 parent
e32244d
commit 1c97e66
Showing
16 changed files
with
341 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/salad/cloud/imdssdk/validation/Violation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.salad.cloud.imdssdk.validation; | ||
|
||
public class Violation { | ||
|
||
private final String path; | ||
private final String message; | ||
|
||
public Violation(String path, String message) { | ||
this.path = path; | ||
this.message = message; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s: %s", path, message); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/salad/cloud/imdssdk/validation/ViolationAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.salad.cloud.imdssdk.validation; | ||
|
||
import com.salad.cloud.imdssdk.validation.exceptions.ValidationException; | ||
import com.salad.cloud.imdssdk.validation.validators.AbstractValidator; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ViolationAggregator { | ||
|
||
List<Violation> violations = new ArrayList<>(); | ||
|
||
public <T> ViolationAggregator add(AbstractValidator<T> validator, T value) { | ||
Violation[] violations = validator.validate(value); | ||
if (violations.length > 0) { | ||
this.violations.addAll(Arrays.asList(violations)); | ||
} | ||
return this; | ||
} | ||
|
||
public Violation[] aggregate() { | ||
return violations.toArray(new Violation[0]); | ||
} | ||
|
||
public void validateAll() { | ||
if (!violations.isEmpty()) { | ||
throw new ValidationException(aggregate()); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/salad/cloud/imdssdk/validation/exceptions/ValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.salad.cloud.imdssdk.validation.exceptions; | ||
|
||
import com.salad.cloud.imdssdk.validation.Violation; | ||
import lombok.Getter; | ||
|
||
public class ValidationException extends RuntimeException { | ||
|
||
@Getter | ||
private final Violation[] violations; | ||
|
||
public ValidationException(Violation[] violations) { | ||
super(); | ||
this.violations = violations; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return "Validation failed with the following violations: " + this.buildViolationsString(); | ||
} | ||
|
||
private String buildViolationsString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (Violation violation : this.violations) { | ||
sb.append(violation.toString()).append("\n"); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/salad/cloud/imdssdk/validation/validators/AbstractValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.salad.cloud.imdssdk.validation.validators; | ||
|
||
public abstract class AbstractValidator<T> implements Validator<T> { | ||
|
||
private String fieldName; | ||
|
||
public AbstractValidator(String fieldName) { | ||
this.fieldName = fieldName; | ||
} | ||
|
||
public AbstractValidator() { | ||
this.fieldName = ""; | ||
} | ||
|
||
public String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
public AbstractValidator<T> setFieldName(String fieldName) { | ||
this.fieldName = fieldName; | ||
return this; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/com/salad/cloud/imdssdk/validation/validators/ListValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.salad.cloud.imdssdk.validation.validators; | ||
|
||
import com.salad.cloud.imdssdk.validation.Violation; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ListValidator<T> extends AbstractValidator<List<T>> { | ||
|
||
private AbstractValidator<T> itemValidator; | ||
private Integer minLength; | ||
private Integer maxLength; | ||
private Boolean uniqueItems; | ||
|
||
public ListValidator(String fieldName) { | ||
super(fieldName); | ||
} | ||
|
||
public ListValidator() {} | ||
|
||
public ListValidator<T> minLength(Integer minLength) { | ||
this.minLength = minLength; | ||
return this; | ||
} | ||
|
||
public ListValidator<T> maxLength(Integer maxLength) { | ||
this.maxLength = maxLength; | ||
return this; | ||
} | ||
|
||
public ListValidator<T> uniqueItems(Boolean uniqueItems) { | ||
this.uniqueItems = uniqueItems; | ||
return this; | ||
} | ||
|
||
public ListValidator<T> itemValidator(AbstractValidator<T> itemValidator) { | ||
this.itemValidator = itemValidator; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Violation[] validate(List<T> list) { | ||
List<Violation> errors = new ArrayList<>(); | ||
|
||
if (minLength != null && list.size() < minLength) { | ||
errors.add(new Violation(getFieldName(), String.format("must have at least %d items", minLength))); | ||
} | ||
|
||
if (maxLength != null && list.size() > maxLength) { | ||
errors.add(new Violation(getFieldName(), String.format("must have at most %d items", maxLength))); | ||
} | ||
|
||
if (uniqueItems != null && list.stream().distinct().count() != list.size()) { | ||
errors.add(new Violation(getFieldName(), "must have unique items")); | ||
} | ||
|
||
if (itemValidator != null) { | ||
for (int i = 0; i < list.size(); i++) { | ||
T item = list.get(i); | ||
Violation[] itemErrors = itemValidator.setFieldName(String.valueOf(i)).validate(item); | ||
for (Violation itemError : itemErrors) { | ||
errors.add( | ||
new Violation(String.format("%s[%s]", getFieldName(), itemValidator.getFieldName()), itemError.getMessage()) | ||
); | ||
} | ||
} | ||
} | ||
|
||
return errors.toArray(new Violation[0]); | ||
} | ||
} |
Oops, something went wrong.